use super::*;
#[derive(Debug)]
pub struct Lch;
pub type Spec = palette::Lch;
pub type Hist = Histo<Spec>;
pub const DARKEST: f32 = 4.5;
pub const LIGHTEST: f32 = 95.5;
pub const MIN_CHROMA: f32 = 10.0;
impl ColorTrait for Spec {}
impl Difference for Spec {
fn col_diff(&self, a: &Self, threshold: u8) -> bool {
use palette::color_difference::ImprovedCiede2000;
self.improved_difference(*a) <= f32::from(threshold)
}
}
impl BuildHisto<Spec> for Lch {
fn filter_cols(histo: Vec<Spec>) -> Vec<Spec> {
let lights = histo.iter().map(|c| c.l).collect::<Vec<_>>();
let darkest = lights.iter().fold(f32::INFINITY, |a, &b| a.min(b)).max(DARKEST);
let lightest = lights.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b)).min(LIGHTEST);
let chromas = histo.iter().map(|c| c.chroma).collect::<Vec<_>>();
let origch = util::avg(&chromas);
let lessch = chromas.iter().fold(f32::INFINITY, |a, &b| a.min(b));
let ch = if origch <= MIN_CHROMA { lessch } else { origch / 2.5 };
let filt = |x: &Spec| (x.l >= darkest && x.l <= lightest) && x.chroma >= ch;
histo.into_iter().filter(filt).collect()
}
fn sort_col(histo: Vec<Hist>, cs: &ColorOrder) -> Vec<Hist> {
let mut histo = histo;
use std::cmp::Ordering;
histo.sort_by(|a, b| match cs {
ColorOrder::LightFirst => (b.color.l, a.color.chroma).partial_cmp(&(a.color.l, b.color.chroma)).unwrap_or(Ordering::Equal),
ColorOrder::DarkFirst => (a.color.l, b.color.chroma).partial_cmp(&(b.color.l, a.color.chroma)).unwrap_or(Ordering::Equal),
});
histo
}
fn sort_by_key_fn(a: Hist) -> impl Ord {
a.color.chroma as i32
}
}