pixie_anim_lib/simd/
fallback.rs

1//! Scalar fallbacks for performance-critical operations.
2
3use crate::quant::Rgb;
4
5use crate::color::Lab;
6use crate::simd::PlanarLabPalette;
7
8/// Find the index of the nearest color in Lab space using a planar palette.
9pub fn find_nearest_color_lab_planar(pixel: Lab, palette: &PlanarLabPalette) -> usize {
10    let mut min_dist = f32::MAX;
11    let mut best_idx = 0;
12
13    for i in 0..palette.len {
14        let dl = pixel.l - palette.l[i];
15        let da = pixel.a - palette.a[i];
16        let db = pixel.b - palette.b[i];
17        let dist = dl * dl + da * da + db * db;
18
19        if dist < min_dist {
20            min_dist = dist;
21            best_idx = i;
22        }
23    }
24
25    best_idx
26}
27
28/// Find the index of the nearest color in a palette using Euclidean distance.
29pub fn find_nearest_color(pixel: Rgb, palette: &[Rgb]) -> usize {
30    let mut min_dist = u32::MAX;
31    let mut best_idx = 0;
32
33    for (i, &color) in palette.iter().enumerate() {
34        let dr = pixel.r as i32 - color.r as i32;
35        let dg = pixel.g as i32 - color.g as i32;
36        let db = pixel.b as i32 - color.b as i32;
37        let dist = (dr * dr + dg * dg + db * db) as u32;
38
39        if dist < min_dist {
40            min_dist = dist;
41            best_idx = i;
42        }
43    }
44
45    best_idx
46}