diff --git a/crates/engine-core/src/cmd/color_cmds.rs b/crates/engine-core/src/cmd/color_cmds.rs index f78d32f82be..b3897b21840 100644 --- a/crates/engine-core/src/cmd/color_cmds.rs +++ b/crates/engine-core/src/cmd/color_cmds.rs @@ -850,19 +850,30 @@ pub fn swatch_in_doc(s: &Session, c: [f32; 4]) -> [f32; 4] { if s.color().settings.working_gray != default_gray.id() || document_profile(&st.doc).content_hash() != default_gray.profile().content_hash() { return c; } - let opts = craft_cms::TransformOptions { intent: Intent::RelativeColorimetric, bpc: true, ..Default::default() }; - let Ok(t) = craft_cms::cached(&working_profile(ColorMode::Rgb), &Arc::new(target.profile().clone()), opts) else { return c }; - let mut out = [0.0f32; 16]; - if t.eval(&[c[0], c[1], c[2]], &mut out).is_err() { - return c; + swatch_to_gray(target, 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_sgray(s: &Session, c: [f32; 4]) -> Option<[f32; 4]> { + if s.color().settings.working_gray != Builtin::DotGain20Compat.id() { + return None; } + swatch_to_gray(Builtin::SGray, c) +} + +/// A swatch colour converted to the gray profile `target` (relative colorimetric with black point compensation), as +/// equal R, G and B; None when the conversion fails. +fn swatch_to_gray(target: Builtin, c: [f32; 4]) -> Option<[f32; 4]> { + let opts = craft_cms::TransformOptions { intent: Intent::RelativeColorimetric, bpc: true, ..Default::default() }; + let t = craft_cms::cached(&working_profile(ColorMode::Rgb), &Arc::new(target.profile().clone()), opts).ok()?; + let mut out = [0.0f32; 16]; + 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 - } + }) } /// The gray [`swatch_in_doc`] converts swatches to: sGray in Grayscale, gamma 2.2 in 8-bit Monotone. diff --git a/crates/engine-paint/src/cmd/fill_cmds.rs b/crates/engine-paint/src/cmd/fill_cmds.rs index bd5228cd88e..f37f02453a1 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, ColorMode}; +use photocraft_color::{BlendMode, ColorMode, SampleType}; use photocraft_geom::Rect; #[cfg(test)] use photocraft_geom::TILE_SIZE; @@ -177,6 +177,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); } @@ -249,6 +258,21 @@ fn check_quick_mask(contents: &str, mode: BlendMode, preserve: bool) -> Result<( Ok(()) } +/// 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_sgray(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) { @@ -281,6 +305,10 @@ mod grayscale_color_tests; #[cfg(test)] mod duotone_grey_value_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); +}