Color comparison

This commit is contained in:
Kornel Lesiński 2019-09-10 16:38:22 +01:00
parent 1b4dcad2dd
commit a1d05e6b0a

View file

@ -64,16 +64,16 @@ pub fn reduced_palette(png: &PngImage) -> Option<PngImage> {
let mut used_enumerated : Vec<(usize, &bool)>= used.iter().enumerate().collect(); let mut used_enumerated : Vec<(usize, &bool)>= used.iter().enumerate().collect();
used_enumerated.sort_by(|a, b| { used_enumerated.sort_by(|a, b| {
//Sort by ascending alpha and descending luma. //Sort by ascending alpha and descending luma.
let color_val = |palette : &Vec<rgb::RGBA<u8>>, i| { let color_val = |i| {
let color = palette.get(i).cloned() let color = palette.get(i).copied()
.unwrap_or_else(|| RGBA8::new(0, 0, 0, 255)); .unwrap_or_else(|| RGBA8::new(0, 0, 0, 255));
let mut result = (color.a as i32) << 18; ((color.a as i32) << 18)
result -= (color.r as i32) * 299; // These are coefficients for standard sRGB to luma conversion
result -= (color.g as i32) * 587; - (color.r as i32) * 299
result -= (color.b as i32) * 114; - (color.g as i32) * 587
result - (color.b as i32) * 114
}; };
color_val(palette, a.0).cmp(&color_val(palette, b.0)) color_val(a.0).cmp(&color_val(b.0))
}); });
let mut next_index = 0u16; let mut next_index = 0u16;