PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Channels: Cut with one channel targeted clears only that channel (#2698) (#2712)
Valentin Klein7dc8bbd84a12
diff --git a/crates/engine/src/channel_clip.rs b/crates/engine/src/channel_clip.rsindex a2213b454..cd226cb1c 100644--- a/crates/engine/src/channel_clip.rs+++ b/crates/engine/src/channel_clip.rs@@ -6,9 +6,11 @@//! active layer, instead of making a new layer. The other channels and the layer's transparency//! stay as they are. Pasting into an alpha channel, a layer mask or the Quick Mask goes through//! [`crate::edit_cmds::paste_to_target`].+//! - Cut with a colour channel targeted copies that channel and fills the cut area of that channel+//! only with the background colour (#2698).use photocraft_color::{ColorMode, PixelFormat};-use photocraft_doc::Layer;+use photocraft_doc::{Layer, LayerId};use photocraft_raster::Surface;use serde_json::{Value, json};@@ -113,6 +115,17 @@ pub(crate) fn paste(s: &mut Session, p: &Value, k: usize, in_place: bool, limit:Ok(json!({"layer": id.0, "channel": k, "offset": [dx, dy]}))}+/// Cut with colour channel `k` of layer `id` targeted (#2698): copy the channel as [`copy`] does,+/// then fill the selected area (or the canvas) of that channel only with the background colour's+/// value for it, as Photoshop does. The other channels and the layer's transparency stay as they+/// are. One history step.+pub(crate) fn cut(s: &mut Session, id: LayerId, k: usize) -> Result<Value> {+ let r = copy(s)?.ok_or_else(|| EngineError::Other("no colour channel is targeted".into()))?;+ let bg = s.tools.background;+ s.edit("Cut Pixels", |doc, _| crate::channel_cmds::clear_color_channel(doc, id, k, bg))?;+ Ok(r)+}+/// Copy reads only: with an alpha channel targeted it needs no pixel layer.pub(crate) fn copies_alpha_channel(s: &Session) -> bool {s.active().is_some_and(|st| matches!(st.channel_view.target, ChannelTarget::Alpha(i) if i < st.doc.channels.len()))diff --git a/crates/engine/src/channel_clip/tests.rs b/crates/engine/src/channel_clip/tests.rsindex 661728b58..e6fa57333 100644--- a/crates/engine/src/channel_clip/tests.rs+++ b/crates/engine/src/channel_clip/tests.rs@@ -128,3 +128,22 @@ fn copying_and_pasting_channels_fail_cleanly() {assert!(s.execute("edit.paste", json!({})).is_err());assert_eq!(px(&s, 5, 5), before);}++#[test]+fn cut_a_colour_channel_clears_only_that_channel() {+ let mut s = session(8);+ 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();+ s.execute("select.rect", json!({"x": 2, "y": 2, "width": 10, "height": 10})).unwrap();+ s.execute("edit.cut", json!({})).unwrap();+ let clip = s.clipboard.as_ref().unwrap();+ assert_eq!(clip.surface.format().mode, ColorMode::Grayscale, "the red channel copies as grayscale");+ assert!(near(&clip.surface.pixel(5, 5), &[left[0], 1.0]), "the clip holds the old red, {:?}", clip.surface.pixel(5, 5));+ // 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]), "{:?}", 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");+}diff --git a/crates/engine/src/edit_cmds.rs b/crates/engine/src/edit_cmds.rsindex 9116779f1..3eb1ce5f9 100644--- a/crates/engine/src/edit_cmds.rs+++ b/crates/engine/src/edit_cmds.rs@@ -559,10 +559,14 @@ pub fn specs() -> Vec<CommandSpec> {};}vec![- spec!("edit.cut", "Cut", &["Edit"], Some("Cmd+X"), "{}", has_pixels, |s, _| {+ spec!("edit.cut", "Cut", &["Edit"], Some("Cmd+X"), "{} (a targeted colour channel cuts that channel only)", has_pixels, |s, p| {// Refuse a locked layer before copying, so a refused Cut leaves the clipboard alone.let id = active_id(s)?;crate::commands::check_pixels_unlocked(&s.active().ok_or(EngineError::NoDocument)?.doc, id)?;+ // A targeted colour channel cuts that channel only (#2698).+ if let Some(k) = crate::channel_clip::paste_color_target(s, p) {+ return crate::channel_clip::cut(s, id, k);+ }let r = copy(s, false)?;let bg = s.tools.background;s.edit("Cut Pixels", |doc, _| clear_selected(doc, id, bg))?;