From e7c68638ce3b09d41c3a9afca2a6be1329939668 Mon Sep 17 00:00:00 2001 From: rmsff <17902130+rmsff@users.noreply.github.com> Date: Wed, 11 Nov 2026 03:16:53 +0300 Subject: [PATCH 1/2] Background Eraser: refuse one colour channel of an RGB document With only Red, Green or Blue targeted the reference application refuses the stroke ("target channels do not cover the composite"), erases nothing and adds no History state. The check sits in the setup shared by the command and its live preview, so the preview draws nothing either. The composite and the partial sets that weren't measured keep erasing. The transparency-lock preview test targeted Green alone; it targets Green and Blue now. Its helpers are pub(super) for the new tests. --- ...ckground_eraser_transparency_lock_tests.rs | 6 ++--- crates/engine/src/cmd/eraser_cmds.rs | 22 ++++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/cmd/background_eraser_transparency_lock_tests.rs b/crates/engine/src/cmd/background_eraser_transparency_lock_tests.rs index a5993caa64..c4d91dd92c 100644 --- a/crates/engine/src/cmd/background_eraser_transparency_lock_tests.rs +++ b/crates/engine/src/cmd/background_eraser_transparency_lock_tests.rs @@ -6,7 +6,7 @@ use super::*; /// White Background plus a normal layer "Target" with a solid red band across it (y 10..30). -fn session(depth: u32) -> Session { +pub(super) fn session(depth: u32) -> Session { let mut s = Session::new(); s.execute("file.new", json!({"width": 64, "height": 40, "depth": depth})).unwrap(); s.execute("layer.new.layer", json!({"name": "Target"})).unwrap(); @@ -16,7 +16,7 @@ fn session(depth: u32) -> Session { s } -fn target(s: &Session) -> &Layer { +pub(super) fn target(s: &Session) -> &Layer { let d = s.active().unwrap(); d.doc.layer(d.active_layer.unwrap()).unwrap() } @@ -83,7 +83,7 @@ fn with_a_colour_channel_target_the_preview_matches_the_commit_on_a_locked_layer s.execute("edit.fill", json!({"color": "#3a7fd0", "opacity": 37})).unwrap(); s.execute("select.deselect", json!({})).unwrap(); s.execute("layer.setProps", json!({"locks": {"transparency": true}})).unwrap(); - let opts = |pts: &[[f64; 2]]| json!({"points": pts, "size": 16, "hardness": 0.5, "opacity": 0.53, "target": {"colors": [1]}}); + let opts = |pts: &[[f64; 2]]| json!({"points": pts, "size": 16, "hardness": 0.5, "opacity": 0.53, "target": {"colors": [1, 2]}}); let all = [[8.0, 9.0], [32.0, 10.0], [56.0, 11.0]]; let mut live = live_background_eraser(&s, &opts(&all[..1])).unwrap(); for q in &all[1..] { diff --git a/crates/engine/src/cmd/eraser_cmds.rs b/crates/engine/src/cmd/eraser_cmds.rs index 29833f949a..2d08834f01 100644 --- a/crates/engine/src/cmd/eraser_cmds.rs +++ b/crates/engine/src/cmd/eraser_cmds.rs @@ -133,9 +133,12 @@ fn magic_eraser(s: &mut Session, p: &Value) -> Result { Ok(damage(s, dmg, true)) } -/// The stroke, settings and layer of a `paint.backgroundEraser` (shared by the command and its -/// live preview), after the same checks. -fn background_eraser_setup(s: &Session, p: &Value) -> Result<(Stroke, BgEraseSettings, LayerId)> { +/// The reference application's alert for a colour-channel target that isn't the composite (#12488). +const CHANNEL_TARGET_REFUSED: &str = "Could not use the background eraser because the target channels do not cover the composite."; + +/// The stroke, settings, layer and colour-channel target of a `paint.backgroundEraser` (shared by +/// the command and its live preview), after the same checks. +fn background_eraser_setup(s: &Session, p: &Value) -> Result<(Stroke, BgEraseSettings, LayerId, Option)> { const CMD: &str = "paint.backgroundEraser"; let pts = crate::brush_cmds::parse_points(p, CMD)?; if pts.iter().any(|q| !q.x.is_finite() || !q.y.is_finite() || q.x.abs() > 1e7 || q.y.abs() > 1e7) { @@ -162,14 +165,18 @@ fn background_eraser_setup(s: &Session, p: &Value) -> Result<(Stroke, BgEraseSet if let Some(e) = pixels_locked(&d.doc, l) { return Err(e); } - Ok((Stroke { brush, points: pts }, bs, id)) + let colors = crate::color_channels::target(s, p)?; + // Measured: one colour channel of an RGB document. Other partial sets keep erasing those channels. + if d.doc.mode == ColorMode::Rgb && colors.is_some_and(|c| c.len() == 1) { + return Err(EngineError::Other(CHANNEL_TARGET_REFUSED.into())); + } + Ok((Stroke { brush, points: pts }, bs, id, colors)) } /// `paint.backgroundEraser`: a stroke erasing colours similar to the sampled one. fn background_eraser(s: &mut Session, p: &Value) -> Result { const CMD: &str = "paint.backgroundEraser"; - let (stroke, bs, id) = background_eraser_setup(s, p)?; - let colors = crate::color_channels::target(s, p)?; + let (stroke, bs, id, colors) = background_eraser_setup(s, p)?; let bg = s.tools.background; let dmg = s.edit("Background Eraser", |doc, _| { let selection = doc.selection.clone(); @@ -234,10 +241,9 @@ fn selected_background_effect( /// preview at the last point is the committed result. pub fn live_background_eraser(s: &Session, p: &Value) -> Result { const CMD: &str = "paint.backgroundEraser"; - let (stroke, bs, id) = background_eraser_setup(s, p)?; + let (stroke, bs, id, colors) = background_eraser_setup(s, p)?; let mut doc = (*s.active().ok_or(EngineError::NoDocument)?.doc).clone(); doc.layer(id).ok_or(EngineError::NoLayer(id))?; - let colors = crate::color_channels::target(s, p)?; let bg = s.tools.background; if colors.is_none() { crate::extra_cmds::background_to_layer(&mut doc, id); From af2fe890f8713e5f7c752cf211bda27dfd1a1d91 Mon Sep 17 00:00:00 2001 From: rmsff <17902130+rmsff@users.noreply.github.com> Date: Wed, 11 Nov 2026 03:17:08 +0300 Subject: [PATCH 2/2] Test the Background Eraser refusing one colour channel --- .../background_eraser_channel_target_tests.rs | 111 ++++++++++++++++++ crates/engine/src/cmd/eraser_cmds.rs | 4 + 2 files changed, 115 insertions(+) create mode 100644 crates/engine/src/cmd/background_eraser_channel_target_tests.rs diff --git a/crates/engine/src/cmd/background_eraser_channel_target_tests.rs b/crates/engine/src/cmd/background_eraser_channel_target_tests.rs new file mode 100644 index 0000000000..90d2140f05 --- /dev/null +++ b/crates/engine/src/cmd/background_eraser_channel_target_tests.rs @@ -0,0 +1,111 @@ +//! The Background Eraser with one colour channel of an RGB document targeted (#12488): the +//! reference application refuses with an alert, erases nothing and adds no History state, with +//! Lock transparent pixels off and on. The composite erases as before; the partial sets that +//! weren't measured (two of three channels, CMYK inks and Lab components) keep erasing those +//! channels. 32-bit documents are left to #11912, where the reference refuses the tool outright. + +use super::transparency_lock_tests::{session, target}; +use super::*; + +const CLICK: [[f64; 2]; 1] = [[32.0, 20.0]]; +const DRAG: [[f64; 2]; 3] = [[8.0, 20.0], [32.0, 20.0], [56.0, 20.0]]; + +fn stroke(points: &[[f64; 2]], extra: Value) -> Value { + let mut p = json!({"points": points, "size": 20, "hardness": 1, "tolerance": 50}); + if let (Some(p), Some(extra)) = (p.as_object_mut(), extra.as_object()) { + p.extend(extra.clone()); + } + p +} + +/// Runs `p` and the live preview of its first point, expecting both refused with the alert text +/// and the document (pixels, layers, History) left as it was. +fn assert_refused(s: &mut Session, p: Value, case: &str) { + let (doc, steps) = (s.active().unwrap().doc.clone(), s.active().unwrap().history.past_len()); + let mut first = p.clone(); + first["points"] = json!([p["points"][0]]); + let live = live_background_eraser(s, &first).err().map(|e| e.to_string()); + assert_eq!(live.as_deref(), Some(CHANNEL_TARGET_REFUSED), "{case}: the live preview shows nothing"); + let err = s.execute("paint.backgroundEraser", p).unwrap_err().to_string(); + assert_eq!(err, CHANNEL_TARGET_REFUSED, "{case}"); + let st = s.active().unwrap(); + assert!(std::sync::Arc::ptr_eq(&doc, &st.doc), "{case}: nothing erased"); + assert_eq!(st.history.past_len(), steps, "{case}: no History step"); +} + +/// Runs `p`, expecting it to erase as one History step. +fn assert_erases(s: &mut Session, p: Value, case: &str) { + let id = s.active().unwrap().active_layer.unwrap(); + let (before, steps) = (s.active().unwrap().doc.layer(id).unwrap().surface().unwrap().clone(), s.active().unwrap().history.past_len()); + s.execute("paint.backgroundEraser", p).unwrap_or_else(|e| panic!("{case}: {e}")); + let st = s.active().unwrap(); + assert!(*st.doc.layer(id).unwrap().surface().unwrap() != before, "{case}: erased"); + assert_eq!(st.history.past_len(), steps + 1, "{case}: one History step"); +} + +#[test] +fn one_colour_channel_from_the_channels_panel_refuses_the_click_and_the_drag() { + for depth in [8, 16] { + for lock in [false, true] { + for channel in ["red", "green", "blue"] { + for points in [&CLICK[..], &DRAG[..]] { + let mut s = session(depth); + s.execute("layer.setProps", json!({"locks": {"transparency": lock}})).unwrap(); + s.execute("channel.target", json!({"channel": channel})).unwrap(); + assert_refused(&mut s, stroke(points, json!({})), &format!("{depth}-bit, lock {lock}, {channel}, {} points", points.len())); + assert_eq!(target(&s).locks.transparency, lock); + } + } + } + } +} + +#[test] +fn one_colour_channel_as_the_target_param_refuses_too() { + for k in 0..3 { + let mut s = session(8); + assert_refused(&mut s, stroke(&DRAG, json!({"target": {"colors": [k]}})), &format!("colors [{k}]")); + let mut s = session(8); + assert_refused(&mut s, stroke(&CLICK, json!({"target": {"color": k}})), &format!("color {k}")); + } +} + +#[test] +fn a_click_on_the_background_layer_is_refused_too() { + for depth in [8, 16] { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 64, "height": 40, "depth": depth})).unwrap(); + s.execute("channel.target", json!({"channel": "red"})).unwrap(); + assert_refused(&mut s, stroke(&CLICK, json!({})), &format!("{depth}-bit Background")); + } +} + +#[test] +fn the_composite_erases_as_before() { + for depth in [8, 16, 32] { + let mut s = session(depth); + s.execute("channel.target", json!({"channel": "red"})).unwrap(); + s.execute("channel.target", json!({"channel": "composite"})).unwrap(); + assert_erases(&mut s, stroke(&CLICK, json!({})), &format!("{depth}-bit composite")); + assert_eq!(target(&s).surface().unwrap().rgba(32, 20)[3], 0.0, "{depth}-bit: erased to transparency"); + let mut s = session(depth); + assert_erases(&mut s, stroke(&DRAG, json!({"target": {"colors": [0, 1, 2]}})), &format!("{depth}-bit, all three colour channels")); + } +} + +#[test] +fn partial_sets_that_were_not_measured_keep_erasing() { + let mut s = session(8); + assert_erases(&mut s, stroke(&DRAG, json!({"target": {"colors": [1, 2]}})), "RGB green and blue"); + for (mode, components) in [("cmyk", 4), ("lab", 3)] { + for k in 0..components { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 64, "height": 40, "mode": mode})).unwrap(); + s.execute("select.rect", json!({"x": 0, "y": 10, "width": 64, "height": 20, "antiAlias": false})).unwrap(); + s.execute("edit.fill", json!({"color": "#1a3a6a"})).unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + s.tools.background = [0.9, 0.2, 0.1, 1.0]; + assert_erases(&mut s, stroke(&DRAG, json!({"target": {"colors": [k]}})), &format!("{mode} component {k}")); + } + } +} diff --git a/crates/engine/src/cmd/eraser_cmds.rs b/crates/engine/src/cmd/eraser_cmds.rs index 2d08834f01..473699aa63 100644 --- a/crates/engine/src/cmd/eraser_cmds.rs +++ b/crates/engine/src/cmd/eraser_cmds.rs @@ -304,3 +304,7 @@ mod tests; #[cfg(test)] #[path = "background_eraser_transparency_lock_tests.rs"] mod transparency_lock_tests; + +#[cfg(test)] +#[path = "background_eraser_channel_target_tests.rs"] +mod channel_target_tests;