This repository has been archived on 2026-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
pallete-tool/src/main.rs

116 lines
3.7 KiB
Rust

use clap::{Parser, ValueEnum};
use palette::{FromColor, Hsl, Srgb};
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<PathBuf>,
/// Format in which to export the palette (e.g., JSON, XML)
#[arg(short, long, value_enum, value_name = "FORMAT")]
format: Option<PaletteFormat>,
}
/// 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 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((
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");
write!(
out,
r#"<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>"#
).unwrap();
write!(out, "{:?}", &export_path.file_name()).unwrap();
write!(
out,
r#"</title> <style> body{{background-color: #332f35;
margin: 0; /* Remove default margin */
padding: 0; /* Remove default padding */
display: flex; /* Center content */
justify-content: center; /* Center horizontally */
align-items: center; /* Center vertically */
height: 100vh; /* Make sure body takes up the full viewport height */
}}
.custom-div {{ display: flex;
flex-wrap: wrap;
flex-grow: 0;
padding: 5px;
background: #27232a;
text-align: center;
margin: 10px 0 20px;
}} </style></head><body> <div class="custom-div"> "#
)
.unwrap();
for v in vec {
write!(out," <div class='color' style='background:{}; color: #ffffff; min-width: 25%; height: 75px; line-height: 75px; font-size: 14px'> \n {} \n</div>",v,v).expect("Can't write color in the output");
}
write!(out, "</body></html>").unwrap();
}