From 6a8c9c85be82fd1f67e2bbf22b1c43e2bece7f3d Mon Sep 17 00:00:00 2001 From: dandaedre Date: Wed, 28 Aug 2024 19:16:50 +0200 Subject: [PATCH] First fast draft --- .cargo/config.toml | 3 ++ .gitignore | 5 +++ Cargo.toml | 13 +++++++ Palettes/GBND.md | 13 +++++++ Palettes/Images/GRBND.png | Bin 0 -> 8486 bytes src/main.rs | 76 ++++++++++++++++++++++++++++++++++++++ test.html | 13 +++++++ 7 files changed, 123 insertions(+) create mode 100644 .cargo/config.toml create mode 100644 Cargo.toml create mode 100644 Palettes/GBND.md create mode 100644 Palettes/Images/GRBND.png create mode 100644 src/main.rs create mode 100644 test.html 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 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J}L;#`{V=& zRf-M@OP=AUeYDu5tk}d_4UHC?~{bF literal 0 HcmV?d00001 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