about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDevon Sawatsky <novedevo@gmail.com>2021-09-27 20:32:28 -0700
committergennyble <gen@nyble.dev>2021-09-28 23:05:18 +0000
commit94a6f25f35ffd44cb2028ea431197fe000bc8f7b (patch)
tree8e787c4aa1b79aed08f38dcc2e4b3c37f7a77ef7
parentb1f06dbfb6404434ab211add1a12b0b08dcbc7e2 (diff)
downloadcolorsquash-94a6f25f35ffd44cb2028ea431197fe000bc8f7b.tar.gz
colorsquash-94a6f25f35ffd44cb2028ea431197fe000bc8f7b.zip
fix pernicious bug in difference selection
-rw-r--r--src/main.rs31
1 files changed, 9 insertions, 22 deletions
diff --git a/src/main.rs b/src/main.rs
index 9b75920..7f1e1fa 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -75,37 +75,24 @@ fn quantize(pixels: Pixels<Rgb<u8>>) -> Vec<Rgb<u8>> {
             .then(colour2[2].cmp(&colour1[2]))
     });
 
-    println!("Sorted! Selecting colors...");
-
-    let mut sorted_iter = sorted.iter();
-
     let mut selected_colors: Vec<Rgb<u8>> = Vec::with_capacity(MAX_COLORS);
-    selected_colors.push(sorted_iter.next().unwrap().0);
-
-    for (key, _value) in sorted_iter {
-        if selected_colors.len() < MAX_COLORS {
-            for selected_color in selected_colors.iter() {
-                if rgb_difference(key, selected_color) > RGB_TOLERANCE {
-                    selected_colors.push(*key);
-                    break;
-                }
-            }
-        } else {
+
+    for (key, _value) in sorted.iter() {
+        if selected_colors.len() >= MAX_COLORS {
             break;
+        } else if selected_colors
+            .iter()
+            .all(|color| rgb_difference(key, color) > RGB_TOLERANCE)
+        {
+            selected_colors.push(*key);
         }
     }
 
     selected_colors
 }
+
 #[allow(clippy::many_single_char_names)]
 fn rgb_difference(a: &Rgb<u8>, z: &Rgb<u8>) -> f32 {
-    //((a.0[0] as i16 - b.0[0] as i16).abs() + (a.0[1] as i16 - b.0[1] as i16).abs() +(a.0[2] as i16 - b.0[2] as i16).abs()) as u16
-    //(a.0[0] as i16 - b.0[0] as i16).abs().max((a.0[1] as i16 - b.0[1] as i16).abs().max(a.0[2] as i16 - b.0[2] as i16).abs()) as u16
-    //(a.0[0] as i16 - b.0[0] as i16).abs().max((a.0[1] as i16 - b.0[1] as i16).abs()).max((a.0[2] as i16 - b.0[2] as i16).abs()) as u16
-    /*(((a.0[0] as i32 - b.0[0] as i32) * (a.0[0] as i32 - b.0[0] as i32))
-    + ((a.0[1] as i32 - b.0[1] as i32) * (a.0[1] as i32 - b.0[1] as i32))
-    + ((a.0[2] as i32 - b.0[2] as i32) * (a.0[2] as i32 - b.0[2] as i32)))
-    .abs() as u16*/
     let (a, b, c) = pixel_rgb_to_hsv(a);
     let (d, e, f) = pixel_rgb_to_hsv(z);