mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
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 <[email protected]>
This commit is contained in:
1 parent
ea9be47f11
commit
da0c334bb4
2 files changed
+38
-10
No files matched your search
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user