From 97eb0922e40a224b214e4fcbd1561b97baf05de6 Mon Sep 17 00:00:00 2001 From: dandaedre Date: Wed, 28 Aug 2024 20:53:15 +0200 Subject: [PATCH] Sorting and better appearance --- Cargo.toml | 1 + Palettes/GBND.md | 13 --------- Palettes/{Images => }/GRBND.png | Bin src/main.rs | 47 +++++++++++++++++++++++++++++--- test.html | 13 --------- 5 files changed, 44 insertions(+), 30 deletions(-) delete mode 100644 Palettes/GBND.md rename Palettes/{Images => }/GRBND.png (100%) delete mode 100644 test.html diff --git a/Cargo.toml b/Cargo.toml index 6190f13..32e24aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] clap = { version = "4.5.16", features = ["derive"] } image = "0.25.2" +palette = "0.7.6" [profile.dev] debug = 0 diff --git a/Palettes/GBND.md b/Palettes/GBND.md deleted file mode 100644 index 9b4a131..0000000 --- a/Palettes/GBND.md +++ /dev/null @@ -1,13 +0,0 @@ -
- ##9D9998 -
- ##7F6564 -
- ##9FA4A8 -
- ##81818D -
- ##4A2A1B -
- ##96969C -
\ No newline at end of file diff --git a/Palettes/Images/GRBND.png b/Palettes/GRBND.png similarity index 100% rename from Palettes/Images/GRBND.png rename to Palettes/GRBND.png diff --git a/src/main.rs b/src/main.rs index 6e9071d..fb784be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use clap::{Parser, ValueEnum}; +use palette::{FromColor, Hsl, Srgb}; use std::fs::File; use std::io::Write; use std::path::PathBuf; @@ -54,23 +55,61 @@ fn main() { // Iterate over the pixels for pixel in img.pixels() { // Get the color as a tuple (R, G, B, A) - let rgba = pixel.2.to_rgb(); + let rgb = pixel.2.to_rgb(); + let srgb = Srgb::new( + rgb[0] as f64 / 255., + rgb[1] as f64 / 255., + rgb[2] as f64 / 255., + ); + let hsl = Hsl::from_color(srgb); // Insert the color into the HashSet - colors.insert((rgba[0], rgba[1], rgba[2])); + colors.insert(( + rgb[0], + rgb[1], + rgb[2], + (hsl.hue.into_positive_degrees() * 100.) as u32, + )); } let mut vec = Vec::with_capacity(colors.len() + 1); // Print the unique colors for color in &colors { println!("Color: {:?}", color); + vec.push(format!("#{:02X}{:02X}{:02X}", color.0, color.1, color.2)); } println!("Found {} unique colors", colors.len()); - let mut out = File::create(export_path).expect("Can't open output file"); + let mut out = File::create(&export_path).expect("Can't open output file"); + write!( + out, + r#""# + ).unwrap(); + write!(out, "{:?}", &export_path.file_name()).unwrap(); + write!( + out, + r#"
"# + ) + .unwrap(); for v in vec { - write!(out,"
\n #{} \n
",v,v).expect("Can't write color in the output"); + write!(out,"
\n {} \n
",v,v).expect("Can't write color in the output"); } + write!(out, "").unwrap(); } diff --git a/test.html b/test.html deleted file mode 100644 index d2de661..0000000 --- a/test.html +++ /dev/null @@ -1,13 +0,0 @@ -
- ##9D9998 -
- ##81818D -
- ##4A2A1B -
- ##96969C -
- ##9FA4A8 -
- ##7F6564 -
\ No newline at end of file