diff --git a/crates/engine-paint/src/history_brush_format_mismatch_tests.rs b/crates/engine-paint/src/history_brush_format_mismatch_tests.rs index 1f39e60af4..ed85086e49 100644 --- a/crates/engine-paint/src/history_brush_format_mismatch_tests.rs +++ b/crates/engine-paint/src/history_brush_format_mismatch_tests.rs @@ -78,8 +78,8 @@ fn a_source_of_another_bit_depth_is_refused() { } /// Not measured, so these keep today's behaviour: RGB to CMYK or Lab, and a change of both the -/// colour mode and the bit depth, convert the source; Indexed Color flattens the document, so the -/// source lacks the new layer, as before. +/// colour mode and the bit depth, convert the source. (Indexed Color is refused with the colour +/// mode text since #17841.) #[test] fn unmeasured_mode_pairs_keep_converting() { for mode in ["image.mode.cmyk", "image.mode.lab"] { @@ -88,9 +88,6 @@ fn unmeasured_mode_pairs_keep_converting() { paints(&mut s, json!({}), mode); } let mut s = session("rgb", 8); - s.execute("image.mode.indexedColor", json!({})).unwrap(); - assert!(refused(&mut s, json!({}), "indexed").starts_with("the target did not exist")); - let mut s = session("rgb", 8); s.execute("image.mode.bits16", json!({})).unwrap(); s.execute("image.mode.grayscale", json!({})).unwrap(); paints(&mut s, json!({}), "mode and depth"); diff --git a/crates/engine-paint/src/history_brush_indexed_bitmap_mode_alert_tests.rs b/crates/engine-paint/src/history_brush_indexed_bitmap_mode_alert_tests.rs new file mode 100644 index 0000000000..6070154dd8 --- /dev/null +++ b/crates/engine-paint/src/history_brush_indexed_bitmap_mode_alert_tests.rs @@ -0,0 +1,92 @@ +//! #17841: in an Indexed Color or Bitmap document the History Brush refuses a source state of +//! another colour mode (RGB, Grayscale) with the colour mode alert of #12842, adding no state and +//! changing no pixel. The check runs before the layer lookup, so this text wins over "the target +//! did not exist" that the Indexed conversion's new layer would give. Sources of the same mode +//! paint as before. + +use serde_json::{Value, json}; + +use crate::Session; +use crate::retouch_cmds::LiveRetouch; + +const MODE: &str = "Could not use the history brush because the current color mode does not match that of the history state."; + +/// Rows Open (white), Fill (red) in a `mode` document. +fn session(mode: &str) -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 24, "height": 16, "mode": mode, "depth": 8, "background": "white"})).unwrap(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s +} + +fn steps(s: &Session) -> usize { + s.active().unwrap().history.past_len() +} + +fn last_row(s: &Session) -> usize { + s.active().unwrap().history.entries().len() - 1 +} + +fn params(state: Value) -> Value { + json!({"points": [[4, 8], [20, 8]], "size": 6, "hardness": 100, "state": state}) +} + +fn refused(s: &mut Session, state: Value, case: &str) -> String { + let (before, n) = (s.active().unwrap().doc.clone(), steps(s)); + let e = s.execute("paint.historyBrush", params(state)).expect_err(case).to_string(); + assert_eq!(steps(s), n, "{case}: no state"); + assert!(s.active().unwrap().doc == before, "{case}: no pixel changed"); + e +} + +fn to_bitmap(s: &mut Session) { + s.execute("image.mode.bitmap", json!({"method": "threshold"})).unwrap(); +} + +#[test] +fn an_indexed_document_refuses_the_rgb_state_from_before_the_conversion() { + for state in [json!(1), json!(0), Value::Null] { + let mut s = session("rgb"); + s.execute("image.mode.indexedColor", json!({})).unwrap(); + let case = format!("indexed, state {state}"); + assert_eq!(refused(&mut s, state, &case), MODE, "{case}"); + } +} + +#[test] +fn a_bitmap_document_refuses_an_rgb_or_grayscale_state() { + // RGB → Grayscale → Bitmap: the RGB fill state and the Grayscale state just before Bitmap. + let mut s = session("rgb"); + s.execute("image.mode.grayscale", json!({})).unwrap(); + let gray_row = last_row(&s); + to_bitmap(&mut s); + assert_eq!(refused(&mut s, json!(1), "bitmap, RGB state"), MODE); + assert_eq!(refused(&mut s, json!(gray_row), "bitmap, Grayscale state"), MODE); + assert_eq!(refused(&mut s, Value::Null, "bitmap, default source"), MODE); + + // A Grayscale document converted to Bitmap. + let mut g = session("gray"); + to_bitmap(&mut g); + assert_eq!(refused(&mut g, json!(1), "gray document to bitmap"), MODE); +} + +#[test] +fn the_live_preview_is_refused_too() { + let mut s = session("rgb"); + s.execute("image.mode.indexedColor", json!({})).unwrap(); + let e = LiveRetouch::begin(&s, "paint.historyBrush", ¶ms(json!(1))).err().map(|e| e.to_string()); + assert_eq!(e.as_deref(), Some(MODE)); +} + +#[test] +fn a_source_of_the_same_mode_still_paints() { + let mut s = session("rgb"); + s.execute("image.mode.indexedColor", json!({})).unwrap(); + let row = last_row(&s); + s.execute("edit.fill", json!({"color": "#ffffff"})).unwrap(); + let (before, n) = (s.active().unwrap().doc.clone(), steps(&s)); + let r = s.execute("paint.historyBrush", params(json!(row))).unwrap(); + assert!(!r["damage"].is_null(), "{r}"); + assert_eq!(steps(&s), n + 1); + assert!(s.active().unwrap().doc != before, "the Indexed state painted"); +} diff --git a/crates/engine-paint/src/history_source.rs b/crates/engine-paint/src/history_source.rs index ab82fbf3b0..da4ce71e5d 100644 --- a/crates/engine-paint/src/history_source.rs +++ b/crates/engine-paint/src/history_source.rs @@ -52,12 +52,16 @@ pub fn checked_source(st: &DocState, p: &Value, cmd: &str) -> Result Result<()> { let (mode_differs, depth_differs) = (source.mode != st.doc.mode, source.depth != st.doc.depth); - if source.mode == ColorMode::Rgb && st.doc.mode == ColorMode::Grayscale && !depth_differs { + let rgb_to_gray = source.mode == ColorMode::Rgb && st.doc.mode == ColorMode::Grayscale && !depth_differs; + let into_indexed_or_bitmap = mode_differs && matches!(st.doc.mode, ColorMode::Indexed | ColorMode::Bitmap); + if rgb_to_gray || into_indexed_or_bitmap { return Err(EngineError::Other("Could not use the history brush because the current color mode does not match that of the history state.".into())); } if depth_differs && !mode_differs { @@ -69,3 +73,7 @@ pub fn refuse_other_format(st: &DocState, source: &Document) -> Result<()> { #[cfg(test)] #[path = "history_brush_format_mismatch_tests.rs"] mod history_brush_format_mismatch_tests; + +#[cfg(test)] +#[path = "history_brush_indexed_bitmap_mode_alert_tests.rs"] +mod history_brush_indexed_bitmap_mode_alert_tests;