diff --git a/crates/brush/src/colorize.rs b/crates/brush/src/colorize.rs index b48c43294c..47211426f6 100644 --- a/crates/brush/src/colorize.rs +++ b/crates/brush/src/colorize.rs @@ -267,10 +267,13 @@ fn turned(c: &Color, degrees: f32) -> Option { } } -/// Replace a solid paint's color with what `adjust` gives for it, dropping its swatch link. +/// Replace a solid paint's color with what `adjust` gives for it, dropping its swatch link. A +/// color that comes back unchanged (an amount too small to move its components) keeps the paint +/// and its link. fn recolor_paint(p: &mut Paint, adjust: &dyn Fn(&Color) -> Option) { if let Paint::Solid { color, .. } = p && let Some(c) = adjust(color) + && c != *color { *p = Paint::solid(c); } diff --git a/crates/brush/src/tests.rs b/crates/brush/src/tests.rs index 0b6cbf6011..2a39d50010 100644 --- a/crates/brush/src/tests.rs +++ b/crates/brush/src/tests.rs @@ -1599,6 +1599,30 @@ fn jitter_at_a_limit_keeps_the_swatch_link() { } } +/// A hue or saturation jitter too small to move a Lab color's components gives each copy the color +/// it had, and the copies keep the swatch link. +#[test] +fn jitter_too_small_to_change_a_color_keeps_the_swatch_link() { + let linked = Paint::Solid { color: Color::lab(50.0, 64.0, 0.0), swatch: Some("Spot".into()), tint: 1.0 }; + // 1e-50 degrees turns a and b by less than an f32 holds; 1e-6 points scales the chroma by + // 1 + 1.6e-8, which rounds back to the same f32 components. + type Jitter = fn(&mut [Node], f64, u64); + let jitters: [(&str, Jitter, f64); 2] = [("hue", crate::colorize::jitter_hues, 1e-50), ("saturation", crate::colorize::jitter_saturations, 1e-6)]; + for (name, jitter, amount) in jitters { + let mut copies: Vec = (0..8) + .map(|_| { + let mut art = rect_art(10.0, 4.0); + art.appearance.set_fill(linked.clone()); + art + }) + .collect(); + jitter(&mut copies, amount, 7); + for n in &copies { + assert_eq!(n.appearance.fill_paint(), linked, "{name}"); + } + } +} + /// An instance whose fill has an opacity of 0 does not stain its symbol's art, so the art keeps its /// swatch links. #[test]