From d6b92af09152c15832f0650facf623e3169ee51b Mon Sep 17 00:00:00 2001 From: rmsff <17902130+rmsff@users.noreply.github.com> Date: Tue, 10 Nov 2026 17:14:39 +0300 Subject: [PATCH 1/2] Refuse every pattern with the Pattern Stamp in Indexed Color paint.patternStamp and its live strokes return the reference application's message in an Indexed Color document, whatever the pattern's colour mode, and paint nothing; a press with the Pattern Stamp shows it as a modal alert. The options bar's Mode reads Threshold there. Other documents keep painting. --- crates/engine/src/cmd/retouch_cmds.rs | 4 +++- .../src/cmd/retouch_cmds/pattern_stamp.rs | 24 +++++++++++++++++-- crates/ui-egui/src/panels/brush_controls.rs | 7 ++++-- crates/ui-egui/src/panels/options_bar.rs | 5 +++- crates/ui-egui/src/tools/brush_refusal.rs | 9 ++++++- 5 files changed, 42 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/cmd/retouch_cmds.rs b/crates/engine/src/cmd/retouch_cmds.rs index b4fcdd9022..4a9bf1c4c2 100644 --- a/crates/engine/src/cmd/retouch_cmds.rs +++ b/crates/engine/src/cmd/retouch_cmds.rs @@ -29,7 +29,9 @@ mod art_history; mod content_aware_move; mod patch; mod pattern_stamp; -pub use pattern_stamp::live_impressionist as live_pattern_impressionist; +pub use pattern_stamp::{ + INDEXED_REFUSAL as PATTERN_STAMP_INDEXED_REFUSAL, live_impressionist as live_pattern_impressionist, press_refusal as pattern_stamp_press_refusal, +}; mod periodic; mod remove; pub use content_aware_move::{ContentAwarePreview, preview as content_aware_move_preview}; diff --git a/crates/engine/src/cmd/retouch_cmds/pattern_stamp.rs b/crates/engine/src/cmd/retouch_cmds/pattern_stamp.rs index 8c05c99085..f5e4cb1e65 100644 --- a/crates/engine/src/cmd/retouch_cmds/pattern_stamp.rs +++ b/crates/engine/src/cmd/retouch_cmds/pattern_stamp.rs @@ -9,7 +9,7 @@ //! Impressionist was not measured: those strokes lay their dabs over the pixels in Normal, as //! they did before Behind existed. -use photocraft_color::{BlendMode, PixelFormat}; +use photocraft_color::{BlendMode, ColorMode, PixelFormat}; use photocraft_compose::pattern::{Placement, Tile, render}; use photocraft_doc::{LayerId, Pattern}; use photocraft_geom::Rect; @@ -19,11 +19,30 @@ use photocraft_raster::{Surface, from_rgba_into, to_rgba}; use serde_json::{Value, json}; use super::{bad, blend_param, damage_json, flag, has_pixel_layer, parse_brush, reject_huge_stroke}; -use crate::{Result, Session}; +use crate::{EngineError, Result, Session}; const STREAM_PHASE_X: u64 = 101; const STREAM_PHASE_Y: u64 = 102; +/// The reference's alert for a Pattern Stamp stroke in an Indexed Color document (#12887). +pub const INDEXED_REFUSAL: &str = "Could not fill because the pattern and image have different color modes."; + +/// The reference refuses every pattern in an Indexed Color document before painting: RGB, +/// Grayscale, CMYK, Lab and Indexed ones from another image. It paints only a pattern with the +/// document's own colour table, which PhotoCraft can't tell apart, so that one is refused too. +/// Bitmap, Duotone and Multichannel documents weren't measured and paint as before. +fn mode_refusal(s: &Session) -> Result<()> { + if s.active().is_some_and(|st| st.doc.mode == ColorMode::Indexed) { Err(EngineError::Other(INDEXED_REFUSAL.into())) } else { Ok(()) } +} + +/// [`mode_refusal`] for a press with the stroke's `p`, so the canvas can alert before the stroke +/// starts: `None` when the pattern paints, or when `p` names no pattern (the stroke reports that). +pub fn press_refusal(s: &Session, p: &Value) -> Option { + mode_refusal(s).err()?; + crate::pattern_cmds::resolve_param(s, "paint.patternStamp", p).ok()?; + Some(INDEXED_REFUSAL.into()) +} + pub(super) fn enabled(s: &Session) -> std::result::Result<(), String> { has_pixel_layer(s)?; if crate::pattern_cmds::resolve_id(s, "").is_none() { Err("no pattern selected".into()) } else { Ok(()) } @@ -58,6 +77,7 @@ impl Setup { let aligned = flag(p, "aligned", true); let impressionist = flag(p, "impressionist", false); let pat = crate::pattern_cmds::resolve_param(s, CMD, p)?; + mode_refusal(s)?; if pat.is_empty() { return Err(bad(CMD, "the pattern is empty")); } diff --git a/crates/ui-egui/src/panels/brush_controls.rs b/crates/ui-egui/src/panels/brush_controls.rs index 4f30ac1bdf..481b56b829 100644 --- a/crates/ui-egui/src/panels/brush_controls.rs +++ b/crates/ui-egui/src/panels/brush_controls.rs @@ -40,8 +40,11 @@ pub(super) fn paint_mode_dropdown(ui: &mut egui::Ui, id: &str, b: &mut photocraf /// picking another mode leaves it. The wheel steps it as it steps the other Mode dropdowns. /// Without `behind_enabled` (the Background is the active layer) Behind is greyed, as in the /// Shift+right-click menu, and the wheel and the keys step past it. -pub(super) fn pattern_stamp_mode_dropdown(ui: &mut egui::Ui, id: &str, b: &mut photocraft_engine::BrushSettings, behind_enabled: bool) { - let opts = crate::brush_mode_menu::pattern_stamp_choices(); +pub(super) fn pattern_stamp_mode_dropdown(ui: &mut egui::Ui, id: &str, b: &mut photocraft_engine::BrushSettings, behind_enabled: bool, threshold: bool) { + let mut opts = crate::brush_mode_menu::pattern_stamp_choices(); + if threshold && let Some(normal) = opts.iter_mut().find(|o| o.0 == (Some(BlendMode::Normal), false)) { + normal.1 = tl!("Threshold"); + } let before = if b.behind { (None, true) } else { (Some(b.mode), false) }; let mut mode = before; let groups = |m: &(Option, bool)| m.0.is_some_and(starts_blend_group); diff --git a/crates/ui-egui/src/panels/options_bar.rs b/crates/ui-egui/src/panels/options_bar.rs index 895e6b0a02..3f8ab2f446 100644 --- a/crates/ui-egui/src/panels/options_bar.rs +++ b/crates/ui-egui/src/panels/options_bar.rs @@ -148,7 +148,10 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { // Its list has Behind, greyed on the Background (#12879). opt_label(ui, tl!("Mode")); let behind = crate::brush_mode_menu::enabled(app, crate::brush_mode_menu::Item::Behind); - super::brush_controls::pattern_stamp_mode_dropdown(ui, "retouch-mode", &mut brush, behind); + // An Indexed Color document names Normal "Threshold" (#12887); the other + // entries weren't measured there. + let threshold = app.session.active().is_some_and(|st| st.doc.mode == photocraft_doc::ColorMode::Indexed); + super::brush_controls::pattern_stamp_mode_dropdown(ui, "retouch-mode", &mut brush, behind, threshold); } else if let Some(modes) = crate::blend_keys::tool_modes(tool) { opt_label(ui, tl!("Mode")); if let Some(Some(m)) = diff --git a/crates/ui-egui/src/tools/brush_refusal.rs b/crates/ui-egui/src/tools/brush_refusal.rs index e75c60794d..25490cefa4 100644 --- a/crates/ui-egui/src/tools/brush_refusal.rs +++ b/crates/ui-egui/src/tools/brush_refusal.rs @@ -46,8 +46,15 @@ pub fn tool_name(app: &PhotocraftApp, tool: Tool) -> Option<&'static str> { /// The alert a press with `tool` gets, if any. `hidden` is the press-time hidden-target refusal /// (`canvas::tool_input`), which this only words; the lock and the group are checked here. Lock /// transparent pixels is no refusal, and a group's targeted mask paints. A tool with a -/// [`mode_tool_name`] gets its colour-mode alert first, in an Indexed Color or Bitmap document. +/// [`mode_tool_name`] gets its colour-mode alert first, in an Indexed Color or Bitmap document; the +/// Pattern Stamp only the engine's, for any pattern in an Indexed Color document. pub fn alert(app: &PhotocraftApp, tool: Tool, hidden: bool) -> Option { + // The Pattern Stamp's colour-mode alert depends on its pattern too (#12887). + if tool == Tool::PatternStamp { + use photocraft_engine::retouch_cmds::{PATTERN_STAMP_INDEXED_REFUSAL, pattern_stamp_press_refusal}; + let p = crate::retouch_ui::pattern_stamp_params(app, &[]); + return pattern_stamp_press_refusal(&app.session, &p).is_some().then(|| tl!(PATTERN_STAMP_INDEXED_REFUSAL).to_owned()); + } if let Some(name) = mode_tool_name(tool) && let Some(template) = match app.session.active()?.doc.mode { photocraft_doc::ColorMode::Indexed => Some(INDEXED), From be5778c4f87fb62b7df3b0ef3e43b34add7001df Mon Sep 17 00:00:00 2001 From: rmsff <17902130+rmsff@users.noreply.github.com> Date: Tue, 10 Nov 2026 17:14:39 +0300 Subject: [PATCH 2/2] Test the Pattern Stamp's Indexed Color refusal --- .../src/cmd/retouch_cmds/pattern_stamp.rs | 4 + .../pattern_stamp_indexed_tests.rs | 96 +++++++++++++++++++ .../src/options_bar_mode_wheel_tests.rs | 21 ++++ 3 files changed, 121 insertions(+) create mode 100644 crates/engine/src/cmd/retouch_cmds/pattern_stamp_indexed_tests.rs diff --git a/crates/engine/src/cmd/retouch_cmds/pattern_stamp.rs b/crates/engine/src/cmd/retouch_cmds/pattern_stamp.rs index f5e4cb1e65..c01ca41a9b 100644 --- a/crates/engine/src/cmd/retouch_cmds/pattern_stamp.rs +++ b/crates/engine/src/cmd/retouch_cmds/pattern_stamp.rs @@ -386,3 +386,7 @@ mod tests { assert!(reject_huge_stroke("t", &stroke(&[(0.0, 0.0), (9000.0, 9000.0)], 100.0)).is_err()); } } + +#[cfg(test)] +#[path = "pattern_stamp_indexed_tests.rs"] +mod pattern_stamp_indexed_tests; diff --git a/crates/engine/src/cmd/retouch_cmds/pattern_stamp_indexed_tests.rs b/crates/engine/src/cmd/retouch_cmds/pattern_stamp_indexed_tests.rs new file mode 100644 index 0000000000..6fb7adc4c0 --- /dev/null +++ b/crates/engine/src/cmd/retouch_cmds/pattern_stamp_indexed_tests.rs @@ -0,0 +1,96 @@ +//! #12887: in an Indexed Color document the Pattern Stamp refuses every pattern (RGB, Grayscale, +//! CMYK, Lab, or one defined from an Indexed image) with the reference application's message, +//! painting nothing and adding no history; the live strokes are refused the same way. RGB, +//! Grayscale, CMYK and Lab documents at every depth keep painting with it. + +use super::super::LiveRetouch; +use super::*; + +/// A new `mode` document of `depth` bits, 64 × 64, white. +fn new_doc(s: &mut Session, mode: &str, depth: u64) { + s.execute("file.new", json!({"width": 64, "height": 64, "depth": depth, "mode": mode})).unwrap(); +} + +/// The reference's test pattern: 20 × 20 with red, green, blue and white quadrants, defined from a +/// `mode` document. Returns its id. +fn quadrants(s: &mut Session, mode: &str) -> String { + s.execute("file.new", json!({"width": 20, "height": 20, "depth": 8, "mode": mode})).unwrap(); + for (x, y, color) in [(0, 0, "#ff0000"), (10, 0, "#00ff00"), (0, 10, "#0000ff")] { + s.execute("select.rect", json!({"x": x, "y": y, "width": 10, "height": 10})).unwrap(); + s.execute("edit.fill", json!({"color": color})).unwrap(); + } + s.execute("select.deselect", json!({})).unwrap(); + let r = s.execute("edit.definePattern", json!({"name": format!("Quadrants {mode}")})).unwrap(); + r["pattern"].as_str().unwrap().to_string() +} + +fn to_indexed(s: &mut Session) { + s.execute("image.mode.indexedColor", json!({"palette": "exact", "forced": "none", "dither": "none"})).unwrap(); +} + +fn stroke(pattern: &str, impressionist: bool) -> Value { + json!({"points": [[12, 32], [52, 32]], "size": 30, "hardness": 100, "pattern": pattern, "impressionist": impressionist}) +} + +/// The active layer's pixels and the History length. +fn state(s: &Session) -> (Vec<[f32; 4]>, usize) { + let st = s.active().unwrap(); + let surf = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); + ((0..64).flat_map(|y| (0..64).map(move |x| surf.rgba(x, y))).collect(), st.history.past_len()) +} + +#[test] +fn an_rgb_pattern_is_refused_in_an_indexed_document_with_the_reference_words() { + let mut s = Session::new(); + let pat = quadrants(&mut s, "rgb"); + new_doc(&mut s, "rgb", 8); + to_indexed(&mut s); + let before = state(&s); + for impressionist in [false, true] { + let err = s.execute("paint.patternStamp", stroke(&pat, impressionist)).unwrap_err().to_string(); + assert_eq!(err, INDEXED_REFUSAL, "impressionist {impressionist}"); + assert_eq!(state(&s), before, "impressionist {impressionist}: nothing painted, no history"); + } + // The press check the canvas alerts with, and the live strokes, which preview nothing. + assert_eq!(press_refusal(&s, &stroke(&pat, false)).as_deref(), Some(INDEXED_REFUSAL)); + let live = LiveRetouch::begin(&s, "paint.patternStamp", &stroke(&pat, false)).err().map(|e| e.to_string()); + assert_eq!(live.as_deref(), Some(INDEXED_REFUSAL), "live"); + let dabs = live_impressionist(&s, &stroke(&pat, true)).err().map(|e| e.to_string()); + assert_eq!(dabs.as_deref(), Some(INDEXED_REFUSAL), "live Impressionist"); +} + +#[test] +fn other_documents_keep_painting_with_the_rgb_pattern() { + let mut s = Session::new(); + let pat = quadrants(&mut s, "rgb"); + for mode in ["rgb", "grayscale", "cmyk", "lab"] { + for depth in [8, 16, 32] { + new_doc(&mut s, mode, depth); + let (pixels, steps) = state(&s); + assert_eq!(press_refusal(&s, &stroke(&pat, false)), None, "{mode}/{depth}"); + s.execute("paint.patternStamp", stroke(&pat, false)).unwrap_or_else(|e| panic!("{mode}/{depth}: {e}")); + let after = state(&s); + assert_ne!(after.0, pixels, "{mode}/{depth}: painted"); + assert_eq!(after.1, steps + 1, "{mode}/{depth}: one history step"); + } + } +} + +#[test] +fn grayscale_cmyk_lab_and_indexed_patterns_are_refused_in_an_indexed_document_too() { + let mut s = Session::new(); + let mut patterns = ["grayscale", "cmyk", "lab"].map(|mode| quadrants(&mut s, mode)).to_vec(); + // A pattern defined from an Indexed Color image. + s.execute("file.new", json!({"width": 20, "height": 20, "depth": 8, "mode": "rgb"})).unwrap(); + to_indexed(&mut s); + patterns.push(s.execute("edit.definePattern", json!({"name": "Indexed"})).unwrap()["pattern"].as_str().unwrap().to_string()); + new_doc(&mut s, "rgb", 8); + to_indexed(&mut s); + let before = state(&s); + for pat in patterns { + let err = s.execute("paint.patternStamp", stroke(&pat, false)).unwrap_err().to_string(); + assert_eq!(err, INDEXED_REFUSAL, "{pat}"); + assert_eq!(press_refusal(&s, &stroke(&pat, false)).as_deref(), Some(INDEXED_REFUSAL), "{pat}"); + assert_eq!(state(&s), before, "{pat}: nothing painted, no history"); + } +} diff --git a/crates/ui-egui/src/options_bar_mode_wheel_tests.rs b/crates/ui-egui/src/options_bar_mode_wheel_tests.rs index 7870917ded..cb72244fad 100644 --- a/crates/ui-egui/src/options_bar_mode_wheel_tests.rs +++ b/crates/ui-egui/src/options_bar_mode_wheel_tests.rs @@ -127,6 +127,27 @@ fn the_pattern_stamp_mode_dropdown_has_behind() { assert_eq!(h.state().ui.tool, Tool::PatternStamp); } +/// The centre of a dropdown showing `value`. +fn combo(h: &Harness<'_, PhotocraftApp>, value: &str) -> Option { + h.query_all_by_role(Role::ComboBox).find(|n| n.value().as_deref() == Some(value)).map(|n| n.rect().center()) +} + +/// In an Indexed Color document the Pattern Stamp's Mode reads "Threshold" for Normal (#12887); the +/// other entries weren't measured there, so the list and the wheel stay as today. +#[test] +fn the_pattern_stamp_mode_reads_threshold_in_an_indexed_document() { + let mut h = harness(Tool::PatternStamp); + h.state_mut().run("image.mode.indexedColor", json!({"palette": "exact", "forced": "none", "dither": "none"})).unwrap(); + h.run_steps(4); + let at = combo(&h, "Threshold").expect("Mode reads Threshold"); + wheel(&mut h, at, -1.0); + assert_eq!(brush_mode(&h), BlendMode::Dissolve); + assert!(combo(&h, "Dissolve").is_some() && combo(&h, "Threshold").is_none(), "Dissolve reads as itself"); + wheel(&mut h, at, 1.0); + assert_eq!(brush_mode(&h), BlendMode::Normal); + assert!(combo(&h, "Threshold").is_some()); +} + /// The Paint Bucket's Mode dropdown has Clear after Dissolve, as the Brush's does. #[test] fn the_paint_bucket_mode_dropdown_has_clear() {