From 57e7779ed85ea7cb249db1a94dbe9340fa6cae1e Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 00:56:57 +0100 Subject: [PATCH 1/3] Fill: Color and Foreground Color convert to the Grayscale document's grey (#17469) In a Grayscale document Edit > Fill with Color or Foreground Color wrote the colour's plain luma (blue gave 29). Both now go through swatch_in_doc as Background Color already did (blue gives 70; the reference measured 69). Picked CMYK inks are still matched on the colour as picked, and other modes are unchanged. The fill-layer test no longer expects a Grayscale Solid Color layer to equal Edit > Fill; the layer was not measured and keeps its luma grey. Co-Authored-By: Claude Opus 5.5 --- crates/engine-paint/src/cmd/fill_cmds.rs | 16 +++- .../cmd/fill_cmds/grayscale_color_tests.rs | 95 +++++++++++++++++++ .../tests/it/paint_in_fill_layer_mode.rs | 7 +- 3 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 crates/engine-paint/src/cmd/fill_cmds/grayscale_color_tests.rs diff --git a/crates/engine-paint/src/cmd/fill_cmds.rs b/crates/engine-paint/src/cmd/fill_cmds.rs index 7968d1cc76..cdc18942a3 100644 --- a/crates/engine-paint/src/cmd/fill_cmds.rs +++ b/crates/engine-paint/src/cmd/fill_cmds.rs @@ -14,6 +14,7 @@ use photocraft_geom::TILE_SIZE; use photocraft_raster::Surface; use serde_json::{Value, json}; +use crate::color_cmds::swatch_in_doc; use crate::commands::{cmyk_param, color_param, layer_param, parse_blend}; use crate::content_sampling::{content_aware, content_aware_options}; pub use crate::pixel_fill::greyed_modes; @@ -85,16 +86,18 @@ pub fn fill(s: &mut Session, p: &Value) -> Result { let mut out = json!({ "contents": contents }); let restore = contents == "history"; let source = match contents { - "foreground" => color_source(s, s.tools.foreground, 0), - // As the document takes the swatch: its gray profile's value in a Grayscale document (#11940). - "background" => color_source(s, crate::color_cmds::swatch_in_doc(s, s.tools.background), 1), + // As the document takes the swatch: its gray profile's value in a Grayscale document (#11940, #17469). + "foreground" => color_source(s, swatch_in_doc(s, s.tools.foreground), 0), + "background" => color_source(s, swatch_in_doc(s, s.tools.background), 1), "color" => { - let rgba = color_param(CMD, p, "color", s.tools.foreground)?; + let picked = color_param(CMD, p, "color", s.tools.foreground)?; + // The picked inks are matched on the colour as picked; only the written value is converted. + let rgba = swatch_in_doc(s, picked); match cmyk_param(CMD, p, "colorCmyk")? { Some(cmyk) if rgba[3] >= 1.0 => Source::Cmyk { rgba, cmyk }, Some(_) => return Err(bad("`colorCmyk` requires an opaque color")), None if p.get("color").is_none_or(Value::is_null) => color_source(s, rgba, 0), - None => match s.tools.matching_cmyk([rgba[0], rgba[1], rgba[2]]) { + None => match s.tools.matching_cmyk([picked[0], picked[1], picked[2]]) { Some(cmyk) if rgba[3] >= 1.0 => Source::Cmyk { rgba, cmyk }, _ => Source::Color(rgba), }, @@ -172,6 +175,9 @@ mod tests; #[cfg(test)] mod mode_by_document_tests; +#[cfg(test)] +mod grayscale_color_tests; + #[cfg(test)] #[path = "fill_dissolve_opacity_rounding_tests.rs"] mod fill_dissolve_opacity_rounding_tests; diff --git a/crates/engine-paint/src/cmd/fill_cmds/grayscale_color_tests.rs b/crates/engine-paint/src/cmd/fill_cmds/grayscale_color_tests.rs new file mode 100644 index 0000000000..f0d134a5e1 --- /dev/null +++ b/crates/engine-paint/src/cmd/fill_cmds/grayscale_color_tests.rs @@ -0,0 +1,95 @@ +//! #17469: in a Grayscale document Edit › Fill with Color or Foreground Color writes the colour as the document takes +//! it, the same grey Background Color already writes (#11940): RGB 0, 0, 255 gives about 69 (the reference measured +//! 69), not its luma 29. Other modes keep the colour; Black, White and 50% Gray stay exact. + +use super::*; +use serde_json::json; + +const BLUE: [f32; 4] = [0.0, 0.0, 1.0, 1.0]; +const GREEN_200: [f32; 4] = [0.0, 200.0 / 255.0, 0.0, 1.0]; + +/// A white 64×32 document of `mode` at `depth` bits. +fn session(mode: &str, depth: u32) -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 64, "height": 32, "mode": mode, "depth": depth, "background": "white"})).unwrap(); + s +} + +fn pixel(s: &mut Session) -> [f32; 4] { + let v = s.execute("document.pixel", json!({"x": 50, "y": 20})).unwrap(); + let a: Vec = v.as_array().unwrap().iter().map(|c| c.as_f64().unwrap() as f32).collect(); + [a[0], a[1], a[2], a[3]] +} + +fn level(c: f32) -> i32 { + (c * 255.0).round() as i32 +} + +/// The pixel after filling a fresh document with `params`, the foreground and background both set to `c`. +fn filled(mode: &str, depth: u32, c: [f32; 4], params: serde_json::Value) -> [f32; 4] { + let mut s = session(mode, depth); + s.tools.foreground = c; + s.tools.background = c; + s.execute("edit.fill", params).unwrap(); + pixel(&mut s) +} + +fn hex(c: [f32; 4]) -> String { + format!("#{:02x}{:02x}{:02x}", level(c[0]), level(c[1]), level(c[2])) +} + +#[test] +fn gray_8_color_fill_writes_the_converted_gray_not_the_luma() { + let p = filled("grayscale", 8, [1.0; 4], json!({"contents": "color", "color": "#0000ff"})); + // The luma grey is 29; the colour as the document takes it is about 69 (sGray gives 70). + assert!((level(p[0]) - 69).abs() <= 1, "filled pixel {p:?}"); + assert_eq!(level(p[0]), level(p[2]), "the grey is written as equal channels"); +} + +#[test] +fn color_and_foreground_equal_background_color_at_every_gray_depth() { + for depth in [8, 16, 32] { + for c in [BLUE, GREEN_200] { + let bg = filled("grayscale", depth, c, json!({"contents": "background"})); + let fg = filled("grayscale", depth, c, json!({"contents": "foreground"})); + let color = filled("grayscale", depth, c, json!({"contents": "color", "color": hex(c)})); + // The old `{"color": …}` form without a colour takes the foreground. + let default = filled("grayscale", depth, c, json!({})); + for (what, got) in [("foreground", fg), ("color", color), ("default color", default)] { + assert!((got[0] - bg[0]).abs() < 0.5 / 255.0, "{depth}-bit {}: {what} {got:?}, background {bg:?}", hex(c)); + } + } + } +} + +#[test] +fn other_modes_keep_the_colour_as_it_is() { + for c in [BLUE, GREEN_200] { + let p = filled("rgb", 8, [1.0; 4], json!({"contents": "color", "color": hex(c)})); + assert_eq!([level(p[0]), level(p[1]), level(p[2])], [level(c[0]), level(c[1]), level(c[2])], "RGB color {p:?}"); + let q = filled("rgb", 8, c, json!({"contents": "foreground"})); + assert_eq!(q, p, "RGB foreground"); + for mode in ["cmyk", "lab"] { + let before = filled(mode, 8, c, json!({"contents": "background"})); + for params in [json!({"contents": "color", "color": hex(c)}), json!({"contents": "foreground"})] { + let got = filled(mode, 8, c, params.clone()); + assert!(got.iter().zip(before).all(|(a, b)| (a - b).abs() < 0.5 / 255.0), "{mode} {params}: {got:?} vs {before:?}"); + } + } + } +} + +#[test] +fn black_white_and_gray_stay_exact_in_gray_documents() { + for depth in [8, 16, 32] { + for (contents, want) in [("black", 0.0), ("white", 1.0)] { + let p = filled("grayscale", depth, BLUE, json!({"contents": contents})); + assert_eq!(p[0], want, "{depth}-bit {contents}"); + } + let g = filled("grayscale", depth, BLUE, json!({"contents": "gray"})); + assert!((g[0] - 128.0 / 255.0).abs() < 0.5 / 255.0, "{depth}-bit 50% Gray {g:?}"); + let black = filled("grayscale", depth, [1.0; 4], json!({"contents": "color", "color": "#000000"})); + let white = filled("grayscale", depth, [0.0, 0.0, 0.0, 1.0], json!({"contents": "color", "color": "#ffffff"})); + assert_eq!((black[0], white[0]), (0.0, 1.0), "{depth}-bit Color black and white"); + } +} diff --git a/crates/engine/tests/it/paint_in_fill_layer_mode.rs b/crates/engine/tests/it/paint_in_fill_layer_mode.rs index 6dfe2fbd25..469b0e07f8 100644 --- a/crates/engine/tests/it/paint_in_fill_layer_mode.rs +++ b/crates/engine/tests/it/paint_in_fill_layer_mode.rs @@ -68,10 +68,13 @@ fn solid_color_fill_matches_edit_fill() { s.execute("layer.newFillLayer.solidColor", json!({"color": "#0000ff"})).unwrap(); assert_in_mode(&s, cm, mode); let got = pixel(&mut s, 10, 10); - let want = painted(mode, depth, BLUE); - assert!(close(got, want, 0.01), "{mode}/{depth}: fill layer {got:?}, painted {want:?}"); if cm == ColorMode::Grayscale { + // Edit › Fill converts a colour through the document's grey as the reference does (#17469); the fill + // layer was not measured and keeps its luma grey. assert!((got[0] - 0.114).abs() < 0.01 && got[0] == got[1] && got[1] == got[2], "{mode}/{depth}: {got:?}"); + } else { + let want = painted(mode, depth, BLUE); + assert!(close(got, want, 0.01), "{mode}/{depth}: fill layer {got:?}, painted {want:?}"); } } } From 51c85c8f1e7d16acd0fefc06414291bd9abf2bd4 Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 01:36:52 +0100 Subject: [PATCH 2/3] Test: the Free Transform style test uses a yellow Stroke Its Grayscale cases need the Stroke apart from the blue fill. With the fill now written as the measured grey (70), the red Stroke's luma grey (76) came too close; yellow stays apart in every mode and keeps the RGB checks. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/transform_fx_tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/transform_fx_tests.rs b/crates/ui-egui/src/transform_fx_tests.rs index a1acc162fa..30914daa91 100644 --- a/crates/ui-egui/src/transform_fx_tests.rs +++ b/crates/ui-egui/src/transform_fx_tests.rs @@ -11,7 +11,7 @@ use crate::PhotocraftApp; use craft::imaging::ui::transform_preview::StylePlan; /// A 200 × 160 document with a 40 × 20 blue block at (60, 60) on its own layer, wearing a 4 px -/// red outside Stroke. +/// yellow outside Stroke (yellow stays apart from the blue in every mode, also as a Grayscale grey). fn app(mode: &str, depth: u32) -> Box { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); app.run("file.new", json!({"width": 200, "height": 160, "mode": mode, "depth": depth})).unwrap(); @@ -19,7 +19,7 @@ fn app(mode: &str, depth: u32) -> Box { app.run("select.rect", json!({"x": 60, "y": 60, "width": 40, "height": 20})).unwrap(); app.run("edit.fill", json!({"color": "#0000ff"})).unwrap(); app.run("select.deselect", json!({})).unwrap(); - app.run("layer.layerStyle.stroke", json!({"size": 4, "color": "#ff0000", "position": "outside"})).unwrap(); + app.run("layer.layerStyle.stroke", json!({"size": 4, "color": "#ffff00", "position": "outside"})).unwrap(); app.sync_views(); app } From 8ddbee2c25cf0f9797fe98df85e5c044dedbdc6e Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 02:10:31 +0100 Subject: [PATCH 3/3] Fill: colour fills in 8-bit Monotone documents take the grey value (#17478) swatch_in_doc now converts swatches in an 8-bit Monotone document as well, through a gamma 2.2 grey, which fits the reference application's measured values there (200,30,30 -> 104, 30,30,200 -> 64, 140,140,140 -> 139) where sGray (the Grayscale document's curve) gives 61 and 140. Duotones with more inks and 16-bit Duotones were not measured and keep the luma. Co-Authored-By: Claude Opus 5.5 --- crates/engine-core/src/cmd/color_cmds.rs | 19 ++++-- crates/engine-paint/src/cmd/fill_cmds.rs | 3 + .../cmd/fill_cmds/duotone_grey_value_tests.rs | 61 +++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 crates/engine-paint/src/cmd/fill_cmds/duotone_grey_value_tests.rs diff --git a/crates/engine-core/src/cmd/color_cmds.rs b/crates/engine-core/src/cmd/color_cmds.rs index c709bb6d43..f78d32f82b 100644 --- a/crates/engine-core/src/cmd/color_cmds.rs +++ b/crates/engine-core/src/cmd/color_cmds.rs @@ -840,16 +840,18 @@ pub fn document_profile(doc: &Document) -> Arc { /// so pixel conversions such as Image › Mode › Grayscale keep using the document's profile. Other /// modes, and a failed conversion, keep the colour. So do Grayscale documents outside the default /// gray working space, which #11940 did not measure: a Working Gray other than Dot Gain 20 %, or a -/// document tagged with a gray profile other than that one. +/// document tagged with a gray profile other than that one. An 8-bit Monotone document (one ink) +/// takes the colour's gray with a gamma 2.2 curve instead, which fits the reference's values there +/// (RGB 200,30,30 / 30,30,200 / 140,140,140 become 104 / 64 / 139; sGray gives 104 / 61 / 140), +/// not its luma 81 (#17478). Duotones with more inks and 16-bit Duotones were not measured. 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 Some((st, target)) = s.active().and_then(|st| swatch_gray(&st.doc).map(|t| (st, t))) 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() { return c; } 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 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; @@ -863,6 +865,15 @@ pub fn swatch_in_doc(s: &Session, c: [f32; 4]) -> [f32; 4] { } } +/// The gray [`swatch_in_doc`] converts swatches to: sGray in Grayscale, gamma 2.2 in 8-bit Monotone. +fn swatch_gray(doc: &Document) -> Option { + match doc.mode { + ColorMode::Grayscale => Some(Builtin::SGray), + ColorMode::Duotone if doc.depth == SampleType::U8 && doc.duotone.as_ref().is_some_and(|d| d.inks.len() == 1) => Some(Builtin::GrayGamma22), + _ => None, + } +} + /// Profile describing the compositor's RGB output for this document (see the module docs). pub fn composite_profile(doc: &Document) -> Arc { match mode_space(doc.mode) { diff --git a/crates/engine-paint/src/cmd/fill_cmds.rs b/crates/engine-paint/src/cmd/fill_cmds.rs index cdc18942a3..3da89c1f25 100644 --- a/crates/engine-paint/src/cmd/fill_cmds.rs +++ b/crates/engine-paint/src/cmd/fill_cmds.rs @@ -178,6 +178,9 @@ mod mode_by_document_tests; #[cfg(test)] mod grayscale_color_tests; +#[cfg(test)] +mod duotone_grey_value_tests; + #[cfg(test)] #[path = "fill_dissolve_opacity_rounding_tests.rs"] mod fill_dissolve_opacity_rounding_tests; diff --git a/crates/engine-paint/src/cmd/fill_cmds/duotone_grey_value_tests.rs b/crates/engine-paint/src/cmd/fill_cmds/duotone_grey_value_tests.rs new file mode 100644 index 0000000000..cd8eb34ef0 --- /dev/null +++ b/crates/engine-paint/src/cmd/fill_cmds/duotone_grey_value_tests.rs @@ -0,0 +1,61 @@ +//! #17478: in an 8-bit Monotone document Edit › Fill with a colour writes the colour's grey (gamma 2.2, which fits the +//! reference's values), not its luma: Background 30,30,200 gives about 64 (the reference measured 64), not 49. A +//! Grayscale document's sGray would give 61. Opacity keeps its linear blend. Duotones with more inks and 16-bit +//! Duotones were not measured and keep the luma. + +use super::*; +use serde_json::json; + +const RED: [f32; 4] = [200.0 / 255.0, 30.0 / 255.0, 30.0 / 255.0, 1.0]; +const BLUE: [f32; 4] = [30.0 / 255.0, 30.0 / 255.0, 200.0 / 255.0, 1.0]; + +/// A white 200×100 document of `mode` (`monotone`, `duotone` or `grayscale`) at `depth` bits, selection +/// 50,25-150,75, foreground red and background blue. +fn session(mode: &str, depth: u32) -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 200, "height": 100, "mode": "grayscale", "depth": depth, "background": "white"})).unwrap(); + if mode != "grayscale" { + s.execute("image.mode.duotone", json!({"type": mode})).unwrap(); + } + s.execute("select.rect", json!({"x": 50, "y": 25, "width": 100, "height": 50, "antiAlias": false})).unwrap(); + s.tools.foreground = RED; + s.tools.background = BLUE; + s +} + +/// The grey level (0..=255) at the selection's centre after `edit.fill` with `params`. +fn filled(mode: &str, depth: u32, params: serde_json::Value) -> i32 { + let mut s = session(mode, depth); + s.execute("edit.fill", params).unwrap(); + let v = s.execute("document.pixel", json!({"x": 100, "y": 50})).unwrap(); + (v[0].as_f64().unwrap() * 255.0).round() as i32 +} + +#[test] +fn monotone_fills_give_the_measured_grey_values() { + for (params, want) in [ + (json!({"contents": "foreground"}), 104), + (json!({"contents": "background"}), 64), + (json!({"contents": "color", "color": "#8c8c8c"}), 139), + (json!({"contents": "gray"}), 128), + (json!({"contents": "black"}), 0), + ] { + let got = filled("monotone", 8, params.clone()); + assert!((got - want).abs() <= 1, "{params}: {got}, the reference gives {want}"); + } +} + +#[test] +fn monotone_opacity_stays_a_linear_blend() { + let half = filled("monotone", 8, json!({"contents": "black", "opacity": 50})); + assert!((127..=128).contains(&half), "Black at 50 %: {half}"); + let under = filled("monotone", 8, json!({"contents": "black", "opacity": 49})); + assert!((129..=131).contains(&under), "Black at 49 %: {under}"); +} + +#[test] +fn unmeasured_duotones_keep_the_luma() { + // Rec.601 luma of 200,30,30 is 81; the grey working space gives about 104. + assert_eq!(filled("duotone", 8, json!({"contents": "foreground"})), 81, "two inks"); + assert_eq!(filled("monotone", 16, json!({"contents": "foreground"})), 81, "16-bit Monotone"); +}