diff --git a/crates/engine/src/cmd/retouch_cmds/dab.rs b/crates/engine/src/cmd/retouch_cmds/dab.rs index a950ad5992..49b0531cc4 100644 --- a/crates/engine/src/cmd/retouch_cmds/dab.rs +++ b/crates/engine/src/cmd/retouch_cmds/dab.rs @@ -265,9 +265,26 @@ impl DabTool { } } +/// The reference application refuses a Dodge stroke in Indexed Color and Bitmap documents, with +/// its alert's words (#12686); other modes (Multichannel and Duotone were not measured) tone as +/// before. The live stroke checks this too, so it previews nothing there. +fn mode_refusal(s: &Session, cmd: &str) -> Result<()> { + let tool = match cmd { + "paint.dodge" => "dodge tool", + _ => return Ok(()), + }; + let why = match s.active().map(|st| st.doc.mode) { + Some(photocraft_color::ColorMode::Indexed) => "it does not work with Indexed Color images (convert image to RGB Color mode to edit).", + Some(photocraft_color::ColorMode::Bitmap) => "it does not work with Bitmap images (convert image to Grayscale mode to edit).", + _ => return Ok(()), + }; + Err(EngineError::Other(format!("Could not use the {tool} because {why}"))) +} + /// Dodge, Burn, Sponge, Blur, Sharpen and Smudge: each dab edits a working copy in order /// (`apply_dab_stroke`). Blur, Sharpen and Smudge can sample all layers. pub(super) fn dab_cmd(s: &mut Session, p: &Value, cmd: &str) -> Result { + mode_refusal(s, cmd)?; let (stroke, id) = parse_brush(s, p, cmd)?; let (tool, label) = DabTool::parse(s, cmd, p, &stroke)?; let all = matches!(tool, DabTool::Focus { .. } | DabTool::Smudge { .. }) && sample_all_layers(p); @@ -418,6 +435,7 @@ pub struct LiveDab { impl LiveDab { /// Start a live stroke of `cmd` from its params; their `points` are rendered. pub fn begin(s: &Session, cmd: &str, p: &Value) -> Result { + mode_refusal(s, cmd)?; has_pixel_layer(s).map_err(EngineError::Other)?; if sample_all_layers(p) { return Err(bad(cmd, "Sample All Layers strokes aren't previewed live")); @@ -673,3 +691,7 @@ impl LiveDab { }) } } + +#[cfg(test)] +#[path = "dodge_refuses_indexed_tests.rs"] +mod dodge_refuses_indexed_tests; diff --git a/crates/engine/src/cmd/retouch_cmds/dodge_refuses_indexed_tests.rs b/crates/engine/src/cmd/retouch_cmds/dodge_refuses_indexed_tests.rs new file mode 100644 index 0000000000..a4da05e112 --- /dev/null +++ b/crates/engine/src/cmd/retouch_cmds/dodge_refuses_indexed_tests.rs @@ -0,0 +1,68 @@ +//! #12686: the Dodge tool refuses Indexed Color and Bitmap documents with the reference +//! application's messages, toning nothing and adding no history; the live stroke is refused the +//! same way. RGB, Grayscale, CMYK and Lab at every depth, Multichannel and Duotone keep dodging. + +use photocraft_color::{Color, ColorMode, SampleType}; +use photocraft_doc::{Document, Size}; +use serde_json::json; + +use super::*; + +const INDEXED: &str = "Could not use the dodge tool because it does not work with Indexed Color images (convert image to RGB Color mode to edit)."; +const BITMAP: &str = "Could not use the dodge tool because it does not work with Bitmap images (convert image to Grayscale mode to edit)."; + +/// A session with one 64 × 64 document of `mode` and `depth`, filled mid grey. +fn session(mode: ColorMode, depth: SampleType) -> Session { + let mut s = Session::new(); + s.add_document(Document::with_background("t", Size::new(64, 64), mode, depth, Color::rgb(0.5, 0.5, 0.5)), None); + s +} + +fn stroke() -> Value { + json!({"points": [[20, 32], [44, 32]], "size": 20, "hardness": 100, "range": "midtones", "exposure": 100}) +} + +fn steps(s: &Session) -> usize { + s.active().unwrap().history.past_len() +} + +fn pixels(s: &Session) -> Vec { + let st = s.active().unwrap(); + let surf = st.doc.layers[0].surface().unwrap(); + (0..64).flat_map(|y| (0..64).flat_map(move |x| surf.pixel(x, y))).collect() +} + +#[test] +fn indexed_and_bitmap_documents_refuse_with_the_reference_words() { + for (mode, want) in [(ColorMode::Indexed, INDEXED), (ColorMode::Bitmap, BITMAP)] { + let mut s = session(mode, SampleType::U8); + let (before, n) = (pixels(&s), steps(&s)); + let err = s.execute("paint.dodge", stroke()).unwrap_err().to_string(); + assert_eq!(err, want, "{mode:?}"); + assert_eq!(pixels(&s), before, "{mode:?}: nothing toned"); + assert_eq!(steps(&s), n, "{mode:?}: no history"); + // The live stroke previews nothing either. + let live = LiveDab::begin(&s, "paint.dodge", &stroke()).err().map(|e| e.to_string()); + assert_eq!(live.as_deref(), Some(want), "{mode:?}: live"); + } +} + +#[test] +fn other_modes_keep_dodging_at_every_depth() { + let mut cases = Vec::new(); + for mode in [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Cmyk, ColorMode::Lab] { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + cases.push((mode, depth)); + } + } + // Not measured in the reference: they keep today's behaviour. + cases.extend([(ColorMode::Multichannel, SampleType::U8), (ColorMode::Duotone, SampleType::U8)]); + for (mode, depth) in cases { + let mut s = session(mode, depth); + let (before, n) = (pixels(&s), steps(&s)); + s.execute("paint.dodge", stroke()).unwrap_or_else(|e| panic!("{mode:?}/{depth:?}: {e}")); + assert_ne!(pixels(&s), before, "{mode:?}/{depth:?}: dodged"); + assert_eq!(steps(&s), n + 1, "{mode:?}/{depth:?}: one history step"); + assert!(LiveDab::begin(&s, "paint.dodge", &stroke()).is_ok(), "{mode:?}/{depth:?}: live"); + } +} diff --git a/crates/ui-egui/src/tools/brush_refusal.rs b/crates/ui-egui/src/tools/brush_refusal.rs index c1919c8639..f1e202635d 100644 --- a/crates/ui-egui/src/tools/brush_refusal.rs +++ b/crates/ui-egui/src/tools/brush_refusal.rs @@ -13,6 +13,20 @@ pub const HIDDEN: &str = "Could not use the {tool} because the target layer is h pub const LOCKED: &str = "Could not use the {tool} because the layer is locked."; pub const GROUP: &str = "Could not use the {tool} because the pixels in a group cannot be edited without merging the group."; +/// The colour-mode alerts (#12686); `{tool}` is [`mode_tool_name`]. The engine refuses the +/// command with the same words. +pub const INDEXED: &str = "Could not use the {tool} because it does not work with Indexed Color images (convert image to RGB Color mode to edit)."; +pub const BITMAP: &str = "Could not use the {tool} because it does not work with Bitmap images (convert image to Grayscale mode to edit)."; + +/// The tool's name in the colour-mode alerts, for the tools whose wording was measured: the Dodge +/// tool. Kept apart from [`tool_name`]: the layer alerts weren't measured for these tools (#12685). +fn mode_tool_name(tool: Tool) -> Option<&'static str> { + match tool { + Tool::Dodge => Some(tl!("dodge tool")), + _ => None, + } +} + /// The tool's name in the alerts, for the tools whose wording was measured: the Brush. pub fn tool_name(tool: Tool) -> Option<&'static str> { match tool { @@ -23,8 +37,17 @@ pub fn tool_name(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. +/// transparent pixels is no refusal, and a group's targeted mask paints. A tool with a +/// [`mode_tool_name`] gets only its colour-mode alert, in an Indexed Color or Bitmap document. pub fn alert(app: &PhotocraftApp, tool: Tool, hidden: bool) -> Option { + if let Some(name) = mode_tool_name(tool) { + let template = match app.session.active()?.doc.mode { + photocraft_doc::ColorMode::Indexed => INDEXED, + photocraft_doc::ColorMode::Bitmap => BITMAP, + _ => return None, + }; + return Some(crate::i18n::fmt(tl!(template), &[("tool", name)])); + } let name = tool_name(tool)?; let template = if hidden { HIDDEN