diff --git a/Palettes/Beach.png b/Palettes/Beach.png new file mode 100644 index 0000000..7d40f5c Binary files /dev/null and b/Palettes/Beach.png differ diff --git a/Palettes/Bender.png b/Palettes/Bender.png new file mode 100644 index 0000000..2c5cbee Binary files /dev/null and b/Palettes/Bender.png differ diff --git a/Palettes/GRBND.png b/Palettes/GRBND.png deleted file mode 100644 index 89fed7f..0000000 Binary files a/Palettes/GRBND.png and /dev/null differ diff --git a/Palettes/Great Green.png b/Palettes/Great Green.png new file mode 100644 index 0000000..8189ed6 Binary files /dev/null and b/Palettes/Great Green.png differ diff --git a/Palettes/Lochre.png b/Palettes/Lochre.png new file mode 100644 index 0000000..34cdd8a Binary files /dev/null and b/Palettes/Lochre.png differ diff --git a/Palettes/Red Haze.png b/Palettes/Red Haze.png new file mode 100644 index 0000000..bdc0e65 Binary files /dev/null and b/Palettes/Red Haze.png differ diff --git a/src/main.rs b/src/main.rs index fb784be..4c8229b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use clap::{Parser, ValueEnum}; +use clap::Parser; use palette::{FromColor, Hsl, Srgb}; use std::fs::File; use std::io::Write; @@ -13,39 +13,149 @@ use std::path::Path; #[command(author, version, about, long_about = None)] struct Cli { /// Path of the image to extract the palette from - #[arg(short, long, value_name = "IMAGE_PATH")] - image: PathBuf, + #[arg(value_name = "IMAGE_PATH")] + image: Vec, /// Path to export the palette to #[arg(short, long, value_name = "EXPORT_PATH")] export: Option, - - /// Format in which to export the palette (e.g., JSON, XML) - #[arg(short, long, value_enum, value_name = "FORMAT")] - format: Option, + // /// Format in which to export the palette (e.g., JSON, XML) + // #[arg(short, long, value_enum, value_name = "FORMAT")] + // format: Option, } -/// Supported export formats for the color palette -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] -enum PaletteFormat { - HTML, - Md, +// /// Supported export formats for the color palette +// #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] +// enum PaletteFormat { +// HTML, +// Md, +// } + +/// A color index +struct ColorIndex { + name: String, + colors: Vec, +} + +impl ColorIndex { + fn new(name: String, colors: Vec) -> Self { + Self { name, colors } + } +} + +/// A single color, with many infos +struct RichColor { + #[allow(dead_code)] + hue: u32, + luma: u32, + rgb: (u8, u8, u8), +} + +impl RichColor { + fn rgb_string(&self) -> String { + format!("#{:02X}{:02X}{:02X}", self.rgb.0, self.rgb.1, self.rgb.2) + } +} + +impl RichColor { + fn new(hue: u32, luma: u32, rgb: (u8, u8, u8)) -> Self { + Self { hue, luma, rgb } + } } fn main() { let cli = Cli::parse(); let export_path = match cli.export { Some(e) => e, - None => cli.image.as_path().with_extension("html"), + None => cli.image[0] + .parent() + .expect("Couldn't find a place to save the output") + .to_path_buf(), }; // Display parsed arguments println!("[INFO] Image Path: {:?}", &cli.image); println!("[INFO] Export Path: {:?}", &export_path); - println!("[INFO] Export Format: {:?}", &cli.format); + // println!("[INFO] Export Format: {:?}", &cli.format); - let image_path = cli.image; + let mut colorblobs = Vec::with_capacity(cli.image.len()); + for image in cli.image { + colorblobs.push(extract_colors(&image)); + } + export(export_path, colorblobs); +} +fn export(export_path: PathBuf, color_vec: Vec) { + let mut out = File::create(&export_path).expect("Can't open output file"); + + write!( + out, + r#""# + ).unwrap(); + write!( + out, + "Palette: {}", + &export_path + .file_name() + .unwrap() + .to_str() + .unwrap() + .split(".") + .nth(0) + .unwrap() + ) + .unwrap(); + write!( + out, + r#" + "# + ) + .unwrap(); + for blob in color_vec { + write!(out, "

{}

", blob.name).unwrap(); + write!(out, "
").unwrap(); + for v in blob.colors { + let c = if v.luma < 60 { "FFFFFF" } else { "000000" }; + write!(out,"
\n {} \n
",v.rgb_string(),c,v.rgb_string()).expect("Can't write color in the output"); + } + write!(out, "
").unwrap(); + } + write!(out, "").unwrap(); +} + +fn extract_colors(image_path: &PathBuf) -> ColorIndex { + let name = format!( + "{}", + image_path + .file_name() + .unwrap() + .to_str() + .unwrap() + .split(".") + .nth(0) + .unwrap() + ); // Open the image let img = image::open(&Path::new(&image_path)).expect("Failed to open image"); @@ -68,48 +178,28 @@ fn main() { rgb[1], rgb[2], (hsl.hue.into_positive_degrees() * 100.) as u32, + (hsl.lightness * 100.) as u32, )); } let mut vec = Vec::with_capacity(colors.len() + 1); // Print the unique colors for color in &colors { - println!("Color: {:?}", color); + println!("[{}] {:?}", name, color); - vec.push(format!("#{:02X}{:02X}{:02X}", color.0, color.1, color.2)); + vec.push(( + color.3, // format!("#{:02X}{:02X}{:02X}", color.0, color.1, color.2), + (color.0, color.1, color.2), + color.4, + )); } - println!("Found {} unique colors", colors.len()); - - 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#"
"# + vec.sort_by_key(|&(k, _, _)| k); + println!("[{}] Found {} unique colors", name, colors.len()); + ColorIndex::new( + name, + vec.iter() + .map(|(hue, rgb, luma)| RichColor::new(*hue, *luma, rgb.to_owned())) + .collect(), ) - .unwrap(); - for v in vec { - write!(out,"
\n {} \n
",v,v).expect("Can't write color in the output"); - } - write!(out, "").unwrap(); }