First fast draft

This commit is contained in:
dandaedre 2024-08-28 19:16:50 +02:00
parent 532f45467e
commit 6a8c9c85be
7 changed files with 123 additions and 0 deletions

3
.cargo/config.toml Normal file
View File

@ -0,0 +1,3 @@
[build]
rustflags = ["-Clink-arg=-fuse-ld=lld"]
# rustflags = ["-Clink-arg=-fuse-ld=lld", "-Zthreads=8"]

5
.gitignore vendored
View File

@ -14,3 +14,8 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# Added by cargo
/target

13
Cargo.toml Normal file
View File

@ -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"

13
Palettes/GBND.md Normal file
View File

@ -0,0 +1,13 @@
<div class='color' style='background:##9D9998; color: #ffffff; min-width: 25%; height: 75px; line-height: 75px; font-size: 14px'>
##9D9998
</div> <div class='color' style='background:##7F6564; color: #ffffff; min-width: 25%; height: 75px; line-height: 75px; font-size: 14px'>
##7F6564
</div> <div class='color' style='background:##9FA4A8; color: #ffffff; min-width: 25%; height: 75px; line-height: 75px; font-size: 14px'>
##9FA4A8
</div> <div class='color' style='background:##81818D; color: #ffffff; min-width: 25%; height: 75px; line-height: 75px; font-size: 14px'>
##81818D
</div> <div class='color' style='background:##4A2A1B; color: #ffffff; min-width: 25%; height: 75px; line-height: 75px; font-size: 14px'>
##4A2A1B
</div> <div class='color' style='background:##96969C; color: #ffffff; min-width: 25%; height: 75px; line-height: 75px; font-size: 14px'>
##96969C
</div>

BIN
Palettes/Images/GRBND.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

76
src/main.rs Normal file
View File

@ -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<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 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," <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");
}
}

13
test.html Normal file
View File

@ -0,0 +1,13 @@
<div class='color' style='background:##9D9998; color: #ffffff; min-width: 25%; height: 75px; line-height: 75px; font-size: 14px'>
##9D9998
</div> <div class='color' style='background:##81818D; color: #ffffff; min-width: 25%; height: 75px; line-height: 75px; font-size: 14px'>
##81818D
</div> <div class='color' style='background:##4A2A1B; color: #ffffff; min-width: 25%; height: 75px; line-height: 75px; font-size: 14px'>
##4A2A1B
</div> <div class='color' style='background:##96969C; color: #ffffff; min-width: 25%; height: 75px; line-height: 75px; font-size: 14px'>
##96969C
</div> <div class='color' style='background:##9FA4A8; color: #ffffff; min-width: 25%; height: 75px; line-height: 75px; font-size: 14px'>
##9FA4A8
</div> <div class='color' style='background:##7F6564; color: #ffffff; min-width: 25%; height: 75px; line-height: 75px; font-size: 14px'>
##7F6564
</div>