From da0c334bb4842b678fde743cb16ea59b167574bb Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 1 Dec 2026 04:32:45 +0100 Subject: [PATCH] Batch fix-up: translucent stop colours under Linear or Perceptual save as drawn (#13855, CodeRabbit on #18551) Without opacity stops of its own, a kept gradient's colour alpha was written as opacity stops under the kept method; Linear and Perceptual bake those on the next read (0.25 came back near 0.14). The record now counts as matching only when the method and Smoothness give that alpha back, so other gradients are saved as their render stops. The save test covers Classic, Linear and Perceptual. Co-Authored-By: Claude Opus 5.5 --- crates/io/src/blocks/tests.rs | 23 +++++++++++++---------- crates/io/src/gradient_bake.rs | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/crates/io/src/blocks/tests.rs b/crates/io/src/blocks/tests.rs index 8031c644b4..c9489bf516 100644 --- a/crates/io/src/blocks/tests.rs +++ b/crates/io/src/blocks/tests.rs @@ -544,15 +544,18 @@ fn oversized_tysh_preserves_fallback_geometry_without_decoding_text() { fn gradient_fill_keeps_translucent_stop_colours_on_save() { use photocraft_compose::gradient_fill::Ramp; use photocraft_doc::effects::GradientInterpolation; - let src = GradientInterpolation::new("classic", 100.0, vec![(0.0, Color::rgba(1.0, 0.0, 0.0, 0.25)), (1.0, Color::rgba(0.0, 0.0, 1.0, 0.25))]); - let mut f = Fill::gradient(crate::gradient_bake::bake_interpolation(&src), 90.0, 1.0, GradientStyle::Linear, false); - if let Fill::Gradient { interpolation, .. } = &mut f { - *interpolation = Some(Box::new(src)); - } - let back = rewritten(&f); - let (a, b) = (Ramp::new(&f).unwrap(), Ramp::new(&back).unwrap()); - for k in 0..=10 { - let t = k as f32 / 10.0; - assert!((a.sample(t)[3] - b.sample(t)[3]).abs() < 1e-3, "at {t}: {:?} before, {:?} after the save", a.sample(t), b.sample(t)); + // Linear and Perceptual bake opacity stops differently from the colours' alpha, so they need the render stops. + for method in ["classic", "linear", "perceptual"] { + let src = GradientInterpolation::new(method, 100.0, vec![(0.0, Color::rgba(1.0, 0.0, 0.0, 0.25)), (1.0, Color::rgba(0.0, 0.0, 1.0, 0.25))]); + let mut f = Fill::gradient(crate::gradient_bake::bake_interpolation(&src), 90.0, 1.0, GradientStyle::Linear, false); + if let Fill::Gradient { interpolation, .. } = &mut f { + *interpolation = Some(Box::new(src)); + } + let back = rewritten(&f); + let (a, b) = (Ramp::new(&f).unwrap(), Ramp::new(&back).unwrap()); + for k in 0..=10 { + let t = k as f32 / 10.0; + assert!((a.sample(t)[3] - b.sample(t)[3]).abs() < 1e-3, "{method} at {t}: {:?} before, {:?} after the save", a.sample(t), b.sample(t)); + } } } diff --git a/crates/io/src/gradient_bake.rs b/crates/io/src/gradient_bake.rs index 2a9226dcef..42fa5d5e17 100644 --- a/crates/io/src/gradient_bake.rs +++ b/crates/io/src/gradient_bake.rs @@ -239,6 +239,31 @@ pub fn interpolation_matches(src: &photocraft_doc::effects::GradientInterpolatio && interpolation_method(src).is_some() && bake_interpolation(src) == stops && bake_interpolation_opacity(src).map_or(opaque(opacity), |o| o == opacity || (opaque(&o) && opaque(opacity))) + && colour_alpha_round_trips(src) +} + +/// Whether the alpha of `src`'s stop colours survives a save under the kept record: without opacity stops of its own, +/// a writer stores that alpha as opacity stops, which the next read bakes with the record's method and Smoothness. +/// Linear and Perceptual bend it (0.25 comes back near 0.14), so such a gradient is saved as its render stops instead. +fn colour_alpha_round_trips(src: &photocraft_doc::effects::GradientInterpolation) -> bool { + if !src.opacity_stops.is_empty() || src.stops.iter().all(|(_, c)| c.alpha >= 1.0 - 1e-4) { + return true; + } + let mut alphas: Vec<(f32, f32)> = src.stops.iter().map(|(t, c)| (unit(*t), unit(c.alpha))).collect(); + alphas.sort_by(|a, b| a.0.total_cmp(&b.0)); + let smooth = if src.smoothness.is_finite() { src.smoothness.clamp(0.0, 100.0) / 100.0 } else { 0.0 }; + let baked = bake_opacity(alphas.clone(), &[], smooth, interpolation_method(src).unwrap_or(Method::Classic)); + // The alpha written, read back linearly between the stops. + let linear = |t: f32| -> f32 { + let i = alphas.partition_point(|a| a.0 < t); + match (i.checked_sub(1).and_then(|k| alphas.get(k)), alphas.get(i)) { + (Some(a), Some(b)) if b.0 > a.0 => a.1 + (b.1 - a.1) * (t - a.0) / (b.0 - a.0), + (_, Some(b)) => b.1, + (Some(a), None) => a.1, + (None, None) => 1.0, + } + }; + baked.iter().all(|(t, o)| (o - linear(*t)).abs() < 1e-3) } /// Dense linear stops reproducing Photoshop's interpolation. Returns the input unchanged when it