diff --git a/crates/engine-core/src/hidden_target.rs b/crates/engine-core/src/hidden_target.rs index a6f1ba5d8a..8228621c3a 100644 --- a/crates/engine-core/src/hidden_target.rs +++ b/crates/engine-core/src/hidden_target.rs @@ -21,6 +21,9 @@ pub const COLOR_REPLACEMENT_MASK: &str = "Could not use the Color Replacement to pub const COLOR_REPLACEMENT_ADJUSTMENT: &str = "Could not use the Color Replacement tool because the target is an adjustment layer."; pub const COLOR_REPLACEMENT_GROUP: &str = "Could not use the Color Replacement tool because the target is a group."; pub const COLOR_REPLACEMENT_ALPHA: &str = "Could not use the Color Replacement tool because it does not work with alpha channels."; +/// Its alert for some but not all colour channels targeted (#17783); one channel alone gets +/// [`COLOR_REPLACEMENT_MASK`]. +pub const COLOR_REPLACEMENT_COMPOSITE: &str = "Could not use the Color Replacement tool because the target channels do not cover the composite."; /// Commands that paint on, or move, the target layer. const GATED: &[&str] = &[ @@ -75,7 +78,8 @@ pub fn refusal(s: &Session, id: &str, params: &Value) -> Option<&'static str> { } /// Why `paint.colorReplacement` with `params` (its `target` filled in) can't recolour its target -/// (#17782): the tool works on a pixel layer's full colour composite only. The target decides +/// (#17782): the tool works on a pixel layer's full colour composite only, not on some of its +/// colour channels (#17783). The target decides /// before the colour mode, depth, hidden and locked checks. Measured in RGB documents only: other /// modes, a smart object's filter mask and the masks of type, shape, fill and smart object layers /// keep the command's generic refusal. @@ -95,6 +99,16 @@ pub fn color_replacement_target(s: &Session, params: &Value) -> Option<&'static (photocraft_doc::LayerContent::Adjustment(_), Target::Mask | Target::Pixels) => Some(COLOR_REPLACEMENT_ADJUSTMENT), (photocraft_doc::LayerContent::Group(_), Target::Mask) => Some(COLOR_REPLACEMENT_GROUP), (photocraft_doc::LayerContent::Raster(_), Target::Mask) => Some(COLOR_REPLACEMENT_MASK), + // Some colour channels of a pixel layer, not all (#17783): one alone is "full color modes", + // two the composite they don't cover. All of them are the composite and paint. + (photocraft_doc::LayerContent::Raster(_), Target::Pixels) => { + let colors = crate::color_channels::target(s, params).ok()??; + (colors.len() < crate::channel_cmds::color_count(&st.doc)).then_some(if colors.len() == 1 { + COLOR_REPLACEMENT_MASK + } else { + COLOR_REPLACEMENT_COMPOSITE + }) + } _ => None, } } diff --git a/crates/engine-paint/src/cmd/brush_cmds.rs b/crates/engine-paint/src/cmd/brush_cmds.rs index 96f20dd142..715b533127 100644 --- a/crates/engine-paint/src/cmd/brush_cmds.rs +++ b/crates/engine-paint/src/cmd/brush_cmds.rs @@ -956,7 +956,7 @@ fn mixer_load_pixel(s: &mut Session, p: &Value) -> Result { fn color_replacement_setup(s: &Session, p: &Value) -> Result<(Stroke, ReplaceSettings, photocraft_doc::LayerId)> { let cmd = "paint.colorReplacement"; // A mask, adjustment layer, group mask or channel target is refused first, with the - // reference application's words (#17782). + // reference application's words (#17782), as are some but not all colour channels (#17783). if let Some(why) = photocraft_engine_core::hidden_target::color_replacement_target(s, p) { return Err(EngineError::Other(why.into())); } @@ -1686,6 +1686,10 @@ mod mixer_live_tests; #[path = "color_replacement_history_name_tests.rs"] mod color_replacement_history_name_tests; +#[cfg(test)] +#[path = "color_replacement_channel_target_tests.rs"] +mod color_replacement_channel_target_tests; + #[cfg(test)] mod define_brush_merged_source_tests; diff --git a/crates/engine-paint/src/cmd/color_replacement_channel_target_tests.rs b/crates/engine-paint/src/cmd/color_replacement_channel_target_tests.rs new file mode 100644 index 0000000000..f10ae5153c --- /dev/null +++ b/crates/engine-paint/src/cmd/color_replacement_channel_target_tests.rs @@ -0,0 +1,95 @@ +//! #17783: Color Replacement recolours the full colour composite only. One colour channel +//! targeted is refused with "it only works in full color modes", some but not all with "the +//! target channels do not cover the composite": nothing painted, no History state, and the live +//! stroke previews nothing. All colour channels paint as before; other colour modes weren't +//! measured and keep painting. + +use photocraft_color::{Color, ColorMode, SampleType}; +use photocraft_doc::{Document, Size}; +use photocraft_engine_core::hidden_target::{COLOR_REPLACEMENT_COMPOSITE, COLOR_REPLACEMENT_MASK}; +use serde_json::json; + +use super::*; + +/// A red 8-bit document of `mode`, blue as the foreground. +fn session_in(mode: ColorMode) -> Session { + let mut s = crate::session(); + s.add_document(Document::with_background("t", Size::new(64, 64), mode, SampleType::U8, Color::rgb(220.0 / 255.0, 30.0 / 255.0, 30.0 / 255.0)), None); + s.tools.foreground = [0.0, 0.0, 1.0, 1.0]; + s +} + +fn session() -> Session { + session_in(ColorMode::Rgb) +} + +fn stroke(target: Option) -> Value { + let mut p = json!({"points": [[20, 32], [44, 32]], "size": 15, "tolerance": 100}); + if let Some(t) = target { + p["target"] = t; + } + p +} + +fn snapshot(s: &Session) -> (usize, Document) { + let st = s.active().unwrap(); + (st.history.past_len(), (*st.doc).clone()) +} + +/// The error the stroke (and its live preview) gives; the document and History stay as they were. +fn refused(s: &mut Session, p: Value) -> String { + let before = snapshot(s); + assert!(live_color_replacement(s, &p).is_err(), "the live stroke previews nothing"); + let err = s.execute("paint.colorReplacement", p).unwrap_err().to_string(); + assert!(snapshot(s) == before, "nothing painted, no History state"); + err +} + +fn paints(s: &mut Session, p: Value) { + let (n, doc) = snapshot(s); + s.execute("paint.colorReplacement", p.clone()).unwrap_or_else(|e| panic!("{p}: {e}")); + let (m, after) = snapshot(s); + assert_eq!(m, n + 1, "{p}: one History state"); + assert!(after != doc, "{p}: the layer changed"); +} + +#[test] +fn one_colour_channel_only_works_in_full_color_modes() { + for target in [json!({"color": 0}), json!({"color": 2}), json!({"colors": [1]})] { + assert_eq!(refused(&mut session(), stroke(Some(target.clone()))), COLOR_REPLACEMENT_MASK, "{target}"); + } +} + +#[test] +fn two_colour_channels_do_not_cover_the_composite() { + for colors in [json!([0, 1]), json!([1, 2]), json!([0, 2])] { + assert_eq!(refused(&mut session(), stroke(Some(json!({"colors": colors})))), COLOR_REPLACEMENT_COMPOSITE, "{colors}"); + } +} + +/// The canvas sends the Channels panel's selection as `target`; without one the command reads the +/// selection itself, with the same refusals. +#[test] +fn the_channels_panel_selection_is_refused_the_same() { + for (colors, why) in [(json!([0]), COLOR_REPLACEMENT_MASK), (json!([0, 1]), COLOR_REPLACEMENT_COMPOSITE)] { + let mut s = session(); + s.execute("channel.target", json!({"colors": colors})).unwrap(); + assert_eq!(refused(&mut s, stroke(None)), why, "{colors}"); + } +} + +#[test] +fn every_colour_channel_is_the_composite_and_paints() { + for target in [Some(json!({"colors": [0, 1, 2]})), Some(json!("pixels")), None] { + paints(&mut session(), stroke(target)); + } + let mut s = session(); + s.execute("channel.target", json!({"colors": [0, 1, 2]})).unwrap(); + paints(&mut s, stroke(None)); +} + +/// Other colour modes weren't measured: one CMYK channel keeps painting. +#[test] +fn a_cmyk_channel_keeps_painting() { + paints(&mut session_in(ColorMode::Cmyk), stroke(Some(json!({"color": 0})))); +} diff --git a/crates/engine/tests/it/multi_channel_editing.rs b/crates/engine/tests/it/multi_channel_editing.rs index 6c6207cc67..c8c01f1598 100644 --- a/crates/engine/tests/it/multi_channel_editing.rs +++ b/crates/engine/tests/it/multi_channel_editing.rs @@ -697,11 +697,14 @@ fn first_single_channel_retouch_preview_preserves_every_other_sample() { s.execute("channel.target", json!({"channel":{"color":0}})).unwrap(); let before = surface(&s); let p = json!({"points":[[6,7,1],[23,15,0.7]],"size":9,"color":"#ffffff","tolerance":100}); - let live = if cmd == "paint.colorReplacement" { - photocraft_engine::brush_cmds::live_color_replacement(&s, &p).unwrap() - } else { - photocraft_engine::retouch_cmds::LiveDab::begin(&s, cmd, &p).unwrap() - }; + // Color Replacement refuses one colour channel, preview and commit alike (#17783). + if cmd == "paint.colorReplacement" { + assert!(photocraft_engine::brush_cmds::live_color_replacement(&s, &p).is_err()); + assert!(s.execute(cmd, p).is_err()); + assert_eq!(surface(&s), before); + continue; + } + let live = photocraft_engine::retouch_cmds::LiveDab::begin(&s, cmd, &p).unwrap(); let shown = live.doc.layer(s.active().unwrap().active_layer.unwrap()).unwrap().surface().unwrap(); for y in 0..24 { for x in 0..32 { @@ -710,9 +713,7 @@ fn first_single_channel_retouch_preview_preserves_every_other_sample() { } } } - if cmd != "paint.colorReplacement" { - s.offer_live_dab(live).unwrap(); - } + s.offer_live_dab(live).unwrap(); s.execute(cmd, p).unwrap(); } } @@ -920,18 +921,23 @@ fn rgb_component_exposure_uses_the_document_transfer_at_all_depths() { } #[test] -fn color_replacement_live_preview_forwards_explicit_component_and_pixel_targets() { - for target in [json!({"colors":[1]}), json!("pixels")] { - // 16-bit: Color Replacement refuses 32-bit RGB (#11923). - let mut s = flat_rgb_at(16); - subset(&mut s); - let params = json!({"points":[[8,8],[22,12]],"size":9,"tolerance":100,"mode":"luminosity","color":"#ffffff","seed":42,"target":target}); - let live = photocraft_engine::brush_cmds::live_color_replacement(&s, ¶ms).unwrap(); - let id = s.active().unwrap().active_layer.unwrap(); - let preview = live.doc.layer(id).unwrap().surface().unwrap().clone(); - s.execute("paint.colorReplacement", params).unwrap(); - assert_eq!(surface(&s), preview); - } +fn color_replacement_live_preview_forwards_pixel_targets_and_refuses_single_components() { + // 16-bit: Color Replacement refuses 32-bit RGB (#11923). + let mut s = flat_rgb_at(16); + subset(&mut s); + let params = |target| json!({"points":[[8,8],[22,12]],"size":9,"tolerance":100,"mode":"luminosity","color":"#ffffff","seed":42,"target":target}); + // One colour component is refused, preview and commit alike (#17783). + let one = params(json!({"colors":[1]})); + let before = surface(&s); + assert!(photocraft_engine::brush_cmds::live_color_replacement(&s, &one).is_err()); + assert!(s.execute("paint.colorReplacement", one).is_err()); + assert_eq!(surface(&s), before); + let pixels = params(json!("pixels")); + let live = photocraft_engine::brush_cmds::live_color_replacement(&s, &pixels).unwrap(); + let id = s.active().unwrap().active_layer.unwrap(); + let preview = live.doc.layer(id).unwrap().surface().unwrap().clone(); + s.execute("paint.colorReplacement", pixels).unwrap(); + assert_eq!(surface(&s), preview); } #[test]