From 7efd224081a190521f6634e766df02d75de77ab7 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 7 Nov 2026 20:25:05 -0600 Subject: [PATCH 1/2] Color Replacement: in Lab, Color keeps L and takes the foreground's a and b (#11922) In a Lab document the Color mode no longer blends through RGB (which changed the pixel's L): it keeps the stored L and moves a and b to the foreground's, as in the reference application. Hue, Saturation and Luminosity keep today's RGB blend. --- crates/paint/src/replace.rs | 16 ++++++ crates/paint/src/replace_lab_tests.rs | 77 +++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 crates/paint/src/replace_lab_tests.rs diff --git a/crates/paint/src/replace.rs b/crates/paint/src/replace.rs index 20619cd48a..31bd4ab90b 100644 --- a/crates/paint/src/replace.rs +++ b/crates/paint/src/replace.rs @@ -96,6 +96,13 @@ pub fn color_replacement_effect(pre: Surface, rs: ReplaceSettings) -> impl FnMut ReplaceMode::Luminosity => BlendMode::Luminosity, }; let fg = [rs.color[0], rs.color[1], rs.color[2]]; + // In a Lab document Color mode keeps the pixel's L and takes the foreground's a and b, as in + // the reference application (#11922): the stored a, b channels move straight to its. + let lab_color = fmt.mode == photocraft_color::ColorMode::Lab && rs.mode == ReplaceMode::Color; + let fg_ab = { + let l = photocraft_color::convert::srgb_to_lab(fg); + [(l[1] + 128.0) / 255.0, (l[2] + 128.0) / 255.0] + }; let tol = rs.tolerance.clamp(0.0, 1.0); let soft = rs.anti_alias && rs.limits != Limits::FindEdges; let mut once: Option<[f32; 3]> = None; @@ -189,6 +196,11 @@ pub fn color_replacement_effect(pre: Surface, rs: ReplaceSettings) -> impl FnMut continue; } let px = work.px_mut(x, y); + if lab_color { + px[1] += (fg_ab[0] - px[1]) * k; + px[2] += (fg_ab[1] - px[2]) * k; + continue; + } let c = to_rgba(&fmt, px); let b = blend_rgb(mode, [c[0], c[1], c[2]], fg); let o = [c[0] + (b[0] - c[0]) * k, c[1] + (b[1] - c[1]) * k, c[2] + (b[2] - c[2]) * k, c[3]]; @@ -199,6 +211,10 @@ pub fn color_replacement_effect(pre: Surface, rs: ReplaceSettings) -> impl FnMut } } +#[cfg(test)] +#[path = "replace_lab_tests.rs"] +mod replace_lab_tests; + #[cfg(test)] mod tests { use super::*; diff --git a/crates/paint/src/replace_lab_tests.rs b/crates/paint/src/replace_lab_tests.rs new file mode 100644 index 0000000000..3402ca6a41 --- /dev/null +++ b/crates/paint/src/replace_lab_tests.rs @@ -0,0 +1,77 @@ +//! #11922: in a Lab document, Color mode keeps the pixel's L and takes the foreground's a and b. +//! The setup is the issue's: red, green and pink stripes and the darker block (200,40,40), a blue +//! foreground, a 40 px hard tip, one click on each. Hue, Saturation and Luminosity keep today's RGB +//! blend. + +use photocraft_color::{ColorMode, PixelFormat, SampleType}; + +use super::*; +use crate::{BrushSettings, StrokePoint}; + +const LAB8: PixelFormat = PixelFormat { mode: ColorMode::Lab, sample: SampleType::U8, alpha: true }; +const COLOURS: [[u8; 3]; 4] = [[220, 30, 30], [30, 180, 60], [240, 120, 120], [200, 40, 40]]; +const BLUE: [f32; 4] = [0.0, 0.0, 1.0, 1.0]; + +/// A Lab/8 surface with the four colours in 100 px columns. +fn target() -> Surface { + let mut s = Surface::new(LAB8); + for (i, c) in COLOURS.iter().enumerate() { + let rgba = [f32::from(c[0]) / 255.0, f32::from(c[1]) / 255.0, f32::from(c[2]) / 255.0, 1.0]; + let x0 = i as i32 * 100; + s.fill_rect(Rect::new(x0, 0, x0 + 100, 100), &photocraft_raster::from_rgba(&LAB8, rgba)); + } + s +} + +fn click(s: &mut Surface, mode: ReplaceMode, x: f64) { + let stroke = Stroke { + brush: BrushSettings { size: 40.0, hardness: 1.0, spacing: 0.25, pressure_size: false, ..Default::default() }, + points: vec![StrokePoint::new(x, 50.0, 1.0)], + }; + let rs = ReplaceSettings { mode, color: BLUE, ..Default::default() }; + apply_color_replacement(s, &stroke, &rs, None, false); +} + +/// The stored L (0..100) and a, b (-128..127) at a pixel. +fn lab(s: &Surface, x: i32, y: i32) -> [f32; 3] { + let px = s.pixel(x, y); + [px[0] * 100.0, px[1] * 255.0 - 128.0, px[2] * 255.0 - 128.0] +} + +#[test] +fn color_keeps_l_and_takes_the_foregrounds_a_and_b() { + let fg = photocraft_color::convert::srgb_to_lab([0.0, 0.0, 1.0]); + for i in 0..COLOURS.len() { + let mut s = target(); + let x = i as i32 * 100 + 50; + let before = lab(&s, x, 50); + click(&mut s, ReplaceMode::Color, f64::from(x) + 0.5); + let after = lab(&s, x, 50); + assert!((after[0] - before[0]).abs() <= 1.0, "colour {i}: L {} → {}", before[0], after[0]); + assert!((after[1] - fg[1]).abs() <= 2.0 && (after[2] - fg[2]).abs() <= 2.0, "colour {i}: a, b {:?} vs the foreground's {:?}", &after[1..], &fg[1..]); + // Outside the tip nothing changed. + assert_eq!(lab(&s, x - 40, 50), before, "colour {i} outside the tip"); + } +} + +#[test] +fn hue_saturation_and_luminosity_keep_todays_blend() { + for mode in [ReplaceMode::Hue, ReplaceMode::Saturation, ReplaceMode::Luminosity] { + let mut s = target(); + let pre = s.pixel(150, 50).to_vec(); + click(&mut s, mode, 150.5); + // Today's path: through RGB, blended, and back. + let blend = match mode { + ReplaceMode::Hue => BlendMode::Hue, + ReplaceMode::Saturation => BlendMode::Saturation, + _ => BlendMode::Luminosity, + }; + let c = to_rgba(&LAB8, &pre); + let b = blend_rgb(blend, [c[0], c[1], c[2]], [0.0, 0.0, 1.0]); + let want = photocraft_raster::from_rgba(&LAB8, [b[0], b[1], b[2], c[3]]); + let got = s.pixel(150, 50); + for (g, w) in got.iter().zip(&want) { + assert!((g - w).abs() <= 1.0 / 255.0 + 1e-6, "{mode:?}: {got:?} vs {want:?}"); + } + } +} From 1cf1e15da5684388e43a17ecbca5fe179f248c20 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 7 Nov 2026 21:41:31 -0600 Subject: [PATCH 2/2] Keep Lab/16 Color Replacement on the RGB blend (CodeRabbit review on crates/paint/src/replace.rs:101) Co-Authored-By: Claude Opus 5.5 --- crates/paint/src/replace.rs | 7 ++++--- crates/paint/src/replace_lab_tests.rs | 27 +++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/crates/paint/src/replace.rs b/crates/paint/src/replace.rs index 31bd4ab90b..c4157fac81 100644 --- a/crates/paint/src/replace.rs +++ b/crates/paint/src/replace.rs @@ -96,9 +96,10 @@ pub fn color_replacement_effect(pre: Surface, rs: ReplaceSettings) -> impl FnMut ReplaceMode::Luminosity => BlendMode::Luminosity, }; let fg = [rs.color[0], rs.color[1], rs.color[2]]; - // In a Lab document Color mode keeps the pixel's L and takes the foreground's a and b, as in - // the reference application (#11922): the stored a, b channels move straight to its. - let lab_color = fmt.mode == photocraft_color::ColorMode::Lab && rs.mode == ReplaceMode::Color; + // In a Lab/8 document Color mode keeps the pixel's L and takes the foreground's a and b, as in + // the reference application (#11922): the stored a, b channels move straight to its. Lab/16 + // wasn't measured and keeps the RGB blend. + let lab_color = fmt.mode == photocraft_color::ColorMode::Lab && fmt.sample == photocraft_color::SampleType::U8 && rs.mode == ReplaceMode::Color; let fg_ab = { let l = photocraft_color::convert::srgb_to_lab(fg); [(l[1] + 128.0) / 255.0, (l[2] + 128.0) / 255.0] diff --git a/crates/paint/src/replace_lab_tests.rs b/crates/paint/src/replace_lab_tests.rs index 3402ca6a41..caf024fb40 100644 --- a/crates/paint/src/replace_lab_tests.rs +++ b/crates/paint/src/replace_lab_tests.rs @@ -14,11 +14,15 @@ const BLUE: [f32; 4] = [0.0, 0.0, 1.0, 1.0]; /// A Lab/8 surface with the four colours in 100 px columns. fn target() -> Surface { - let mut s = Surface::new(LAB8); + target_in(LAB8) +} + +fn target_in(fmt: PixelFormat) -> Surface { + let mut s = Surface::new(fmt); for (i, c) in COLOURS.iter().enumerate() { let rgba = [f32::from(c[0]) / 255.0, f32::from(c[1]) / 255.0, f32::from(c[2]) / 255.0, 1.0]; let x0 = i as i32 * 100; - s.fill_rect(Rect::new(x0, 0, x0 + 100, 100), &photocraft_raster::from_rgba(&LAB8, rgba)); + s.fill_rect(Rect::new(x0, 0, x0 + 100, 100), &photocraft_raster::from_rgba(&fmt, rgba)); } s } @@ -75,3 +79,22 @@ fn hue_saturation_and_luminosity_keep_todays_blend() { } } } + +#[test] +fn lab_16_color_keeps_todays_blend() { + // Lab/16 wasn't measured: its Color mode still blends through RGB. + let lab16 = PixelFormat { sample: SampleType::U16, ..LAB8 }; + for i in 0..COLOURS.len() { + let mut s = target_in(lab16); + let x = i as i32 * 100 + 50; + let pre = s.pixel(x, 50).to_vec(); + click(&mut s, ReplaceMode::Color, f64::from(x) + 0.5); + let c = to_rgba(&lab16, &pre); + let b = blend_rgb(BlendMode::Color, [c[0], c[1], c[2]], [0.0, 0.0, 1.0]); + let want = photocraft_raster::from_rgba(&lab16, [b[0], b[1], b[2], c[3]]); + let got = s.pixel(x, 50); + for (g, w) in got.iter().zip(&want) { + assert!((g - w).abs() <= 1.0 / 65535.0 + 1e-6, "colour {i}: {got:?} vs {want:?}"); + } + } +}