Use more accurate color ramp conversion method
parent
c4af44a5e4
commit
0dc7e95c21
|
@ -1,15 +1,45 @@
|
|||
use crate::reskit::utility;
|
||||
use std::error::Error;
|
||||
use std::{error::Error, ops::RangeInclusive};
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use image::{ GenericImageView, DynamicImage };
|
||||
|
||||
/**
|
||||
* Get a particular RGB component adjusted for the Mega Drive VDP colour ramp.
|
||||
* See colour ramp at https://plutiedev.com/vdp-color-ramp
|
||||
*/
|
||||
fn rgb_component_to_ramp( component: u16 ) -> Result<u16, Box<dyn Error>> {
|
||||
let ramp: [(RangeInclusive<u16>, (u16,u16)); 7] = [
|
||||
(0..=52, (0x0, 0x2)),
|
||||
(52..=87, (0x2, 0x4)),
|
||||
(87..=116, (0x4, 0x6)),
|
||||
(116..=144, (0x6, 0x8)),
|
||||
(144..=172, (0x8, 0xA)),
|
||||
(172..=206, (0xA, 0xC)),
|
||||
(206..=255, (0xC, 0xE))
|
||||
];
|
||||
|
||||
for i in 0..ramp.len() {
|
||||
let ( ramp_range, ( cram_round_down, cram_round_up ) ) = &ramp[ i ];
|
||||
if ramp_range.contains( &component ) {
|
||||
let range_midpoint = ramp_range.start() + ( ( ramp_range.end() - ramp_range.start() ) / 2 );
|
||||
return if component >= range_midpoint {
|
||||
Ok( *cram_round_up )
|
||||
} else {
|
||||
Ok( *cram_round_down )
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err( "rgb component not in range 0-255" )?
|
||||
}
|
||||
|
||||
pub fn color_to_palette( r: u16, g: u16, b: u16, palette: &mut [u16; 16] ) -> Result<u32, Box<dyn Error>> {
|
||||
let final_val =
|
||||
( ( r & 0x00F0 ) >> 4 ) |
|
||||
( g & 0x00F0 ) |
|
||||
( ( b & 0x00F0 ) << 4 );
|
||||
( rgb_component_to_ramp( b )? << 8 ) |
|
||||
( rgb_component_to_ramp( g )? << 4 ) |
|
||||
( rgb_component_to_ramp( r )? );
|
||||
|
||||
// Does the color already exist?
|
||||
for i in 0..palette.len() {
|
||||
|
|
Loading…
Reference in New Issue