Works enough
This commit is contained in:
parent
97eb0922e4
commit
e3fb4884b9
Binary file not shown.
|
After Width: | Height: | Size: 4.0 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 8.3 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 8.3 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 4.5 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 3.5 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 3.5 KiB |
188
src/main.rs
188
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<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>,
|
||||
// /// 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,
|
||||
// /// 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<RichColor>,
|
||||
}
|
||||
|
||||
impl ColorIndex {
|
||||
fn new(name: String, colors: Vec<RichColor>) -> 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<ColorIndex>) {
|
||||
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,
|
||||
"Palette: {}",
|
||||
&export_path
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.split(".")
|
||||
.nth(0)
|
||||
.unwrap()
|
||||
)
|
||||
.unwrap();
|
||||
write!(
|
||||
out,
|
||||
r#"</title> <style> body{{background-color: #332f35;
|
||||
margin: 0;
|
||||
font-family: "Open Sans","Segoe UI",Tahoma,sans-serif;
|
||||
padding: 0;
|
||||
display: grid;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
}}
|
||||
.custom-div {{ display: flex;
|
||||
flex-wrap: wrap;
|
||||
flex-grow: 0;
|
||||
padding: 5px;
|
||||
background: #27232a;
|
||||
text-align: center;
|
||||
margin: 10px 0 20px;
|
||||
}}
|
||||
h1 {{
|
||||
text-transform: uppercase;
|
||||
color: #eaeaea;
|
||||
}}
|
||||
</style></head><body>
|
||||
"#
|
||||
)
|
||||
.unwrap();
|
||||
for blob in color_vec {
|
||||
write!(out, "<h1>{}</h1>", blob.name).unwrap();
|
||||
write!(out, "<div class='custom-div'>").unwrap();
|
||||
for v in blob.colors {
|
||||
let c = if v.luma < 60 { "FFFFFF" } else { "000000" };
|
||||
write!(out," <div class='color' style='background:{}; color: #{}; min-width: 25%; height: 75px; line-height: 75px; font-size: 14px'> \n {} \n</div>",v.rgb_string(),c,v.rgb_string()).expect("Can't write color in the output");
|
||||
}
|
||||
write!(out, "</div>").unwrap();
|
||||
}
|
||||
write!(out, "</body></html>").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#"<!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"> "#
|
||||
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," <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();
|
||||
}
|
||||
|
|
|
|||
Reference in New Issue