From 2731e601238a4028a270f9051d4cf06362caac22 Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 03:33:21 +0100 Subject: [PATCH] Fill: Quick Mask colour fills in Grayscale, CMYK and Lab take the grey profile's value (#17487) A colour filled into the Quick Mask of an 8- or 16-bit Grayscale, CMYK or Lab document now goes through the sGray conversion the Grayscale swatch already uses, so (200,30,30) writes 104 and (0,255,0) 219, as the reference application does. RGB documents, 32-bit documents, other modes and alpha channels keep the RGB weights. Co-Authored-By: Claude Opus 5.5 --- crates/engine-core/src/cmd/color_cmds.rs | 24 +++-- crates/engine-paint/src/cmd/fill_cmds.rs | 30 +++++- .../src/fill_quick_mask_mode_gray_tests.rs | 94 +++++++++++++++++++ 3 files changed, 137 insertions(+), 11 deletions(-) create mode 100644 crates/engine-paint/src/fill_quick_mask_mode_gray_tests.rs diff --git a/crates/engine-core/src/cmd/color_cmds.rs b/crates/engine-core/src/cmd/color_cmds.rs index c709bb6d432..30ad6e0fd54 100644 --- a/crates/engine-core/src/cmd/color_cmds.rs +++ b/crates/engine-core/src/cmd/color_cmds.rs @@ -843,24 +843,28 @@ pub fn document_profile(doc: &Document) -> Arc { /// document tagged with a gray profile other than that one. pub fn swatch_in_doc(s: &Session, c: [f32; 4]) -> [f32; 4] { let Some(st) = s.active().filter(|st| st.doc.mode == ColorMode::Grayscale) else { return c }; - let default_gray = Builtin::DotGain20Compat; - if s.color().settings.working_gray != default_gray.id() || document_profile(&st.doc).content_hash() != default_gray.profile().content_hash() { + if document_profile(&st.doc).content_hash() != Builtin::DotGain20Compat.profile().content_hash() { return c; } + swatch_gray(s, c).unwrap_or(c) +} + +/// A swatch colour's gray with the sRGB tone curve (sGray), the conversion [`swatch_in_doc`] makes in a Grayscale +/// document, as equal R, G and B. None outside the default Working Gray (Dot Gain 20 %) or when the conversion fails. +pub fn swatch_gray(s: &Session, c: [f32; 4]) -> Option<[f32; 4]> { + if s.color().settings.working_gray != Builtin::DotGain20Compat.id() { + return None; + } let opts = craft_cms::TransformOptions { intent: Intent::RelativeColorimetric, bpc: true, ..Default::default() }; let sgray = Arc::new(Builtin::SGray.profile().clone()); - let Ok(t) = craft_cms::cached(&working_profile(ColorMode::Rgb), &sgray, opts) else { return c }; + let t = craft_cms::cached(&working_profile(ColorMode::Rgb), &sgray, opts).ok()?; let mut out = [0.0f32; 16]; - if t.eval(&[c[0], c[1], c[2]], &mut out).is_err() { - return c; - } + t.eval(&[c[0], c[1], c[2]], &mut out).ok()?; let g = out[0]; - if g.is_finite() { + g.is_finite().then(|| { let g = g.clamp(0.0, 1.0); [g, g, g, c[3]] - } else { - c - } + }) } /// Profile describing the compositor's RGB output for this document (see the module docs). diff --git a/crates/engine-paint/src/cmd/fill_cmds.rs b/crates/engine-paint/src/cmd/fill_cmds.rs index 7968d1cc76d..3fbc872337e 100644 --- a/crates/engine-paint/src/cmd/fill_cmds.rs +++ b/crates/engine-paint/src/cmd/fill_cmds.rs @@ -6,7 +6,7 @@ //! Every parameter is validated: a bad one is an error, never a panic. use photocraft_algo::content_aware::FillOptions; -use photocraft_color::BlendMode; +use photocraft_color::{BlendMode, ColorMode, SampleType}; use photocraft_geom::Rect; #[cfg(test)] use photocraft_geom::TILE_SIZE; @@ -142,6 +142,15 @@ pub fn fill(s: &mut Session, p: &Value) -> Result { } _ => return Err(bad(format!("unknown contents `{contents}`"))), }; + // A colour filled into the Quick Mask of a Grayscale, CMYK or Lab document becomes its grey through the grey + // profile, as the reference writes it: (200,30,30) gives 104, not the RGB weights' 81 (#17487). + let source = match source { + Source::Color(c) | Source::Cmyk { rgba: c, .. } if matches!(contents, "foreground" | "background" | "color") => match quick_mask_gray(s, p, c) { + Some(g) => Source::Color(g), + None => source, + }, + source => source, + }; if area.is_empty() { return Ok(out); } @@ -158,6 +167,21 @@ pub fn fill(s: &mut Session, p: &Value) -> Result { Ok(out) } +/// The grey a colour takes in the Quick Mask of an 8- or 16-bit Grayscale, CMYK or Lab document (the Grayscale +/// document's own swatch grey). None elsewhere: RGB documents keep the RGB weights, and 32-bit documents and the +/// other modes, which were not measured, keep them too. +fn quick_mask_gray(s: &Session, p: &Value, c: [f32; 4]) -> Option<[f32; 4]> { + let doc = &s.active()?.doc; + if crate::channel_cmds::target_of(p) != crate::channel_cmds::Target::QuickMask || doc.depth == SampleType::F32 { + return None; + } + match doc.mode { + ColorMode::Grayscale => Some(crate::color_cmds::swatch_in_doc(s, c)), + ColorMode::Cmyk | ColorMode::Lab => crate::color_cmds::swatch_gray(s, c), + _ => None, + } +} + /// A fill colour: the inks it was picked as when it was picked as CMYK (#4196), else plain sRGB. fn color_source(s: &Session, c: [f32; 4], role: usize) -> Source { match s.tools.picked_role_cmyk(role) { @@ -172,6 +196,10 @@ mod tests; #[cfg(test)] mod mode_by_document_tests; +#[cfg(test)] +#[path = "../fill_quick_mask_mode_gray_tests.rs"] +mod fill_quick_mask_mode_gray_tests; + #[cfg(test)] #[path = "fill_dissolve_opacity_rounding_tests.rs"] mod fill_dissolve_opacity_rounding_tests; diff --git a/crates/engine-paint/src/fill_quick_mask_mode_gray_tests.rs b/crates/engine-paint/src/fill_quick_mask_mode_gray_tests.rs new file mode 100644 index 00000000000..c7c301dd9d7 --- /dev/null +++ b/crates/engine-paint/src/fill_quick_mask_mode_gray_tests.rs @@ -0,0 +1,94 @@ +//! #17487: a colour filled into the Quick Mask of a Grayscale, CMYK or Lab document becomes its grey through the grey +//! profile, as the reference writes it: (200,30,30) gives 104 and (0,255,0) 219 (Lab 220), not the RGB weights' 81 +//! and 150. RGB documents keep 81 / 150; alpha channels and pixel layers keep what they wrote before. + +use serde_json::{Value, json}; + +use crate::Session; + +const RED: [f32; 4] = [200.0 / 255.0, 30.0 / 255.0, 30.0 / 255.0, 1.0]; + +/// A white 40×20 document of `mode` at `depth` bits, in Quick Mask mode, with (200,30,30) as both swatches. +fn session(mode: &str, depth: u32) -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 40, "height": 20, "mode": mode, "depth": depth, "background": "white"})).unwrap(); + s.tools.foreground = RED; + s.tools.background = RED; + s.execute("select.editInQuickMaskMode", json!({"on": true})).unwrap(); + s +} + +fn quick_mask_level(s: &Session) -> i32 { + let q = s.active().unwrap().doc.quick_mask.as_ref().expect("in Quick Mask mode").surface.sample_channel(30, 10, 0); + (q * 255.0).round() as i32 +} + +/// The Quick Mask level after running `cmd` with `params` in a fresh document. +fn filled(mode: &str, depth: u32, cmd: &str, params: Value) -> i32 { + let mut s = session(mode, depth); + s.execute(cmd, params).unwrap(); + quick_mask_level(&s) +} + +fn foreground() -> Value { + json!({"contents": "foreground"}) +} + +fn green() -> Value { + json!({"contents": "color", "color": "#00ff00"}) +} + +#[test] +fn gray_cmyk_and_lab_quick_masks_take_the_grey_profile_value() { + for (mode, want_green) in [("grayscale", 219), ("cmyk", 219), ("lab", 220)] { + let red = filled(mode, 8, "edit.fill", foreground()); + assert!((red - 104).abs() <= 1, "{mode}/8 Foreground (200,30,30): {red}, reference 104"); + let green = filled(mode, 8, "edit.fill", green()); + assert!((green - want_green).abs() <= 1, "{mode}/8 Color (0,255,0): {green}, reference {want_green}"); + } +} + +#[test] +fn rgb_quick_masks_keep_the_rgb_weights() { + for depth in [8, 16, 32] { + let red = filled("rgb", depth, "edit.fill", foreground()); + let green = filled("rgb", depth, "edit.fill", green()); + assert_eq!((red, green), (81, 150), "RGB/{depth}"); + } +} + +#[test] +fn background_color_and_the_fill_keys_give_the_same_grey() { + for mode in ["grayscale", "cmyk", "lab"] { + let want = filled(mode, 8, "edit.fill", foreground()); + for (cmd, params) in [ + ("edit.fill", json!({"contents": "background"})), + ("edit.fill", json!({"contents": "color", "color": "#c81e1e"})), + ("edit.fillForeground", json!({})), + ("edit.fillBackground", json!({})), + ] { + let got = filled(mode, 8, cmd, params.clone()); + assert!((got - want).abs() <= 1, "{mode} {cmd} {params}: {got}, Foreground gives {want}"); + } + } +} + +#[test] +fn black_and_white_stay_exact_in_the_quick_mask() { + for mode in ["grayscale", "cmyk", "lab"] { + assert_eq!(filled(mode, 8, "edit.fill", json!({"contents": "color", "color": "#000000"})), 0, "{mode} black"); + assert_eq!(filled(mode, 8, "edit.fill", json!({"contents": "color", "color": "#ffffff"})), 255, "{mode} white"); + } +} + +#[test] +fn an_alpha_channel_keeps_the_rgb_weights() { + // Only the Quick Mask was measured; a targeted alpha channel keeps what it wrote before. + let mut s = crate::session(); + s.execute("file.new", json!({"width": 40, "height": 20, "mode": "cmyk", "depth": 8, "background": "white"})).unwrap(); + s.tools.foreground = RED; + s.execute("channel.new", json!({"fill": "white"})).unwrap(); + s.execute("edit.fill", foreground()).unwrap(); + let a = s.active().unwrap().doc.channels[0].surface.sample_channel(30, 10, 0); + assert_eq!((a * 255.0).round() as i32, 81); +}