From 296beeac7bb725f463b847ebea9e2c4df283c407 Mon Sep 17 00:00:00 2001 From: Devon Sawatsky Date: Mon, 27 Sep 2021 19:35:21 -0700 Subject: clarify and improve sort --- src/main.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index eda43c9..c421f48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ fn main() { let before = Instant::now(); let filename = args().nth(1).unwrap(); let outname = args().nth(2).unwrap(); - // The percent of RGB value difference a color has to surpass to be considere unique + // The percent of RGB value difference a color has to surpass to be considered unique let tolerance = 0.6; let rgb_tolerance = 10.0 * tolerance; let max_colors = 256; @@ -25,6 +25,7 @@ fn main() { let mut colors: HashMap, usize> = HashMap::new(); + //count pixels for pixel in image.pixels() { match colors.get_mut(pixel) { None => { @@ -41,7 +42,13 @@ fn main() { ); let mut sorted: Vec<(Rgb, usize)> = colors.into_iter().collect(); - sorted.sort_by(|a, b| a.1.cmp(&b.1).then(a.0 .0.cmp(&b.0 .0)).reverse()); + sorted.sort_by(|(colour1, freq1), (colour2, freq2)| { + freq2 + .cmp(freq1) + .then(colour2[0].cmp(&colour1[0])) + .then(colour2[1].cmp(&colour1[1])) + .then(colour2[2].cmp(&colour1[2])) + }); println!("Sorted! Selecting colors..."); -- cgit 1.4.1-3-g733a5