diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..de18d3f --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,3 @@ +[build] +rustflags = ["-Clink-arg=-fuse-ld=lld"] +# rustflags = ["-Clink-arg=-fuse-ld=lld", "-Zthreads=8"] diff --git a/.gitignore b/.gitignore index 3ca43ae..193d30e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,8 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb + + +# Added by cargo + +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..6190f13 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +# cargo-features = ["codegen-backend"] +[package] +name = "pallete-tool" +version = "0.1.0" +edition = "2021" + +[dependencies] +clap = { version = "4.5.16", features = ["derive"] } +image = "0.25.2" + +[profile.dev] +debug = 0 +# codegen-backend = "cranelift" diff --git a/Palettes/GBND.md b/Palettes/GBND.md new file mode 100644 index 0000000..9b4a131 --- /dev/null +++ b/Palettes/GBND.md @@ -0,0 +1,13 @@ +
+ ##9D9998 +
+ ##7F6564 +
+ ##9FA4A8 +
+ ##81818D +
+ ##4A2A1B +
+ ##96969C +
\ No newline at end of file diff --git a/Palettes/Images/GRBND.png b/Palettes/Images/GRBND.png new file mode 100644 index 0000000..89fed7f Binary files /dev/null and b/Palettes/Images/GRBND.png differ diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..6e9071d --- /dev/null +++ b/src/main.rs @@ -0,0 +1,76 @@ +use clap::{Parser, ValueEnum}; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; + +use image::{GenericImageView, Pixel}; +use std::collections::HashSet; +use std::path::Path; + +/// CLI for extracting a color palette from an image and exporting it. +#[derive(Parser, Debug)] +#[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, + + /// 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, +} + +/// Supported export formats for the color palette +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] +enum PaletteFormat { + HTML, + Md, +} + +fn main() { + let cli = Cli::parse(); + let export_path = match cli.export { + Some(e) => e, + None => cli.image.as_path().with_extension("html"), + }; + + // Display parsed arguments + println!("[INFO] Image Path: {:?}", &cli.image); + println!("[INFO] Export Path: {:?}", &export_path); + println!("[INFO] Export Format: {:?}", &cli.format); + + let image_path = cli.image; + + // Open the image + let img = image::open(&Path::new(&image_path)).expect("Failed to open image"); + + // Create a HashSet to store unique colors + let mut colors = HashSet::new(); + + // 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(); + // Insert the color into the HashSet + colors.insert((rgba[0], rgba[1], rgba[2])); + } + + 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"); + + for v in vec { + write!(out,"
\n #{} \n
",v,v).expect("Can't write color in the output"); + } +} diff --git a/test.html b/test.html new file mode 100644 index 0000000..d2de661 --- /dev/null +++ b/test.html @@ -0,0 +1,13 @@ +
+ ##9D9998 +
+ ##81818D +
+ ##4A2A1B +
+ ##96969C +
+ ##9FA4A8 +
+ ##7F6564 +
\ No newline at end of file