diff --git a/crates/trace/src/logo/unmix.rs b/crates/trace/src/logo/unmix.rs index 576986e83c..01ef0ce2e5 100644 --- a/crates/trace/src/logo/unmix.rs +++ b/crates/trace/src/logo/unmix.rs @@ -508,17 +508,18 @@ fn between_classes(pix: &[Px], flat: &[Option], mixed: &HashMap<[u32; 4], } /// For each class with a flat area [`PURE`] or more from it (as when it is named some levels off its -/// color), the colors of its flat areas, each at least `PURE / 2` from the others: the flat areas that +/// color), the distinct colors of its flat areas: the flat areas that /// stand for it (`flat`) and those whose mix holds it most (`between`, [`between_classes`]). Empty for /// a class whose flat areas all come within `PURE` of it. fn area_colors(pix: &[Px], classes: &[[f64; 4]], flat: &[Option], between: &[[Option; 2]]) -> Vec> { let mut colors: Vec> = vec![Vec::new(); classes.len()]; let mut off_named = vec![false; classes.len()]; + let mut kept: HashSet<(usize, [u32; 4])> = HashSet::new(); for (i, px) in pix.iter().enumerate() { let Some(c) = flat.get(i).copied().flatten().or_else(|| between.get(i).and_then(|b| b[0])) else { continue }; let (Some(cv), Some(seen), Some(off)) = (classes.get(c), colors.get_mut(c), off_named.get_mut(c)) else { continue }; *off = *off || (0..4).map(|j| (f64::from(px[j]) - cv[j]).powi(2)).sum::().sqrt() >= PURE; - if !seen.iter().any(|q| distance(px, q) < PURE / 2.0) { + if kept.insert((c, px.map(f32::to_bits))) { seen.push(*px); } } @@ -1018,6 +1019,21 @@ mod tests { } } + #[test] + fn every_flat_area_color_keeps_its_whole_neighborhood() { + // A class named 228, 228, 227 and flat areas of 222 then 223: a pixel of 225 lies within + // `PURE` of 223 but not of 222, so it counts as art of the class only when 223 is kept. + let level = |v: f32| [v / 255.0, v / 255.0, v / 255.0, 1.0]; + let classes = vec![[228.0 / 255.0, 228.0 / 255.0, 227.0 / 255.0, 1.0], [0.0, 0.0, 0.0, 1.0]]; + let pix = vec![level(222.0), level(222.0), level(223.0), level(223.0)]; + let flat = vec![Some(0); pix.len()]; + let between = vec![[None; 2]; pix.len()]; + let colors = area_colors(&pix, &classes, &flat, &between); + assert_eq!(colors[0].len(), 2, "{:?}", colors[0]); + assert!(near_area_color(&level(225.0), &colors[0])); + assert!(!near_area_color(&level(225.0), &colors[0][..1])); + } + #[test] fn weights_sum_to_one() { let pix = vec![[0.2, 0.1, 0.5, 0.8], [0.9, 0.0, 0.05, 0.95]];