Merge #18210 into the merge queue batch

Background from Layer: convert the background colour to grey in Grayscale documents
This commit is contained in:
David Mann committed 2026-11-30 14:56:08 +01:00
commit 2016c78a58
3 files changed
+96 -1

No files matched your search

@@ -76,7 +76,9 @@ fn background_from_layer(s: &mut Session, p: &Value) -> Result<Value> {
}
Some(v) => LayerId(v.as_u64().ok_or_else(|| EngineError::BadParams { cmd: CMD.into(), msg: "`layer` must be a layer id".into() })?),
};
let bg = s.tools.background;
// The background colour in the document's mode, as Edit › Fill uses it: a Grayscale
// document converts it to its grey rather than taking the composite's luma (#17308).
let bg = crate::color_cmds::swatch_in_doc(s, s.tools.background);
// The reference names the state "Convert to Background" however the command runs (#17305).
s.edit("Convert to Background", |doc, active| {
if background_id(doc).is_some() {
@@ -0,0 +1,92 @@
//! Layer › New › Background from Layer in a Grayscale document (#17308).
//!
//! Measured in the reference (Grayscale/8, background colour RGB 0,200,0): the empty area of the
//! new Background reads 32.9 % ink (about 171), the same grey Edit › Fill with the background
//! colour gives, and a 50 % opacity grey 128 square over it about 149. Before the fix the
//! toolbox colour went into the composite unconverted and the empty area read 117.
use photocraft_doc::LayerId;
use photocraft_engine::Session;
use photocraft_geom::Rect;
use serde_json::json;
const GREEN: [f32; 4] = [0.0, 200.0 / 255.0, 0.0, 1.0];
/// A 60×40 transparent document in `mode`: layer A with a grey 128 square at (10,10)-(30,30)
/// at 50 % opacity, and an empty layer B above it.
fn session(mode: &str, depth: u32) -> (Session, LayerId, LayerId) {
let mut s = Session::new();
s.execute("file.new", json!({"width": 60, "height": 40, "mode": mode, "depth": depth, "background": "transparent"})).unwrap();
let new = |s: &mut Session, name: &str| LayerId(s.execute("layer.new.layer", json!({"name": name})).unwrap()["layer"].as_u64().unwrap());
let a = new(&mut s, "A");
s.execute("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20, "antiAlias": false})).unwrap();
s.execute("edit.fill", json!({"contents": "color", "color": "#808080"})).unwrap();
s.execute("select.deselect", json!({})).unwrap();
s.execute("layer.setProps", json!({"layer": a.0, "opacity": 0.5})).unwrap();
let b = new(&mut s, "B");
s.tools.background = GREEN;
(s, a, b)
}
fn pixel(s: &Session, layer: LayerId, x: i32, y: i32) -> Vec<f32> {
s.active().unwrap().doc.layer(layer).unwrap().surface().unwrap().read_region(Rect::new(x, y, x + 1, y + 1))
}
/// What Edit › Fill with the background colour puts on empty layer B in the same document.
fn fill_reference(mode: &str, depth: u32) -> Vec<f32> {
let (mut s, _, b) = session(mode, depth);
s.execute("layer.select", json!({"layer": b.0})).unwrap();
s.execute("edit.fill", json!({"contents": "background"})).unwrap();
pixel(&s, b, 40, 35)
}
/// Background from Layer on A: (empty area, square) of the new Background.
fn converted(mode: &str, depth: u32) -> (Vec<f32>, Vec<f32>) {
let (mut s, a, _) = session(mode, depth);
s.execute("layer.new.backgroundFromLayer", json!({"layer": a.0})).unwrap();
assert!(s.active().unwrap().doc.is_background(a));
(pixel(&s, a, 40, 35), pixel(&s, a, 20, 20))
}
fn close(a: &[f32], b: &[f32], tol: f32) -> bool {
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| (x - y).abs() <= tol)
}
#[test]
fn grayscale_8_fills_with_the_converted_background_colour() {
let (empty, square) = converted("grayscale", 8);
let fill = fill_reference("grayscale", 8);
assert!(close(&empty[..1], &fill[..1], 0.5 / 255.0), "empty area {empty:?} matches Edit › Fill {fill:?}");
let g = (empty[0] * 255.0).round();
assert!((171.0..=172.0).contains(&g), "empty area reads {g}, the reference about 171");
let sq = (square[0] * 255.0).round();
assert!((149.0..=150.0).contains(&sq), "50 % grey 128 over it reads {sq}, the reference about 149");
}
#[test]
fn grayscale_16_and_32_match_edit_fill() {
for depth in [16, 32] {
let (empty, _) = converted("grayscale", depth);
let fill = fill_reference("grayscale", depth);
assert!(close(&empty[..1], &fill[..1], 1e-3), "{depth}-bit: empty area {empty:?} matches Edit › Fill {fill:?}");
}
}
#[test]
fn rgb_keeps_the_background_colour() {
for depth in [8, 16, 32] {
let (empty, _) = converted("rgb", depth);
assert!(close(&empty[..3], &GREEN[..3], 1e-3), "{depth}-bit: {empty:?}");
}
}
#[test]
fn cmyk_and_lab_still_match_edit_fill() {
for mode in ["cmyk", "lab"] {
for depth in [8, 16] {
let (empty, _) = converted(mode, depth);
let fill = fill_reference(mode, depth);
assert!(close(&empty, &fill, 1.5 / 255.0), "{mode} {depth}-bit: {empty:?} vs Edit › Fill {fill:?}");
}
}
}
+1
View File
@@ -37,6 +37,7 @@ mod atn_video_frames;
mod auto_blend_layer_kinds;
mod average_smart;
mod background_and_mask_depth;
mod background_from_layer_grayscale;
mod background_from_layer_history_name;
mod background_from_layer_multi_select;
mod background_job_target;