diff --git a/crates/engine-core/src/cmd/color_cmds.rs b/crates/engine-core/src/cmd/color_cmds.rs index e49c12d4d3..95acb5e203 100644 --- a/crates/engine-core/src/cmd/color_cmds.rs +++ b/crates/engine-core/src/cmd/color_cmds.rs @@ -1033,10 +1033,10 @@ fn convert_color(c: &mut Color, from: ColorMode, to: ColorMode, t: &Transform) { c.c = nc; } -fn convert_fill(f: &mut Fill, cc: &mut dyn FnMut(&mut Color)) { +fn convert_fill(f: &mut Fill, from: ColorMode, cc: &mut dyn FnMut(&mut Color)) { match f { Fill::Solid(c) => cc(c), - Fill::Gradient { stops, opacity_stops, interpolation, .. } => convert_gradient(stops, opacity_stops, interpolation.as_deref_mut(), cc), + Fill::Gradient { stops, opacity_stops, interpolation, .. } => convert_gradient(stops, opacity_stops, interpolation.as_deref_mut(), from, cc), Fill::Pattern { .. } => {} } } @@ -1046,10 +1046,15 @@ fn convert_fill(f: &mut Fill, cc: &mut dyn FnMut(&mut Color)) { /// converted gradient, so a save still writes it: converting the dense baked stops one by one /// does not give exactly the converted gradient baked. Otherwise (no kept gradient, or stops /// edited since) both are converted as they are. +/// +/// `from` is the document's old mode, the only one `cc` converts. Kept stops in another model +/// (Lab stops of a method other than Classic, which bake into stops in `from`) are first brought +/// into `from` as baking reads them, so the rendered stops baked again are converted too. fn convert_gradient( stops: &mut Vec<(f32, Color)>, opacity: &[(f32, f32)], source: Option<&mut photocraft_doc::effects::GradientInterpolation>, + from: ColorMode, cc: &mut dyn FnMut(&mut Color), ) { let Some(source) = source else { @@ -1057,6 +1062,9 @@ fn convert_gradient( return; }; let baked = photocraft_io::gradient_bake::interpolation_matches(source, stops, opacity); + if baked && stops.iter().any(|s| s.1.mode == from) { + source.stops.iter_mut().filter(|s| s.1.mode != from).for_each(|s| s.1 = s.1.in_mode(from)); + } source.stops.iter_mut().for_each(|s| cc(&mut s.1)); if baked { *stops = photocraft_io::gradient_bake::bake_interpolation(source); @@ -1066,8 +1074,8 @@ fn convert_gradient( } /// Applies `cc` to every colour value of `l` (fill, text, shape and effect colours), recursing -/// into groups. -fn convert_layer_colors(l: &mut Layer, cc: &mut dyn FnMut(&mut Color)) { +/// into groups. `from` is the mode whose colours `cc` converts. +fn convert_layer_colors(l: &mut Layer, from: ColorMode, cc: &mut dyn FnMut(&mut Color)) { let mut touched_fx = false; for e in &mut l.effects.items { touched_fx = true; @@ -1077,7 +1085,7 @@ fn convert_layer_colors(l: &mut Layer, cc: &mut dyn FnMut(&mut Color)) { Effect::Stroke(s) => paint_colors(&mut s.paint, cc), Effect::ColorOverlay { color, .. } => cc(color), Effect::GradientOverlay { gradient, interpolation, .. } => { - convert_gradient(&mut gradient.stops, &gradient.opacity_stops, interpolation.as_mut(), cc); + convert_gradient(&mut gradient.stops, &gradient.opacity_stops, interpolation.as_mut(), from, cc); } Effect::Satin(s) => cc(&mut s.color), Effect::BevelEmboss(b) => { @@ -1089,7 +1097,7 @@ fn convert_layer_colors(l: &mut Layer, cc: &mut dyn FnMut(&mut Color)) { } let _ = touched_fx; match &mut l.content { - LayerContent::Fill(f) => convert_fill(f, cc), + LayerContent::Fill(f) => convert_fill(f, from, cc), LayerContent::Text(tx) => { cc(&mut tx.color); for r in &mut tx.runs { @@ -1098,15 +1106,15 @@ fn convert_layer_colors(l: &mut Layer, cc: &mut dyn FnMut(&mut Color)) { } LayerContent::Shape(sh) => { if let Some(f) = &mut sh.fill { - convert_fill(f, cc); + convert_fill(f, from, cc); } if let Some(s) = &mut sh.stroke { - convert_fill(&mut s.paint, cc); + convert_fill(&mut s.paint, from, cc); } } LayerContent::Group(g) => { for c in &mut g.children { - convert_layer_colors(c, cc); + convert_layer_colors(c, from, cc); } } _ => {} @@ -1160,7 +1168,7 @@ pub fn convert_document(doc: &mut Document, dst: &Profile, intent: Intent, bpc: *surf = convert_surface(surf, from_mode, f, &t); }); for l in &mut doc.layers { - convert_layer_colors(l, &mut |c| convert_color(c, from_mode, to_mode, &t)); + convert_layer_colors(l, from_mode, &mut |c| convert_color(c, from_mode, to_mode, &t)); // Preserved PSD blocks describing colours in the old mode no longer apply. } convert_style_colors(&mut doc.text_styles, &mut |c| convert_color(c, from_mode, to_mode, &t)); @@ -1183,7 +1191,7 @@ fn ink_duotone(doc: &mut Document) { let Some(d) = doc.duotone.take_if(|d| !d.inks.is_empty()) else { return }; crate::image_cmds::for_each_surface(&mut doc.layers, false, &mut |surf, _| *surf = ink_surface(surf, &d)); for l in &mut doc.layers { - convert_layer_colors(l, &mut |c| { + convert_layer_colors(l, ColorMode::Duotone, &mut |c| { if matches!(c.mode, ColorMode::Grayscale | ColorMode::Duotone) { let [r, g, b] = d.render(c.c[0]); *c = Color::rgba(r, g, b, c.alpha); diff --git a/crates/engine-core/src/cmd/color_cmds/profile_dither.rs b/crates/engine-core/src/cmd/color_cmds/profile_dither.rs index 82874d27fe..6868532c55 100644 --- a/crates/engine-core/src/cmd/color_cmds/profile_dither.rs +++ b/crates/engine-core/src/cmd/color_cmds/profile_dither.rs @@ -195,7 +195,7 @@ pub(super) fn convert(doc: &mut Document, dst: &Profile, intent: Intent, bpc: bo }); result?; for layer in &mut doc.layers { - convert_layer_colors(layer, &mut |c| convert_color(c, from, to_mode, &t)); + convert_layer_colors(layer, from, &mut |c| convert_color(c, from, to_mode, &t)); } convert_style_colors(&mut doc.text_styles, &mut |c| convert_color(c, from, to_mode, &t)); doc.mode = to_mode; diff --git a/crates/engine-core/src/cmd/convert_gradient_tests.rs b/crates/engine-core/src/cmd/convert_gradient_tests.rs index 64e6dc0236..12cb9a5988 100644 --- a/crates/engine-core/src/cmd/convert_gradient_tests.rs +++ b/crates/engine-core/src/cmd/convert_gradient_tests.rs @@ -62,3 +62,20 @@ fn converting_keeps_stops_edited_since_the_gradient_was_kept() { assert_eq!(stops.len(), 2, "the edited stops are converted, not replaced by the kept gradient"); assert!(stops[0].1.c[..3].iter().all(|v| *v < 0.01) && stops[1].1.c[..3].iter().all(|v| *v > 0.99), "{stops:?}"); } + +/// CodeRabbit on #18551: Lab stops kept with a method other than Classic bake into RGB render +/// stops in an RGB document; converting the profile converts those render stops like every +/// other colour, and they are still the kept gradient baked. +#[test] +fn converting_converts_render_stops_baked_from_lab_stops() { + let lab = |c: Color| c.in_mode(ColorMode::Lab); + let mut src = GradientInterpolation::new("perceptual", 0.0, vec![(0.0, lab(Color::rgba(0.9, 0.1, 0.1, 1.0))), (1.0, lab(Color::rgba(0.1, 0.2, 0.9, 1.0)))]); + src.midpoints = vec![0.3]; + let mut d = document(&src); + let before = bake_interpolation(&src); + assert!(before.iter().all(|(_, c)| c.mode == ColorMode::Rgb)); + convert_document(&mut d, Builtin::DisplayP3.profile(), Intent::RelativeColorimetric, false).unwrap(); + assert_eq!(kept(&d), (true, true), "the rendered stops are the converted gradient baked"); + let LayerContent::Fill(Fill::Gradient { stops, .. }) = &d.layers.last().unwrap().content else { panic!() }; + assert_ne!(*stops, before, "the render stops were converted"); +}