Compare commits
6 Commits
68313eaba9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 4edaa2ba03 | |||
| 00f059108a | |||
| 9286255662 | |||
| e3895898ae | |||
| 14093f7a61 | |||
| 9f722b1040 |
127
Cargo.lock
generated
127
Cargo.lock
generated
@@ -35,6 +35,56 @@ dependencies = [
|
|||||||
"equator",
|
"equator",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstream"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
|
||||||
|
dependencies = [
|
||||||
|
"anstyle",
|
||||||
|
"anstyle-parse",
|
||||||
|
"anstyle-query",
|
||||||
|
"anstyle-wincon",
|
||||||
|
"colorchoice",
|
||||||
|
"is_terminal_polyfill",
|
||||||
|
"utf8parse",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle"
|
||||||
|
version = "1.0.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-parse"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
|
||||||
|
dependencies = [
|
||||||
|
"utf8parse",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-query"
|
||||||
|
version = "1.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
|
||||||
|
dependencies = [
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-wincon"
|
||||||
|
version = "3.0.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
|
||||||
|
dependencies = [
|
||||||
|
"anstyle",
|
||||||
|
"once_cell_polyfill",
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anyhow"
|
name = "anyhow"
|
||||||
version = "1.0.102"
|
version = "1.0.102"
|
||||||
@@ -196,12 +246,58 @@ dependencies = [
|
|||||||
"rand_core 0.10.1",
|
"rand_core 0.10.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap"
|
||||||
|
version = "4.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51"
|
||||||
|
dependencies = [
|
||||||
|
"clap_builder",
|
||||||
|
"clap_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_builder"
|
||||||
|
version = "4.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f"
|
||||||
|
dependencies = [
|
||||||
|
"anstream",
|
||||||
|
"anstyle",
|
||||||
|
"clap_lex",
|
||||||
|
"strsim",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_derive"
|
||||||
|
version = "4.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9"
|
||||||
|
dependencies = [
|
||||||
|
"heck",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_lex"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "color_quant"
|
name = "color_quant"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
|
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "colorchoice"
|
||||||
|
version = "1.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cpufeatures"
|
name = "cpufeatures"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
@@ -387,6 +483,12 @@ dependencies = [
|
|||||||
"zerocopy",
|
"zerocopy",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "heck"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hermit-abi"
|
name = "hermit-abi"
|
||||||
version = "0.5.2"
|
version = "0.5.2"
|
||||||
@@ -437,6 +539,7 @@ dependencies = [
|
|||||||
name = "imagepicker"
|
name = "imagepicker"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"clap",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
"image",
|
"image",
|
||||||
"log",
|
"log",
|
||||||
@@ -472,6 +575,12 @@ dependencies = [
|
|||||||
"windows-sys",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "is_terminal_polyfill"
|
||||||
|
version = "1.70.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itertools"
|
name = "itertools"
|
||||||
version = "0.14.0"
|
version = "0.14.0"
|
||||||
@@ -650,6 +759,12 @@ version = "1.21.4"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "once_cell_polyfill"
|
||||||
|
version = "1.70.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "paste"
|
name = "paste"
|
||||||
version = "1.0.15"
|
version = "1.0.15"
|
||||||
@@ -954,6 +1069,12 @@ version = "1.2.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
|
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strsim"
|
||||||
|
version = "0.11.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.118"
|
version = "2.0.118"
|
||||||
@@ -1014,6 +1135,12 @@ version = "1.0.24"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "utf8parse"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "v_frame"
|
name = "v_frame"
|
||||||
version = "0.3.9"
|
version = "0.3.9"
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = { version = "4.6.*", features = ["derive"] }
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
image = {version = "0.25.10", features = ["png"]}
|
image = {version = "0.25.10", features = ["png"]}
|
||||||
log = "0.4.29"
|
log = "0.4.29"
|
||||||
|
|||||||
74
src/cli.rs
Normal file
74
src/cli.rs
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
use std::fmt::Debug;
|
||||||
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
|
use clap::Parser;
|
||||||
|
use image::{DynamicImage, ImageReader, Rgb};
|
||||||
|
|
||||||
|
use crate::Colour;
|
||||||
|
|
||||||
|
#[derive(Parser)]
|
||||||
|
#[command(name = "imagePicker")]
|
||||||
|
#[command(version = "1.0")]
|
||||||
|
#[command(about = "Matches colours to their closest equivalents in a given image", long_about = None)]
|
||||||
|
/// Matches the colours in one image to either a list of provided colours or a different image.
|
||||||
|
///
|
||||||
|
/// For each provided colour (either explicitly or in the second image), finds the closest colour
|
||||||
|
/// from the first image, using redmean distance.
|
||||||
|
pub struct Cli {
|
||||||
|
/// The source image from which colours should be picked.
|
||||||
|
#[arg(value_parser = valid_image_file)]
|
||||||
|
pub image: DynamicImage,
|
||||||
|
|
||||||
|
/// Optional second image; if used, all colours from this image will be matched to their closest
|
||||||
|
/// equivalent in the first image.
|
||||||
|
#[arg(short = 'i', long = "image", value_parser = valid_image_file)]
|
||||||
|
pub second_image: Option<DynamicImage>,
|
||||||
|
|
||||||
|
/// Optional list of hex values; if used, will match each value to their closest equivalent in
|
||||||
|
/// the first image
|
||||||
|
///
|
||||||
|
/// Should provide valid hex codes only: '24274a', and '#cad3f5' are both valid.
|
||||||
|
#[arg(short = 'c', long = "colours", value_parser = valid_hex_code)]
|
||||||
|
pub colours: Option<Vec<Colour>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for Cli {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.debug_struct("Cli")
|
||||||
|
.field("image", &self.image)
|
||||||
|
.field("image_two", &self.second_image)
|
||||||
|
.field("colours", &self.colours)
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validates that a given string represents a hex code for a RGB colour.
|
||||||
|
///
|
||||||
|
/// Optionally accepts a leading '#'.
|
||||||
|
fn valid_hex_code(s: &str) -> Result<Colour, String> {
|
||||||
|
// accept leading '#' for hex values
|
||||||
|
let start = match s.chars().nth(0) == Some('#') {
|
||||||
|
true => 1,
|
||||||
|
false => 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
let u8s: Vec<u8> = (start..s.len())
|
||||||
|
.step_by(2)
|
||||||
|
.map(|i| u8::from_str_radix(&s[i..i + 2], 16))
|
||||||
|
.collect::<Result<Vec<u8>, ParseIntError>>()
|
||||||
|
.map_err(|e| format!("{}", e))?;
|
||||||
|
let rgb: [u8; 3] = u8s
|
||||||
|
.try_into()
|
||||||
|
.map_err(|_| <&str as Into<String>>::into("Hex string must decode to exactly 3 bytes"))?;
|
||||||
|
Ok(Rgb(rgb))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validates that agiven string points to a valid image file.
|
||||||
|
fn valid_image_file(s: &str) -> Result<DynamicImage, String> {
|
||||||
|
ImageReader::open(s)
|
||||||
|
.map_err(|e| format!("{}", e))?
|
||||||
|
.with_guessed_format()
|
||||||
|
.map_err(|e| format!("{}", e))?
|
||||||
|
.decode()
|
||||||
|
.map_err(|e| format!("{}", e))
|
||||||
|
}
|
||||||
141
src/colours.rs
Normal file
141
src/colours.rs
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
|
use image::{DynamicImage, GenericImageView, Rgb};
|
||||||
|
use log::info;
|
||||||
|
use std::fmt::Debug;
|
||||||
|
|
||||||
|
use crate::encode_hex;
|
||||||
|
|
||||||
|
/// This type is used to efficiently store all colour values from an image.
|
||||||
|
/// By using nested hashmaps we can save on space for colours with identical r and g values.
|
||||||
|
/// Additionally stores a Vec of points to remember which colour maps to which pixel(s).
|
||||||
|
pub type ColourMap = HashMap<u8, HashMap<u8, HashMap<u8, Vec<Point>>>>;
|
||||||
|
|
||||||
|
/// We use one colour type.
|
||||||
|
pub type Colour = Rgb<u8>;
|
||||||
|
|
||||||
|
/// Struct to represent a point in an image.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Point {
|
||||||
|
x: u32,
|
||||||
|
y: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for Point {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.write_str(&format!("{}, {}", &self.x, &self.y))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Struct to save found matches for colours.
|
||||||
|
/// includes the matched colour, and a list of pixels with that colour.
|
||||||
|
pub struct Match {
|
||||||
|
/// matched colour
|
||||||
|
pub colour: Colour,
|
||||||
|
/// list of pixels with the matching colour
|
||||||
|
pub positions: Vec<Point>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract all colours from a provided image into a nested ColourMap
|
||||||
|
pub fn extract_colours(img: DynamicImage) -> ColourMap {
|
||||||
|
let mut out: ColourMap = HashMap::new();
|
||||||
|
for (x, y, p) in img.pixels() {
|
||||||
|
out.entry(p[0])
|
||||||
|
.or_insert_with(HashMap::new)
|
||||||
|
.entry(p[1])
|
||||||
|
.or_insert_with(HashMap::new)
|
||||||
|
.entry(p[2])
|
||||||
|
.or_insert_with(Vec::new)
|
||||||
|
.push(Point { x, y });
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract all colours in a provided image into a flat hashmap.
|
||||||
|
fn extract_colours_set(img: DynamicImage) -> HashSet<Rgb<u8>> {
|
||||||
|
let mut out = HashSet::new();
|
||||||
|
img.pixels().for_each(|(_x, _y, p)| {
|
||||||
|
out.insert(p);
|
||||||
|
});
|
||||||
|
out.into_iter().map(|c| Rgb([c[0], c[1], c[2]])).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finds the "closest" match to a colour in the provided map. Uses redmean distance.
|
||||||
|
///
|
||||||
|
/// arguments:
|
||||||
|
/// colour: Colour which will be matched against all.
|
||||||
|
/// all: Map of Colours to match against.
|
||||||
|
fn closest_colour(colour: &Colour, all: &ColourMap) -> Option<Match> {
|
||||||
|
// use redmean to calculate the distance between colours
|
||||||
|
let mut closest: Option<Match> = None;
|
||||||
|
let mut dist: f32 = f32::MAX;
|
||||||
|
|
||||||
|
let (colour_r, colour_g, colour_b) = (colour[0] as f32, colour[1] as f32, colour[2] as f32);
|
||||||
|
for (r, gbs) in all.iter() {
|
||||||
|
// iterate over all reds
|
||||||
|
let r_bar = 0.5 * (*r as f32 + colour_r);
|
||||||
|
let r_squared = (*r as f32 - colour_r) * (*r as f32 - colour_r);
|
||||||
|
for (g, bs) in gbs.iter() {
|
||||||
|
// iterate over all greens
|
||||||
|
let g_squared = (*g as f32 - colour_g) * (*g as f32 - colour_g);
|
||||||
|
for (b, vec) in bs.iter() {
|
||||||
|
// iterate over all blues
|
||||||
|
let b_squared = (*b as f32 - colour_b) * (*b as f32 - colour_b);
|
||||||
|
let delta = (2.0 + r_bar / 256.0) * r_squared
|
||||||
|
+ 4.0 * g_squared
|
||||||
|
+ (2.0 + (255.0 - r_bar) / 256.0) * b_squared;
|
||||||
|
if delta < dist {
|
||||||
|
// update closest if better match found
|
||||||
|
dist = delta;
|
||||||
|
closest = Some(Match {
|
||||||
|
colour: Rgb([*r, *g, *b]),
|
||||||
|
positions: vec.to_vec(), // FIXME (low priority): would be more efficient if
|
||||||
|
// i can make these a borrow instead, but that
|
||||||
|
// leads to lifetime issues
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return closest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Maps a list of colours to the closest equivalents in the provided map.
|
||||||
|
///
|
||||||
|
/// arguments:
|
||||||
|
/// all_cols: Map of colours match against.
|
||||||
|
/// cols: Vec of Colours which will be matched against all_cols.
|
||||||
|
///
|
||||||
|
/// returns: Hashmap of colour, match pairs.
|
||||||
|
pub fn map_image_list(all_cols: &ColourMap, cols: Vec<Colour>) -> HashMap<Colour, Match> {
|
||||||
|
let mut output_cols: HashMap<Colour, Match> = HashMap::new();
|
||||||
|
|
||||||
|
for c in cols {
|
||||||
|
info!("Matching colour {}", encode_hex(&c));
|
||||||
|
if let Some(col) = closest_colour(&c, &all_cols) {
|
||||||
|
output_cols.insert(c, col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output_cols
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Maps the colours of one image to the closest equivalents in the provided map.
|
||||||
|
///
|
||||||
|
/// arguments:
|
||||||
|
/// img_1_cols: Map of colours to match against.
|
||||||
|
/// img_2: DynamicImage for which each colour will be matched against img_1_cols.
|
||||||
|
///
|
||||||
|
/// returns: Hashmap of colour, match pairs.
|
||||||
|
pub fn map_image_image(img_1_cols: &ColourMap, img_2: DynamicImage) -> HashMap<Colour, Match> {
|
||||||
|
let img_2_cols = extract_colours_set(img_2);
|
||||||
|
|
||||||
|
let mut output_cols: HashMap<Colour, Match> = HashMap::new();
|
||||||
|
img_2_cols.iter().for_each(|c| {
|
||||||
|
info!("Matching colour {}", encode_hex(&c));
|
||||||
|
if let Some(col) = closest_colour(c, &img_1_cols) {
|
||||||
|
output_cols.insert(*c, col);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
output_cols
|
||||||
|
}
|
||||||
81
src/main.rs
81
src/main.rs
@@ -1,67 +1,46 @@
|
|||||||
use std::{collections::{HashMap, HashSet}, env, num::ParseIntError};
|
mod cli;
|
||||||
|
mod colours;
|
||||||
|
|
||||||
|
use clap::Parser;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use image::{DynamicImage, ImageReader, Rgb};
|
|
||||||
|
|
||||||
type ColourSet = HashMap<u8, HashMap<u8, HashSet<u8>>>;
|
use crate::cli::Cli;
|
||||||
type Colour = Rgb<u8>;
|
use crate::colours::{Colour, Match, extract_colours, map_image_image, map_image_list};
|
||||||
|
|
||||||
pub fn decode_hex(s: &str) -> Result<Colour, ParseIntError> {
|
/// Encodes a RGB colour as a hex code.
|
||||||
Ok(Rgb((0..s.len())
|
fn encode_hex(c: &Colour) -> String {
|
||||||
.step_by(2)
|
format!("#{:02x}{:02x}{:02x}", c[0], c[1], c[2])
|
||||||
.map(|i| u8::from_str_radix(&s[i..i + 2], 16))
|
}
|
||||||
.collect())
|
|
||||||
|
/// Formats found matches for CLI otuput.
|
||||||
|
fn pretty_print(h: HashMap<Colour, Match>) {
|
||||||
|
h.iter().for_each(|(k, v)| {
|
||||||
|
println!(
|
||||||
|
"{}: {} ({:?})",
|
||||||
|
encode_hex(k),
|
||||||
|
encode_hex(&v.colour),
|
||||||
|
v.positions
|
||||||
)
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extract_colours(img: DynamicImage) -> ColourSet {
|
/// Driver code.
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn closest_colour(colour: Colour, all: &ColourSet) -> Option<Colour> {
|
|
||||||
// use redmean to calculate the distance between colours
|
|
||||||
// inevitably O(len(all))
|
|
||||||
let mut closest: Option<Colour> = None;
|
|
||||||
let mut dist: f32 = f32::MAX;
|
|
||||||
for (r, gbs) in all.iter() {
|
|
||||||
let r_bar = 0.5 * (r + colour[0]) as f32;
|
|
||||||
let delta_r = ((r + colour[0]) * (r + colour[0])) as f32;
|
|
||||||
for (g, bs) in gbs.iter() {
|
|
||||||
let delta_g = ((g + colour[1]) * (g + colour[1])) as f32;
|
|
||||||
for b in bs {
|
|
||||||
let delta_b = ((b + colour[2]) * (b + colour[2])) as f32;
|
|
||||||
|
|
||||||
let delta = (2.0 + r_bar/256.0) * delta_r + 4.0 * delta_g + (2.0 + (255.0 - r_bar)/256.0) * delta_b;
|
|
||||||
if delta < dist {
|
|
||||||
dist = delta;
|
|
||||||
closest = Some(Rgb([*r,*g,*b]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return closest
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
|
let cli = Cli::parse();
|
||||||
|
|
||||||
let args: Vec<String> = env::args().collect();
|
let set = extract_colours(cli.image);
|
||||||
if args.len() > 1 {
|
|
||||||
let img_file = &args[1];
|
|
||||||
let img = ImageReader::open(img_file).unwrap().decode().unwrap(); // FIXME: unwraps
|
|
||||||
|
|
||||||
let all_cols = extract_colours(img);
|
if let Some(cs) = cli.colours {
|
||||||
let mut output_cols: HashMap<Colour, Colour> = HashMap::new();
|
let colours = map_image_list(&set, cs);
|
||||||
|
pretty_print(colours);
|
||||||
for c_str in args {
|
|
||||||
// TODO: parse args
|
|
||||||
let c = decode_hex(&c_str).unwrap(); // FIXME: unwraps;
|
|
||||||
if let Some(col) = closest_colour(c, &all_cols) {
|
|
||||||
output_cols.insert(c, col);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(i) = cli.second_image {
|
||||||
|
let colours = map_image_image(&set, i);
|
||||||
|
pretty_print(colours);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user