From af709cbe245d1d89c75d19948e282c09b679bf25 Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 09:20:25 +0100 Subject: [PATCH] Gradient: Behind on a mask or channel is an error (#12011) Masks, alpha channels and the Quick Mask have no transparency to paint behind, so paint.gradient in Behind changed nothing there and still reported success. It now refuses before the edit, as the issue asks where Behind has no meaning. Co-Authored-By: Claude Opus 5.5 --- .../src/cmd/gradient_behind_mode_tests.rs | 14 ++++++++++++++ crates/engine-paint/src/cmd/paint_cmds.rs | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/crates/engine-paint/src/cmd/gradient_behind_mode_tests.rs b/crates/engine-paint/src/cmd/gradient_behind_mode_tests.rs index fa79172f8d..62bb101a47 100644 --- a/crates/engine-paint/src/cmd/gradient_behind_mode_tests.rs +++ b/crates/engine-paint/src/cmd/gradient_behind_mode_tests.rs @@ -66,3 +66,17 @@ fn an_unknown_mode_is_an_error_naming_it_and_paints_nothing() { assert_eq!(rgba_at(&s, 550)[3], 0.0, "{mode}: nothing is painted"); } } + +#[test] +fn behind_on_a_mask_or_channel_is_an_error_and_records_nothing() { + let mut s = red_half(); + s.execute("layer.layerMask.revealAll", json!({})).unwrap(); + s.execute("channel.new", json!({})).unwrap(); + s.execute("select.editInQuickMaskMode", json!({"on": true})).unwrap(); + let steps = s.active().unwrap().history.entries().len(); + for target in [json!("mask"), json!("quickMask"), json!({"channel": 0})] { + let err = drag(&mut s, json!({"mode": "behind", "target": target})).expect_err("behind on a mask or channel"); + assert!(err.to_string().contains("behind"), "{target}: {err}"); + assert_eq!(s.active().unwrap().history.entries().len(), steps, "{target}: no History step"); + } +} diff --git a/crates/engine-paint/src/cmd/paint_cmds.rs b/crates/engine-paint/src/cmd/paint_cmds.rs index 97aa7d7a78..50639b6fce 100644 --- a/crates/engine-paint/src/cmd/paint_cmds.rs +++ b/crates/engine-paint/src/cmd/paint_cmds.rs @@ -200,6 +200,10 @@ fn gradient(s: &mut Session, p: &Value) -> Result { let reverse = b(p, "reverse", false); let opacity = opacity(p, "paint.gradient")?; let mode = fill_mode(p, "paint.gradient", false)?; + // Masks and channels have no transparency to paint behind: Behind would change nothing there. + if mode == FillMode::Behind && crate::channel_cmds::target_of(p) != crate::channel_cmds::Target::Pixels { + return Err(EngineError::BadParams { cmd: "paint.gradient".into(), msg: "mode `behind` paints only a layer's pixels, not a mask or channel".into() }); + } let dither = b(p, "dither", true); // Photoshop dithers gradients by default (reduces banding). let layer = if p.get("layer").is_some() { Some(crate::commands::layer_param(s, p)?) } else { s.active().and_then(|st| st.active_layer) }; s.edit(step_name(p), |doc, _| {