Channels: Delete with one channel targeted clears only that channel (#2720) (#2730)

Edit > Clear ignored the Channels panel target: with a single colour channel
targeted it filled every channel of the Background with the background colour,
and made a normal layer's pixels transparent. It now sets only the targeted
channel inside the selection to the background colour's value for that channel
(partly selected pixels blend), keeps the other channels and the layer's
transparency, and stays one undo step, as Photoshop does.

Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Valentin KleinandClaude Opus 5.5 authored and GitHub committed 2026-10-10 15:48:01 -04:00
1 parent e57ee674ae
commit db7a7885f4
3 files changed
+71 -1

No files matched your search

+39
View File
@@ -647,6 +647,45 @@ pub(crate) fn restrict_to_color(before: &Document, after: &mut Document, id: Lay
new.prune();
}
/// The colour channel Clear is limited to: the Channels panel targets a single one.
pub(crate) fn targeted_color(s: &Session) -> Option<usize> {
match s.active()?.channel_view.target {
ChannelTarget::Color(k) => Some(k),
_ => None,
}
}
/// Clear (Delete) with colour channel `k` of layer `id` targeted (#2720): fill the selected area
/// (or the canvas) of that channel only with the background colour's value for it, as Photoshop
/// does. A channel has no transparency of its own, so the other channels and the layer's alpha
/// stay; partly selected pixels blend.
pub(crate) fn clear_color_channel(doc: &mut Document, id: LayerId, k: usize, background: [f32; 4]) -> Result<()> {
let canvas = doc.bounds();
let sel = doc.selection.clone();
let area = sel.as_ref().map_or(canvas, |m| m.content_bounds().intersect(&canvas));
let surf = crate::commands::paint_surface(doc, id, &Value::Null)?;
let fmt = surf.format();
let n = fmt.channels();
// The background colour in the layer's colour model, so `k` picks the right component.
let Some(v) = from_rgba(&fmt, [background[0], background[1], background[2], 1.0]).get(k).copied().filter(|_| k < n) else {
return Err(EngineError::Other(format!("no colour channel {k} in this layer")));
};
if area.is_empty() {
return Ok(());
}
let cover = sel.map(|m| (m.read_region(area), m.format().channels().max(1)));
let mut px = surf.read_region(area);
for (i, p) in px.chunks_exact_mut(n).enumerate() {
let w = cover.as_ref().map_or(1.0, |(m, mk)| m.get(i * mk).copied().unwrap_or(0.0));
if let Some(c) = p.get_mut(k) {
*c += (v - *c) * w.clamp(0.0, 1.0);
}
}
surf.write_region(area, &px);
surf.prune();
Ok(())
}
/// The active document targets an alpha channel or is in Quick Mask mode, so pixel commands
/// work without a pixel layer.
pub(crate) fn edits_channel(s: &Session) -> bool {
+26
View File
@@ -521,3 +521,29 @@ fn apply_image_and_calculations_fail_gracefully_never_panic() {
// A well-formed self-apply still works.
assert!(s.execute("image.applyImage", json!({"source": {"channel": "composite"}})).is_ok());
}
#[test]
fn clear_with_a_colour_channel_targeted_clears_only_that_channel() {
let near = |a: &[f32], b: &[f32]| a.len() == b.len() && a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-3);
// The Background, then a normal layer (Delete there can't make one channel transparent).
for duplicate in [false, true] {
let mut s = session_depth(16);
rect(&mut s, 0, 0, 20, 20, "replace");
s.execute("edit.fill", json!({"color": "#336699"})).unwrap();
s.execute("select.deselect", json!({})).unwrap();
if duplicate {
s.execute("layer.duplicate", json!({})).unwrap();
}
let (left, right) = (px(&s, 5, 5), px(&s, 30, 5));
s.tools.background = [0.8, 0.1, 0.9, 1.0];
s.execute("channel.target", json!({"channel": "red"})).unwrap();
rect(&mut s, 2, 2, 10, 10, "replace");
s.execute("edit.clear", json!({})).unwrap();
// Red takes the background colour's red; green, blue and transparency stay.
assert!(near(&px(&s, 5, 5), &[0.8, left[1], left[2], 1.0]), "duplicate {duplicate}: {:?}", px(&s, 5, 5));
assert!(near(&px(&s, 15, 5), &left), "outside the selection");
assert!(near(&px(&s, 30, 5), &right));
s.undo();
assert!(near(&px(&s, 5, 5), &left), "one undoable step");
}
}
+6 -1
View File
@@ -397,9 +397,14 @@ fn build() -> Vec<CommandSpec> {
has_pixel_or_channel,
crate::fill_cmds::fill
),
cmd!("edit.clear", "Clear", ["Edit"], Some("Delete"), "{}", has_pixel_layer, |s, p| {
cmd!("edit.clear", "Clear", ["Edit"], Some("Delete"), "{} (a targeted colour channel clears that channel only)", has_pixel_layer, |s, p| {
let id = layer_param(s, p)?;
let bg = s.tools.background;
// A targeted colour channel clears that channel only (#2720).
if let Some(k) = crate::channel_cmds::targeted_color(s) {
s.edit("Clear", |doc, _| crate::channel_cmds::clear_color_channel(doc, id, k, bg))?;
return Ok(Value::Null);
}
s.edit("Clear", |doc, _| {
let sel = doc.selection.clone();
let area = sel.as_ref().map(|m| m.content_bounds()).unwrap_or(doc.bounds());