Files
imagePicker/src/colours.rs
2026-06-25 16:06:35 +02:00

107 lines
3.3 KiB
Rust

use std::collections::{HashMap, HashSet};
use image::{DynamicImage, GenericImageView, Rgb};
use log::info;
use std::fmt::Debug;
use crate::encode_hex;
pub type ColourMap = HashMap<u8, HashMap<u8, HashMap<u8, Vec<Point>>>>;
pub type Colour = Rgb<u8>;
#[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))
}
}
pub struct Match {
pub colour: Colour,
pub positions: Vec<Point>,
}
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
}
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()
}
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() {
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() {
let g_squared = (*g as f32 - colour_g) * (*g as f32 - colour_g);
for (b, vec) in bs.iter() {
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 {
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;
}
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
}
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
}