From ceeac4c6c0fe8d6ba4e88fd740eb78cd95474c25 Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:46:30 +0100 Subject: [PATCH 1/4] Canvas Size: Gray extension colour, 128,128,128 (#14543) image.canvasSize knew foreground, white, black, transparent and #rrggbb for extensionColor; "gray" fell through to the background colour, and a recorded Canvas Size step whose extension colour type is Gray ("Gry ") imported as unsupported, while a "gray" step could not be written back. "gray" is now the reference application's 128,128,128, the params doc lists it, and the EXTENSION table maps "Gry " to "gray" both ways, so recorded steps import, play back and export as Gray. Unknown values keep falling back to the background colour. --- crates/engine/src/cmd/atn_cmds/tables.rs | 2 +- crates/engine/src/cmd/image_cmds.rs | 7 +- .../cmd/image_cmds/canvas_size_gray_tests.rs | 110 ++++++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 crates/engine/src/cmd/image_cmds/canvas_size_gray_tests.rs diff --git a/crates/engine/src/cmd/atn_cmds/tables.rs b/crates/engine/src/cmd/atn_cmds/tables.rs index 0a6a07abeea..83fbad1bc25 100644 --- a/crates/engine/src/cmd/atn_cmds/tables.rs +++ b/crates/engine/src/cmd/atn_cmds/tables.rs @@ -126,7 +126,7 @@ pub(super) const OFFSET_FILL: &[(&str, &str)] = &[("Wrp ", "wrap"), ("Rpt ", "re pub(super) const TRIM: &[(&str, &str)] = &[("Trns", "transparent"), ("topLeftPixelColor", "topLeft"), ("bottomRightPixelColor", "bottomRight")]; pub(super) const EXTENSION: &[(&str, &str)] = - &[("Bckg", "background"), ("FrgC", "foreground"), ("Frgd", "foreground"), ("Wht ", "white"), ("Blck", "black"), ("Trns", "transparent")]; + &[("Bckg", "background"), ("FrgC", "foreground"), ("Frgd", "foreground"), ("Wht ", "white"), ("Blck", "black"), ("Gry ", "gray"), ("Trns", "transparent")]; pub(super) const FILL_CONTENTS: &[(&str, &str)] = &[ ("FrgC", "foreground"), diff --git a/crates/engine/src/cmd/image_cmds.rs b/crates/engine/src/cmd/image_cmds.rs index 571f4754aca..2f08f46b42a 100644 --- a/crates/engine/src/cmd/image_cmds.rs +++ b/crates/engine/src/cmd/image_cmds.rs @@ -683,6 +683,8 @@ fn extension_color(s: &Session, p: &Value) -> [f32; 4] { "foreground" => s.tools.foreground, "white" => [1.0; 4], "black" => [0.0, 0.0, 0.0, 1.0], + // The reference application's Gray: 128,128,128 (#14543). + "gray" => [128.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 1.0], "transparent" => [0.0; 4], hex if hex.starts_with('#') => { let h = hex.trim_start_matches('#'); @@ -1250,7 +1252,7 @@ pub fn specs() -> Vec { "image.canvasSize", "Canvas Size…", ["Image"], - r##"{"width":number,"height":number,"units":"px|percent"="px" (percent uses each current side; absolute sizes only),"relative":bool=false,"anchor":"topLeft|top|topRight|left|center|right|bottomLeft|bottom|bottomRight"="center","extensionColor":"background|foreground|white|black|transparent|#rrggbb"="background"}"##, + r##"{"width":number,"height":number,"units":"px|percent"="px" (percent uses each current side; absolute sizes only),"relative":bool=false,"anchor":"topLeft|top|topRight|left|center|right|bottomLeft|bottom|bottomRight"="center","extensionColor":"background|foreground|white|black|gray|transparent|#rrggbb"="background"}"##, has_doc, canvas_size ), @@ -1325,3 +1327,6 @@ mod image_size_drops_selection_tests; #[cfg(test)] mod canvas_size_noop_history_tests; + +#[cfg(test)] +mod canvas_size_gray_tests; diff --git a/crates/engine/src/cmd/image_cmds/canvas_size_gray_tests.rs b/crates/engine/src/cmd/image_cmds/canvas_size_gray_tests.rs new file mode 100644 index 00000000000..c9051272671 --- /dev/null +++ b/crates/engine/src/cmd/image_cmds/canvas_size_gray_tests.rs @@ -0,0 +1,110 @@ +//! #14543: Canvas Size takes Gray as its extension colour, the reference application's 128,128,128, +//! from the command and from recorded `.atn` steps (`canvasExtensionColorType` `Gry `), and a +//! `gray` step is written back as Gray. + +use craft_psd::atn::{self, AtnAction, AtnEvent, AtnFile, AtnItem}; +use craft_psd::descriptor::{Descriptor, Id, UnicodeString, Value as D}; +use serde_json::{Value, json}; + +use crate::Session; +use crate::actions_cmds::{Action, ActionStep}; + +/// A 400 x 300 document at `depth` with a white Background and an empty layer "Top" above it. +fn doc(depth: u32) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 400, "height": 300, "depth": depth, "background": "white"})).unwrap(); + s.execute("layer.new.layer", json!({"name": "Top"})).unwrap(); + s +} + +/// The 8-bit RGB the document shows at pixel (x, y), as the Eyedropper reads it. +fn read(s: &mut Session, x: i32, y: i32) -> [i64; 3] { + let v = s.execute("document.sampleColor", json!({"x": f64::from(x) + 0.5, "y": f64::from(y) + 0.5})).unwrap(); + let c = |i: usize| (v[i].as_f64().unwrap() * 255.0).round() as i64; + [c(0), c(1), c(2)] +} + +/// The alpha of the layer "Top" at pixel (x, y). +fn top_alpha(s: &Session, x: i32, y: i32) -> f32 { + let doc = &s.active().unwrap().doc; + let top = doc.layers.iter().find(|l| l.name == "Top").unwrap(); + top.surface().unwrap().rgba(x, y)[3] +} + +const GROW: fn(&str) -> Value = |color| json!({"width": 500, "height": 400, "anchor": "topLeft", "extensionColor": color}); + +#[test] +fn gray_extends_the_background_with_128_gray_at_every_depth() { + for depth in [8, 16, 32] { + let mut s = doc(depth); + s.execute("image.canvasSize", GROW("gray")).unwrap(); + assert_eq!(read(&mut s, 450, 350), [128, 128, 128], "depth {depth}: the new area"); + assert_eq!(read(&mut s, 10, 10), [255, 255, 255], "depth {depth}: the old Background keeps its pixels"); + assert_eq!(top_alpha(&s, 450, 350), 0.0, "depth {depth}: only the Background is filled"); + } +} + +#[test] +fn an_unknown_extension_colour_still_takes_the_background_colour() { + let mut s = doc(8); + s.execute("image.canvasSize", GROW("grey-ish")).unwrap(); + assert_eq!(read(&mut s, 450, 350), [255, 255, 255]); +} + +#[test] +fn the_params_doc_lists_gray() { + let spec = crate::cmd::command_specs().iter().find(|c| c.id == "image.canvasSize").unwrap(); + assert!(spec.params.contains("|gray|"), "{}", spec.params); +} + +/// A recorded Canvas Size step: +100 / +100 px, relative, from the top left, extension colour type `ty`. +fn recorded_step(ty: &str) -> Vec { + let px = |v: f64| D::UnitFloat { unit: *b"#Pxl", value: v }; + let enm = |t: &str, v: &str| D::Enumerated { type_id: Id::new(t), value: Id::new(v) }; + let descriptor = Descriptor::new("null") + .with("Wdth", px(100.0)) + .with("Hght", px(100.0)) + .with("Rltv", D::Boolean(true)) + .with("Hrzn", enm("HrzL", "Left")) + .with("Vrtc", enm("VrtL", "Top ")) + .with("canvasExtensionColorType", enm("canvasExtensionColorType", ty)); + atn::write(&AtnFile { + name: UnicodeString::new_nul("Canvas"), + actions: vec![AtnAction { + name: UnicodeString::new_nul("Step"), + items: vec![AtnItem { event: AtnEvent::Code(*b"CnvS"), dictionary: b"CnvS".to_vec(), descriptor: Some(descriptor), ..Default::default() }], + ..Default::default() + }], + ..Default::default() + }) + .unwrap() +} + +#[test] +fn a_recorded_gray_step_imports_as_gray_and_plays_back_128_gray() { + let bytes = recorded_step("Gry "); + let imported = crate::atn_cmds::import(&bytes).unwrap(); + assert!(imported.unsupported.is_empty(), "the Gray step is supported: {:?}", imported.unsupported); + assert_eq!(imported.actions[0].steps[0].params["extensionColor"], json!("gray")); + let mut s = doc(8); + let loaded = s.execute("actions.load", json!({"data": photocraft_paint::tile::b64_encode(&bytes)})).unwrap(); + assert_eq!(loaded["unsupported"], json!([])); + let played = s.execute("actions.play", json!({"action": loaded["actions"][0]})).unwrap(); + assert!(played.get("failed").is_none(), "{played}"); + assert_eq!(read(&mut s, 450, 350), [128, 128, 128]); +} + +#[test] +fn a_gray_step_is_written_back_as_gray() { + let action = Action::new("Step", vec![ActionStep::new("image.canvasSize", GROW("gray"))]); + let (bytes, skipped) = crate::atn_cmds::export("Canvas", &[&action], &[], false).unwrap(); + assert!(skipped.is_empty(), "{} skipped", skipped.len()); + let file = atn::parse(&bytes).unwrap(); + let d = file.actions[0].items[0].descriptor.as_ref().unwrap(); + assert!( + matches!(d.get("canvasExtensionColorType"), Some(D::Enumerated { value, .. }) if *value == Id::new("Gry ")), + "{:?}", + d.get("canvasExtensionColorType") + ); + assert!(d.get("canvasExtensionColor").is_none(), "not a custom colour"); +} From 9c6e55f7d79b2b54f02fb11b12f264d7fd24832d Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 23 Nov 2026 09:48:26 +0100 Subject: [PATCH 2/4] Canvas Size: Gray is linear grey 128 in a 32-bit document 32-bit documents hold linear light, so the Gray extension colour there is grey 128 decoded as Image > Mode > 32 Bits/Channel decodes it (0.2159), not 128/255. --- crates/engine-core/src/cmd/image_cmds.rs | 14 +++++++-- .../cmd/image_cmds/canvas_size_gray_tests.rs | 29 +++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/crates/engine-core/src/cmd/image_cmds.rs b/crates/engine-core/src/cmd/image_cmds.rs index 5e4ac32efff..d042ed4f68c 100644 --- a/crates/engine-core/src/cmd/image_cmds.rs +++ b/crates/engine-core/src/cmd/image_cmds.rs @@ -684,8 +684,18 @@ fn extension_color(s: &Session, p: &Value) -> [f32; 4] { "foreground" => s.tools.foreground, "white" => [1.0; 4], "black" => [0.0, 0.0, 0.0, 1.0], - // The reference application's Gray: 128,128,128 (#14543). - "gray" => [128.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 1.0], + // The reference application's Gray: 128,128,128 (#14543). A 32-bit document holds linear + // light, so there it is grey 128 decoded as `change_depth` decodes it (0.2159). + "gray" => { + let v = 128.0 / 255.0; + let v = match s.active().map(|d| (d.doc.mode, d.doc.depth)) { + Some((mode @ (ColorMode::Rgb | ColorMode::Grayscale), SampleType::F32)) => { + photocraft_compose::adjust::Transfer::for_document(mode, SampleType::U8).decode(v) + } + _ => v, + }; + [v, v, v, 1.0] + } "transparent" => [0.0; 4], hex if hex.starts_with('#') => { let h = hex.trim_start_matches('#'); diff --git a/crates/engine-core/src/cmd/image_cmds/canvas_size_gray_tests.rs b/crates/engine-core/src/cmd/image_cmds/canvas_size_gray_tests.rs index 6c471e48c86..90e565a10ad 100644 --- a/crates/engine-core/src/cmd/image_cmds/canvas_size_gray_tests.rs +++ b/crates/engine-core/src/cmd/image_cmds/canvas_size_gray_tests.rs @@ -34,8 +34,8 @@ fn top_alpha(s: &Session, x: i32, y: i32) -> f32 { const GROW: fn(&str) -> Value = |color| json!({"width": 500, "height": 400, "anchor": "topLeft", "extensionColor": color}); #[test] -fn gray_extends_the_background_with_128_gray_at_every_depth() { - for depth in [8, 16, 32] { +fn gray_extends_the_background_with_128_gray_at_8_and_16_bits() { + for depth in [8, 16] { let mut s = doc(depth); s.execute("image.canvasSize", GROW("gray")).unwrap(); assert_eq!(read(&mut s, 450, 350), [128, 128, 128], "depth {depth}: the new area"); @@ -44,6 +44,31 @@ fn gray_extends_the_background_with_128_gray_at_every_depth() { } } +/// The Background's stored colour at pixel (x, y). +fn background_value(s: &Session, x: i32, y: i32) -> [f32; 4] { + s.active().unwrap().doc.background().unwrap().surface().unwrap().rgba(x, y) +} + +/// A 32-bit document holds linear light: its Gray is the value an 8-bit grey 128 becomes when the +/// document is converted to 32 bits (0.2159), not 128/255. +#[test] +fn gray_in_a_32_bit_document_is_linear_grey_128() { + let mut converted = crate::session(); + converted.execute("file.new", json!({"width": 4, "height": 4, "depth": 8, "background": "white"})).unwrap(); + converted.execute("edit.fill", json!({"color": "#808080"})).unwrap(); + converted.execute("image.mode.bits32", json!({})).unwrap(); + let want = background_value(&converted, 1, 1); + assert!((want[0] - 0.2159).abs() < 0.002, "converted grey 128: {want:?}"); + let mut s = doc(32); + s.execute("image.canvasSize", GROW("gray")).unwrap(); + let got = background_value(&s, 450, 350); + for i in 0..3 { + assert!((got[i] - want[i]).abs() < 1e-4, "channel {i}: {got:?} vs {want:?}"); + } + assert_eq!(got[3], 1.0); + assert_eq!(background_value(&s, 10, 10)[..3], [1.0, 1.0, 1.0], "the old Background keeps its pixels"); +} + #[test] fn an_unknown_extension_colour_still_takes_the_background_colour() { let mut s = doc(8); From 657abde6766055adf8925b38001aa97cb8167836 Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 23 Nov 2026 09:58:44 +0100 Subject: [PATCH 3/4] Canvas Size: read the params doc from the installed registry in the test --- .../engine-core/src/cmd/image_cmds/canvas_size_gray_tests.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/engine-core/src/cmd/image_cmds/canvas_size_gray_tests.rs b/crates/engine-core/src/cmd/image_cmds/canvas_size_gray_tests.rs index 90e565a10ad..225d159cfd0 100644 --- a/crates/engine-core/src/cmd/image_cmds/canvas_size_gray_tests.rs +++ b/crates/engine-core/src/cmd/image_cmds/canvas_size_gray_tests.rs @@ -78,7 +78,9 @@ fn an_unknown_extension_colour_still_takes_the_background_colour() { #[test] fn the_params_doc_lists_gray() { - let spec = crate::cmd::command_specs().iter().find(|c| c.id == "image.canvasSize").unwrap(); + // The registry lists the commands once a session has installed them. + let _ = crate::session(); + let spec = crate::registry::find("image.canvasSize").unwrap(); assert!(spec.params.contains("|gray|"), "{}", spec.params); } From 446dd0e38b7f8a65443e32020b6a4485028282a5 Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:32:52 +0100 Subject: [PATCH 4/4] Canvas Size: Gray keeps the background colour in an Indexed document Indexed documents were not measured (#14543), so they keep today's behaviour. --- crates/engine-core/src/cmd/image_cmds.rs | 4 ++++ .../src/cmd/image_cmds/canvas_size_gray_tests.rs | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/crates/engine-core/src/cmd/image_cmds.rs b/crates/engine-core/src/cmd/image_cmds.rs index d042ed4f68c..3e3eabaa114 100644 --- a/crates/engine-core/src/cmd/image_cmds.rs +++ b/crates/engine-core/src/cmd/image_cmds.rs @@ -687,6 +687,10 @@ fn extension_color(s: &Session, p: &Value) -> [f32; 4] { // The reference application's Gray: 128,128,128 (#14543). A 32-bit document holds linear // light, so there it is grey 128 decoded as `change_depth` decodes it (0.2159). "gray" => { + // An Indexed document was not measured (#14543) and keeps today's background colour. + if s.active().is_some_and(|d| d.doc.mode == ColorMode::Indexed) { + return s.tools.background; + } let v = 128.0 / 255.0; let v = match s.active().map(|d| (d.doc.mode, d.doc.depth)) { Some((mode @ (ColorMode::Rgb | ColorMode::Grayscale), SampleType::F32)) => { diff --git a/crates/engine-core/src/cmd/image_cmds/canvas_size_gray_tests.rs b/crates/engine-core/src/cmd/image_cmds/canvas_size_gray_tests.rs index 225d159cfd0..1edddf41c0b 100644 --- a/crates/engine-core/src/cmd/image_cmds/canvas_size_gray_tests.rs +++ b/crates/engine-core/src/cmd/image_cmds/canvas_size_gray_tests.rs @@ -69,6 +69,22 @@ fn gray_in_a_32_bit_document_is_linear_grey_128() { assert_eq!(background_value(&s, 10, 10)[..3], [1.0, 1.0, 1.0], "the old Background keeps its pixels"); } +/// An Indexed Color document was not measured and keeps today's behaviour: Gray takes the +/// background colour there, as an unknown value does. +#[test] +fn gray_in_an_indexed_document_keeps_the_background_colour() { + let grown = |color: &str| { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 400, "height": 300, "background": "white"})).unwrap(); + s.execute("image.mode.indexedColor", json!({})).unwrap(); + s.tools.background = [0.0, 0.0, 1.0, 1.0]; + s.execute("image.canvasSize", GROW(color)).unwrap(); + background_value(&s, 450, 350) + }; + assert_eq!(grown("gray"), grown("background"), "Gray fills with the background colour"); + assert_ne!(grown("gray"), grown("white"), "the test tells the two apart"); +} + #[test] fn an_unknown_extension_colour_still_takes_the_background_colour() { let mut s = doc(8);