Color Replacement: refuse RGB/32, Grayscale, Bitmap and Indexed documents (#11923)

paint.colorReplacement and its live stroke refuse the modes and depths
the reference application refuses, with its words: 32 bit per channel
RGB, Grayscale and Bitmap (only full color modes), and Indexed Color.
Nothing is painted and no history is added. RGB/8, RGB/16, CMYK and Lab
paint as before; cases that weren't measured keep today's behaviour.
This commit is contained in:
Hector Villarreal committed 2026-11-07 21:00:56 -06:00
1 parent e04ecbdaa3
commit 60e45bb926
4 files changed
+107 -4

No files matched your search

+19
View File
@@ -832,6 +832,21 @@ fn mixer_load_pixel(s: &mut Session, p: &Value) -> Result<Value> {
/// channel or Quick Mask `target` is refused rather than recolouring the layer instead.
fn color_replacement_setup(s: &Session, p: &Value) -> Result<(Stroke, ReplaceSettings, photocraft_doc::LayerId)> {
let cmd = "paint.colorReplacement";
// The modes and depths the reference application refuses, with its words (#11923). The live
// stroke runs this setup too, so it previews nothing in them. 32-bit Grayscale, CMYK and Lab,
// Multichannel and Duotone weren't measured and keep painting.
if let Some(st) = s.active() {
use photocraft_color::{ColorMode, SampleType};
let why = match (st.doc.mode, st.doc.depth) {
(ColorMode::Indexed, _) => Some("it does not work with Indexed Color images (convert image to RGB Color mode to edit)."),
(ColorMode::Bitmap, _) | (ColorMode::Grayscale, SampleType::U8 | SampleType::U16) => Some("it only works in full color modes."),
(ColorMode::Rgb, SampleType::F32) => Some("it does not work with 32 bit per channel images (convert image to 8 or 16 bit per channel to edit)."),
_ => None,
};
if let Some(why) = why {
return Err(EngineError::Other(format!("Could not use the Color Replacement tool because {why}")));
}
}
if let Some(t) = p.get("target").filter(|t| t.as_str() != Some("pixels"))
&& crate::color_channels::explicit(p, s.active().map_or(0, |st| crate::channel_cmds::color_count(&st.doc)))?.is_none()
{
@@ -1381,5 +1396,9 @@ pub fn specs() -> Vec<CommandSpec> {
#[cfg(test)]
mod tests;
#[cfg(test)]
#[path = "color_replacement_modes_tests.rs"]
mod color_replacement_modes_tests;
#[cfg(test)]
mod mixer_live_tests;
@@ -0,0 +1,71 @@
//! #11923: the Color Replacement tool refuses RGB/32, Grayscale, Bitmap and Indexed documents with
//! the reference application's messages, painting nothing and adding no history; the live stroke
//! is refused the same way. RGB/8, RGB/16, CMYK and Lab paint.
use photocraft_color::{Color, ColorMode, SampleType};
use photocraft_doc::{Document, Size};
use serde_json::json;
use super::*;
const DEPTH_32: &str =
"Could not use the Color Replacement tool because it does not work with 32 bit per channel images (convert image to 8 or 16 bit per channel to edit).";
const NOT_FULL_COLOR: &str = "Could not use the Color Replacement tool because it only works in full color modes.";
const INDEXED: &str = "Could not use the Color Replacement tool because it does not work with Indexed Color images (convert image to RGB Color mode to edit).";
/// A session with one 64 × 64 document of `mode` and `depth`, filled red.
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(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 stroke() -> Value {
json!({"points": [[20, 32], [44, 32]], "size": 20, "hardness": 100})
}
fn steps(s: &Session) -> usize {
s.active().unwrap().history.past_len()
}
fn pixels(s: &Session) -> Vec<f32> {
let st = s.active().unwrap();
let surf = st.doc.layers[0].surface().unwrap();
(0..64).flat_map(|x| surf.pixel(x, 32).to_vec()).collect()
}
#[test]
fn refused_modes_say_why_and_paint_nothing() {
for (mode, depth, want) in [
(ColorMode::Rgb, SampleType::F32, DEPTH_32),
(ColorMode::Grayscale, SampleType::U8, NOT_FULL_COLOR),
(ColorMode::Grayscale, SampleType::U16, NOT_FULL_COLOR),
(ColorMode::Bitmap, SampleType::U8, NOT_FULL_COLOR),
(ColorMode::Indexed, SampleType::U8, INDEXED),
] {
let mut s = session(mode, depth);
let (before, n) = (pixels(&s), steps(&s));
let err = s.execute("paint.colorReplacement", stroke()).unwrap_err().to_string();
assert_eq!(err, want, "{mode:?}/{depth:?}");
assert_eq!(pixels(&s), before, "{mode:?}/{depth:?}: nothing painted");
assert_eq!(steps(&s), n, "{mode:?}/{depth:?}: no history");
// The live stroke previews nothing either.
let live = live_color_replacement(&s, &stroke()).err().map(|e| e.to_string());
assert_eq!(live.as_deref(), Some(want), "{mode:?}/{depth:?}: live");
}
}
#[test]
fn full_colour_documents_still_paint() {
for (mode, depth) in
[(ColorMode::Rgb, SampleType::U8), (ColorMode::Rgb, SampleType::U16), (ColorMode::Cmyk, SampleType::U8), (ColorMode::Lab, SampleType::U8)]
{
let mut s = session(mode, depth);
let (before, n) = (pixels(&s), steps(&s));
s.execute("paint.colorReplacement", stroke()).unwrap_or_else(|e| panic!("{mode:?}/{depth:?}: {e}"));
assert_ne!(pixels(&s), before, "{mode:?}/{depth:?}: painted");
assert_eq!(steps(&s), n + 1, "{mode:?}/{depth:?}");
assert!(live_color_replacement(&s, &stroke()).is_ok(), "{mode:?}/{depth:?}: live");
}
}
+15 -3
View File
@@ -99,6 +99,12 @@ fn adjustments_filters_and_drawing_preserve_other_encoded_components() {
let mut s = setup(mode, depth);
subset(&mut s);
let before = surface(&s);
// Color Replacement refuses 32-bit RGB, as the reference application does (#11923).
if *cmd == "paint.colorReplacement" && mode == "rgb" && depth == 32 {
assert!(s.execute(cmd, p.clone()).is_err(), "{mode} {depth} {cmd} is refused");
assert_eq!(surface(&s), before, "{mode} {depth} {cmd}: nothing changed");
continue;
}
s.execute(cmd, p.clone()).unwrap_or_else(|e| panic!("{mode} {depth} {cmd}: {e}"));
let after = surface(&s);
unchanged(&before, &after);
@@ -669,7 +675,8 @@ fn last_filter_rejects_non_object_journal_params_without_panicking() {
#[test]
fn first_single_channel_retouch_preview_preserves_every_other_sample() {
for cmd in ["paint.dodge", "paint.burn", "paint.sponge", "paint.blur", "paint.sharpen", "paint.smudge", "paint.colorReplacement"] {
let mut s = setup("rgb", 32);
// Color Replacement refuses 32-bit RGB (#11923): it is checked at 16 bits.
let mut s = setup("rgb", if cmd == "paint.colorReplacement" { 16 } else { 32 });
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});
@@ -765,7 +772,11 @@ fn lab_single_channel_tone_parameters_round_trip_without_applying_lightness_twic
}
fn flat_rgb() -> Session {
let mut s = setup("rgb", 32);
flat_rgb_at(32)
}
fn flat_rgb_at(depth: u32) -> Session {
let mut s = setup("rgb", depth);
s.edit("flat", |doc, id| {
doc.layer_mut(id.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(0, 0, 32, 24), &[0.25, 0.5, 0.25, 1.0]);
Ok(())
@@ -894,7 +905,8 @@ 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")] {
let mut s = flat_rgb();
// 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, &params).unwrap();
@@ -115,7 +115,8 @@ fn contiguous_limits_skip_a_matching_island_that_discontiguous_recolours() {
/// 8 (the live stroke keeps its working pixels in the layer's depth between dabs).
#[test]
fn a_stroke_is_drawn_live_and_is_one_history_step() {
for depth in [8, 32] {
// Color Replacement refuses 32-bit RGB documents, as the reference application does (#11923).
for depth in [8, 16] {
live_stroke_at(depth);
}
}