From 4901407eaca38d045b7d36ec3a9d4337849dfe4c Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 06:17:22 -0800 Subject: [PATCH] Brushes: saturation jitter keeps a swatch link when the saturation stays A copy whose saturation jitter would move a color past full saturation (or past the largest Lab a and b) left the color as it was but still dropped its swatch link, so a spot or global color lost its link in those copies. Such a copy now keeps the color and its link, as brightness jitter does for a color already at black or at full brightness. --- crates/brush/src/colorize.rs | 8 +++++--- crates/brush/src/tests.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/crates/brush/src/colorize.rs b/crates/brush/src/colorize.rs index 7af2baed28..b48c43294c 100644 --- a/crates/brush/src/colorize.rs +++ b/crates/brush/src/colorize.rs @@ -212,7 +212,8 @@ const LAB_AB_MAX: f64 = 127.0; /// so its hue and brightness stay: RGB in HSB of its values, CMYK over C, M and Y with K kept, Lab /// by moving its chroma (the length of a and b) by `points` with its lightness and hue angle kept. /// The result stays between no saturation and full. `None` for a color without a hue (white, -/// black and grays, which have no saturation to move). +/// black and grays, which have no saturation to move) and for one whose saturation stays as it is +/// (already at the end it would move past), so such a color keeps its swatch link. fn saturated(c: &Color, points: f32) -> Option { if !points.is_finite() || vectorcraft_color::recolor::Neutral::of(c).is_some() { return None; @@ -220,7 +221,8 @@ fn saturated(c: &Color, points: f32) -> Option { let by = points / 100.0; let move_by = |v: [f32; 3]| { let [h, s, b] = vectorcraft_color::rgb_to_hsb(v); - (s > 0.0).then(|| vectorcraft_color::hsb_to_rgb([h, (s + by).clamp(0.0, 1.0), b])) + let to = (s + by).clamp(0.0, 1.0); + (s > 0.0 && to != s).then(|| vectorcraft_color::hsb_to_rgb([h, to, b])) }; match *c { Color::Rgb { r, g, b } => move_by([r, g, b]).map(|[r, g, b]| Color::rgb(r, g, b)), @@ -236,7 +238,7 @@ fn saturated(c: &Color, points: f32) -> Option { // already reaches past that does not grow. let fit = (LAB_AB_MAX / a.abs().max(b.abs())).max(1.0); let scale = ((chroma + f64::from(points)).max(0.0) / chroma).min(fit); - Some(Color::lab(l, (a * scale) as f32, (b * scale) as f32)) + (scale != 1.0).then(|| Color::lab(l, (a * scale) as f32, (b * scale) as f32)) } } } diff --git a/crates/brush/src/tests.rs b/crates/brush/src/tests.rs index 21022b1ab1..d23580cdea 100644 --- a/crates/brush/src/tests.rs +++ b/crates/brush/src/tests.rs @@ -1566,6 +1566,39 @@ fn brightness_jitter_moves_after_the_colorization_in_each_model() { assert!(lab.iter().all(|c| matches!(*c, Color::Lab { l, a, b } if a == 40.0 && b == 30.0 && (l - 50.0).abs() <= 20.001)), "{lab:?}"); } +/// A swatch-linked color already at the end a copy's saturation or brightness moves toward stays as +/// it is and keeps its link; the copies that move it lose the link. +#[test] +fn jitter_at_a_limit_keeps_the_swatch_link() { + let doc = Document::new(1.0, 1.0); + let path = line(0.0, 0.0, 200.0, 0.0); + // Full red (full saturation and brightness), full magenta and yellow ink, and a Lab red whose a + // is at its largest: saturation that grows and brightness that grows leave them as they are. + for (color, saturation_jitter, brightness_jitter) in [ + (Color::rgb(1.0, 0.0, 0.0), 50.0, 0.0), + (Color::cmyk(0.0, 1.0, 1.0, 0.0), 50.0, 0.0), + (Color::lab(50.0, 127.0, 0.0), 50.0, 0.0), + (Color::rgb(1.0, 0.0, 0.0), 0.0, 50.0), + (Color::lab(100.0, 20.0, 10.0), 0.0, 50.0), + ] { + let linked = Paint::Solid { color, swatch: Some("Spot".into()), tint: 1.0 }; + let mut art = rect_art(10.0, 4.0); + art.appearance.set_fill(linked.clone()); + let mut s = Scatter { art, saturation_jitter, ..Default::default() }; + s.brightness_jitter = brightness_jitter; + let out = stroke_pieces(&doc, &Brush { name: "s".into(), kind: BrushKind::Scatter(s) }, &path, &stroke(1.0)); + let (mut kept, mut moved) = (0, 0); + for n in &out { + match n.appearance.fill_paint() { + p if p == linked => kept += 1, + Paint::Solid { color: c, swatch: None, .. } if c != color => moved += 1, + p => panic!("{color:?}: {p:?}"), + } + } + assert!(kept > 0 && moved > 0, "{color:?}: {kept} kept, {moved} moved"); + } +} + /// An instance whose fill has an opacity of 0 does not stain its symbol's art, so the art keeps its /// swatch links. #[test]