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

107 lines
3.3 KiB
Rust

mod image_functions;
mod structs;
mod utilities;
use clap::Parser;
use image_functions::{extract_colors, extract_kcluster};
use indicatif::{HumanDuration, MultiProgress, ProgressBar, ProgressStyle};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use std::{
path::PathBuf,
time::{Duration, Instant},
};
use structs::{ColorIndex, ExtractMode};
use utilities::{export_html, small_name};
/// 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(value_name = "IMAGE_PATH")]
image: Vec<PathBuf>,
/// Path to export the palette to
#[arg(short, long, value_name = "EXPORT_PATH")]
export: Option<PathBuf>,
/// Type of extraction, full or k-mean-cluster
#[arg(short, long, value_enum, value_name = "MODE")]
mode: Option<ExtractMode>,
/// Max number of colors to extract
#[arg(short, long, value_enum, value_name = "NB_COLORS")]
nb_colors: Option<usize>,
/// Number of passes to process
#[arg(short, long, value_name = "NB_RUNS")]
runs: Option<u64>,
/// Extra Verbosity
#[arg(short, default_value_t = false)]
verbosity: bool,
}
fn main() {
let started = Instant::now();
let spinner_style = ProgressStyle::with_template(
"[{elapsed_precise}] {spinner} {bar:40.cyan/blue} {pos}/{len} [{prefix:.bold.dim}]",
)
.unwrap()
.tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈ ");
let cli = Cli::parse();
let export_path = match cli.export.clone() {
Some(e) => e,
None => cli.image[0]
// .parent()
// Check output here, not later
// .expect("Couldn't find a place to save the output")
.with_file_name("Palettes.html"),
// .to_path_buf(),
};
let mode = match cli.mode {
Some(f) => f,
None => ExtractMode::Full,
};
// Display parsed arguments
if cli.verbosity {
println!("[INFO] Image Path: {:?}", &cli.image);
println!("[INFO] Export Path: {:?}", &export_path);
// println!("[INFO] Export Format: {:?}", &cli.format);
}
let function = match mode {
ExtractMode::Full => extract_colors,
ExtractMode::Cluster => extract_kcluster,
};
let m = MultiProgress::new();
let pb_general = ProgressBar::new(cli.image.len() as u64)
.with_style(spinner_style.clone())
.with_prefix("Images processed");
m.add(pb_general.clone());
pb_general.enable_steady_tick(Duration::from_millis(100));
let colorblobs: Vec<_> = cli
.image
.par_iter()
// .progress_with(pb_general.clone())
.map(|image| {
let pb = m.add(ProgressBar::new(cli.runs.unwrap_or(8)));
pb.enable_steady_tick(Duration::from_millis(100));
pb.set_style(spinner_style.clone());
pb.set_prefix(format!("[{:?}]", small_name(image)));
let r = function(image, &cli, &pb);
pb_general.inc(1);
r
})
.collect::<Vec<ColorIndex>>();
pb_general.finish_and_clear();
if cli.verbosity {
println!("Done in {}", HumanDuration(started.elapsed()));
}
export_html(export_path, colorblobs);
}