about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2021-03-16 01:01:34 -0500
committergennyble <gen@nyble.dev>2021-03-16 01:01:34 -0500
commit97e113eedde3cd802a10eba8656ddb8e949ff985 (patch)
tree58a0603addc12fcc954e278780c1210c216dce02 /src
parent1b3e0e2e216b07124a84c00434fb436b9764eced (diff)
downloadcolorsquash-97e113eedde3cd802a10eba8656ddb8e949ff985.tar.gz
colorsquash-97e113eedde3cd802a10eba8656ddb8e949ff985.zip
Fix colors being non-deterministic
Diffstat (limited to 'src')
-rw-r--r--src/main.rs36
1 files changed, 29 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs
index 4b324c0..82d6a99 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,4 @@
-use std::{collections::HashMap, env::args, time::Instant};
+use std::{cmp::Ordering, collections::HashMap, env::args, time::Instant};
 
 use image::{io::Reader as ImageReader};
 use image::Rgb;
@@ -36,7 +36,33 @@ fn main() {
 	println!("{} has {} colors in it. Sorting most occuring to least...", filename, colors.len());
 
 	let mut sorted: Vec<(Rgb<u8>, usize)> = colors.into_iter().collect();
-	sorted.sort_by(|a, b| a.1.cmp(&b.1).reverse());
+	sorted.sort_by(|a, b| {
+		match a.1.cmp(&b.1) {
+			Ordering::Equal => {
+				match a.0.0[0].cmp(&b.0.0[0]) {
+					Ordering::Equal => {
+						match a.0.0[1].cmp(&b.0.0[1]) {
+							Ordering::Equal => {
+								match a.0.0[2].cmp(&b.0.0[2]) {
+									Ordering::Equal => {
+										panic!("Same color in count map, what?")
+									},
+									Ordering::Greater => Ordering::Less,
+									Ordering::Less => Ordering::Greater
+								}
+							},
+							Ordering::Greater => Ordering::Less,
+							Ordering::Less => Ordering::Greater
+						}
+					},
+					Ordering::Greater => Ordering::Less,
+					Ordering::Less => Ordering::Greater
+				}
+			},
+			Ordering::Greater => Ordering::Less,
+			Ordering::Less => Ordering::Greater
+		}
+	});
 
 	println!("Sorted! Selecting colors...");
 
@@ -119,8 +145,4 @@ fn rgb_difference(a: &Rgb<u8>, b: &Rgb<u8>) -> u16 {
 	//((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
-}
-
-//rd.abs().max(gd.abs().max(bd).abs()) as u16
-//Diff0: Rgb([92, 77, 40]) Rgb([92, 77, 50])
-//0.max(0.max(10))
\ No newline at end of file
+}
\ No newline at end of file