From 54be5b8cb0f494e28dbed06bc3a93aefd2a9caf1 Mon Sep 17 00:00:00 2001 From: gennyble Date: Mon, 9 Oct 2023 03:40:01 -0500 Subject: real, actual difference algorithms --- src/lib.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 383aa83..c16bfb2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -129,8 +129,10 @@ impl Squasher { } fn select_colors(sorted: &[(RGB8, usize)], max_colors: T, difference: &DiffFn) -> Vec { + // I made these numbers up #[allow(non_snake_case)] - let RGB_TOLERANCE: f32 = 0.01 * 768.0; + //let RGB_TOLERANCE: f32 = 0.01 * 768.0; + let RGB_TOLERANCE: f32 = 36.0; let mut selected_colors: Vec<(RGB8, usize)> = Vec::with_capacity(max_colors.as_usize()); for (key, count) in sorted.iter() { @@ -238,3 +240,19 @@ pub fn rgb_difference(a: &RGB8, b: &RGB8) -> f32 { let absdiff = |a: u8, b: u8| (a as f32 - b as f32).abs(); absdiff(a.r, b.r) + absdiff(a.g, b.g) + absdiff(a.b, b.b) } + +// https://en.wikipedia.org/wiki/Color_difference#sRGB +#[inline(always)] +pub fn redmean_difference(a: &RGB8, b: &RGB8) -> f32 { + let delta_r = a.r as f32 - b.r as f32; + let delta_g = a.g as f32 - b.g as f32; + let delta_b = a.b as f32 - b.b as f32; + // reasonably sure calling it prime is wrong, but + let r_prime = 0.5 * (a.r as f32 + b.r as f32); + + let red_part = (2.0 + (r_prime / 256.0)) * (delta_r * delta_r); + let green_part = 4.0 * (delta_g * delta_g); + let blue_part = (2.0 + (255.0 - r_prime) / 256.0) * (delta_b * delta_b); + + (red_part + green_part + blue_part).sqrt() +} -- cgit 1.4.1-3-g733a5