From cd087a5f4ef6d0d79fcf8526256c44c2a1130ced Mon Sep 17 00:00:00 2001 From: DuBearte <130190089+DuBearte@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:22:11 -0500 Subject: [PATCH 1/7] Color Replacement: the tolerance fades over its whole range Each dab now replaces a colour by 1 - (d / tolerance)^1.15 instead of fully up to three quarters of the tolerance, so colours near the limit are only partly replaced, as the reference application measures. The exponent is fitted to the issue's eight-block table. Fixes #11916 Co-Authored-By: Claude Opus 5.5 --- crates/paint/src/replace.rs | 10 +- .../src/replace_tolerance_falloff_tests.rs | 110 ++++++++++++++++++ 2 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 crates/paint/src/replace_tolerance_falloff_tests.rs diff --git a/crates/paint/src/replace.rs b/crates/paint/src/replace.rs index 20619cd48a..7686ed93b9 100644 --- a/crates/paint/src/replace.rs +++ b/crates/paint/src/replace.rs @@ -73,6 +73,10 @@ impl Default for ReplaceSettings { } } +#[cfg(test)] +#[path = "replace_tolerance_falloff_tests.rs"] +mod replace_tolerance_falloff_tests; + fn dist(a: &[f32; 4], b: [f32; 3]) -> f32 { (a[0] - b[0]).abs().max((a[1] - b[1]).abs()).max((a[2] - b[2]).abs()) } @@ -103,8 +107,10 @@ pub fn color_replacement_effect(pre: Surface, rs: ReplaceSettings) -> impl FnMut if d > tol { 0.0 } else if soft && tol > 0.0 { - // Soft falloff over the last quarter of the tolerance. - ((tol - d) / (tol * 0.25)).clamp(0.0, 1.0) + // Soft across the whole tolerance, as the reference measures (#11916): each dab + // replaces less the further a colour is from the sample. The exponent is fitted to + // its eight-block table (within 6 levels at Tolerance 10, 30, 50 and 100). + (1.0 - (d / tol).powf(1.15)).clamp(0.0, 1.0) } else { 1.0 } diff --git a/crates/paint/src/replace_tolerance_falloff_tests.rs b/crates/paint/src/replace_tolerance_falloff_tests.rs new file mode 100644 index 0000000000..4c044250cd --- /dev/null +++ b/crates/paint/src/replace_tolerance_falloff_tests.rs @@ -0,0 +1,110 @@ +//! #11916: colours near the Tolerance limit are only partly replaced, fading over the whole +//! range, as the reference application measures. + +use super::*; +use crate::{BrushSettings, StrokePoint}; +use photocraft_color::PixelFormat; + +/// Eight 100 px blocks at y 300..600, block i = (200, 40 + 20 i, 40); a 40 px hard round tip +/// swept from x 50 to 750 at y 450 in Color mode with blue, Sampling Once, Discontiguous. +fn blocks_after(tolerance: f32) -> Vec<[f32; 3]> { + let fmt = PixelFormat::RGBA8; + let mut s = Surface::new(fmt); + for i in 0..8 { + let c = [200.0 / 255.0, (40.0 + 20.0 * i as f32) / 255.0, 40.0 / 255.0, 1.0]; + s.fill_rect(Rect::new(100 * i, 300, 100 * i + 100, 600), &photocraft_raster::from_rgba(&fmt, c)); + } + let stroke = Stroke { + brush: BrushSettings { size: 40.0, hardness: 1.0, spacing: 0.25, pressure_size: false, ..Default::default() }, + points: vec![StrokePoint::new(50.0, 450.0, 1.0), StrokePoint::new(750.0, 450.0, 1.0)], + }; + let rs = ReplaceSettings { + mode: ReplaceMode::Color, + sampling: Sampling::Once, + limits: Limits::Discontiguous, + tolerance: tolerance / 100.0, + anti_alias: true, + color: [0.0, 0.0, 1.0, 1.0], + ..Default::default() + }; + apply_color_replacement(&mut s, &stroke, &rs, None, false); + (0..8).map(|i| s.rgba(100 * i + 50, 450)).map(|c| [c[0] * 255.0, c[1] * 255.0, c[2] * 255.0]).collect() +} + +fn untouched(i: usize) -> [f32; 3] { + [200.0, 40.0 + 20.0 * i as f32, 40.0] +} + +#[test] +fn the_tolerance_fades_over_its_whole_range() { + // The reference's table; None = untouched. Tolerance 100's block 7 sits under the stroke's + // last dab, which the reference's stroke reached fewer times than ours, so it isn't compared. + let table: [(f32, [Option<[f32; 3]>; 7]); 4] = [ + (10.0, [Some([71.0, 71.0, 255.0]), Some([122.0, 74.0, 183.0]), None, None, None, None, None]), + (30.0, [Some([71.0, 71.0, 255.0]), Some([85.0, 85.0, 254.0]), Some([102.0, 95.0, 242.0]), Some([141.0, 106.0, 181.0]), None, None, None]), + ( + 50.0, + [ + Some([71.0, 71.0, 255.0]), + Some([85.0, 85.0, 255.0]), + Some([97.0, 96.0, 254.0]), + Some([114.0, 110.0, 247.0]), + Some([132.0, 120.0, 226.0]), + Some([157.0, 137.0, 180.0]), + Some([188.0, 156.0, 92.0]), + ], + ), + ( + 100.0, + [ + Some([71.0, 71.0, 255.0]), + Some([85.0, 85.0, 255.0]), + Some([99.0, 99.0, 255.0]), + Some([111.0, 111.0, 255.0]), + Some([123.0, 123.0, 254.0]), + Some([138.0, 137.0, 251.0]), + Some([151.0, 149.0, 246.0]), + ], + ), + ]; + for (tol, row) in table { + let got = blocks_after(tol); + for (i, want) in row.iter().enumerate() { + let want = want.unwrap_or_else(|| untouched(i)); + let within = if row[i].is_some() { 6.0 } else { 0.5 }; + for k in 0..3 { + assert!((got[i][k] - want[k]).abs() <= within, "tolerance {tol}, block {i}: {:?}, expected {want:?}", got[i]); + } + } + } + // Block 7 lies outside Tolerance 50 and below, and is still only partly replaced at 100. + assert_eq!(blocks_after(50.0)[7].map(f32::round), untouched(7)); + let b7 = blocks_after(100.0)[7]; + assert!(b7[0] > 160.0 && b7[2] < 230.0, "tolerance 100, block 7 partly replaced: {b7:?}"); +} + +/// A click on the border between red above and block 1 below samples red: the red is replaced +/// fully, block 1 only partly (the reference: 67,67,255 and 157,68,119). +#[test] +fn a_border_click_partly_replaces_the_neighbouring_colour() { + let fmt = PixelFormat::RGBA8; + let mut s = Surface::new(fmt); + s.fill_rect(Rect::new(0, 0, 200, 450), &photocraft_raster::from_rgba(&fmt, [220.0 / 255.0, 30.0 / 255.0, 30.0 / 255.0, 1.0])); + s.fill_rect(Rect::new(0, 450, 200, 600), &photocraft_raster::from_rgba(&fmt, [200.0 / 255.0, 60.0 / 255.0, 40.0 / 255.0, 1.0])); + let stroke = Stroke { + brush: BrushSettings { size: 40.0, hardness: 1.0, spacing: 0.25, pressure_size: false, ..Default::default() }, + points: vec![StrokePoint::new(100.0, 449.5, 1.0)], + }; + let rs = ReplaceSettings { + mode: ReplaceMode::Color, + sampling: Sampling::Continuous, + limits: Limits::Contiguous, + tolerance: 0.3, + color: [0.0, 0.0, 1.0, 1.0], + ..Default::default() + }; + apply_color_replacement(&mut s, &stroke, &rs, None, false); + let (red, below) = (s.rgba(100, 440).map(|v| v * 255.0), s.rgba(100, 455).map(|v| v * 255.0)); + assert!(red[0] < 75.0 && red[2] > 250.0, "red replaced fully: {red:?}"); + assert!(below[0] > 120.0 && below[0] < 199.0 && below[2] > 41.0, "block 1 partly replaced: {below:?}"); +} From c8aa94678ec1ff9f1812654749dae9d301b447e1 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 21 Nov 2026 07:41:44 -0600 Subject: [PATCH 2/7] Color Replacement: alerts for a hidden layer, Lock All and a group A Color Replacement press on a hidden target layer, a layer with Lock All or a group now opens the reference application's alert with OK and paints nothing, instead of a status-bar message. The group text is the tool's own. Lock transparent pixels still recolours. Fixes #11924 --- .../color_replacement_refusal_alert_tests.rs | 70 +++++++++++++++++++ crates/ui-egui/src/hidden_layer_tests.rs | 6 +- crates/ui-egui/src/tools/brush_refusal.rs | 12 +++- 3 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 crates/ui-egui/src/color_replacement_refusal_alert_tests.rs diff --git a/crates/ui-egui/src/color_replacement_refusal_alert_tests.rs b/crates/ui-egui/src/color_replacement_refusal_alert_tests.rs new file mode 100644 index 0000000000..2ebbeb5b82 --- /dev/null +++ b/crates/ui-egui/src/color_replacement_refusal_alert_tests.rs @@ -0,0 +1,70 @@ +//! #11924: when the Color Replacement tool can't paint (a hidden target layer, Lock All, a group), +//! a press opens the reference application's alert with OK and changes nothing. Lock transparent +//! pixels still recolours. Other tools keep their alerts. + +use serde_json::json; + +use super::brush_refusal_alert_tests::{active, alerts, app, refused, steps, stroke}; +use super::*; +use crate::control::{ControlRequest, Reply, handle}; + +#[test] +fn a_hidden_target_layer() { + let mut app = app(Tool::ColorReplacement); + let id = active(&app); + app.run("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + refused(&mut app, "Could not use the Color Replacement tool because the target layer is hidden."); + assert!(app.ui.status.is_empty() || !app.ui.status_error, "no status-bar error as well: {:?}", app.ui.status); + assert_eq!(photocraft_engine::hidden_target::HIDDEN, "Could not complete your request because the target layer is hidden.", "the shared text is unchanged"); +} + +#[test] +fn lock_all() { + let mut app = app(Tool::ColorReplacement); + app.run("layer.lockLayers", json!({"all": true})).unwrap(); + refused(&mut app, "Could not use the Color Replacement tool because the layer is locked."); +} + +#[test] +fn a_group_is_active() { + let mut app = app(Tool::ColorReplacement); + app.run("layer.new.group", json!({})).unwrap(); + refused(&mut app, "Could not use the Color Replacement tool because the target is a group."); +} + +#[test] +fn lock_transparent_pixels_still_recolours() { + let mut app = app(Tool::ColorReplacement); + app.run("layer.lockLayers", json!({"transparency": true})).unwrap(); + let before = steps(&app); + stroke(&mut app); + assert!(alerts(&app).is_empty(), "no refusal: {:?}", alerts(&app)); + assert_eq!(steps(&app), before + 1, "the stroke was applied"); +} + +#[test] +fn the_alert_is_listed_and_confirmed_over_the_control_channel() { + let ctx = egui::Context::default(); + let mut app = app(Tool::ColorReplacement); + app.run("layer.lockLayers", json!({"all": true})).unwrap(); + stroke(&mut app); + let mut call = |method: &str, params: serde_json::Value| { + let (req, _rx) = ControlRequest::new(method, params); + match handle(&mut app, &ctx, &req) { + Reply::Done(v) => v, + _ => panic!("{method}: expected an immediate reply"), + } + }; + let inspect = call("ui.inspect", json!({})); + let dialog = &inspect["result"]["dialogs"][0]; + assert_eq!(dialog["fields"]["message"], "Could not use the Color Replacement tool because the layer is locked.", "{inspect}"); + assert_eq!(call("ui.dialog.confirm", json!({"dialog": dialog["id"]}))["ok"], true); + assert!(alerts(&app).is_empty()); +} + +#[test] +fn the_brush_keeps_its_group_text() { + let mut app = app(Tool::Brush); + app.run("layer.new.group", json!({})).unwrap(); + refused(&mut app, "Could not use the brush tool because the pixels in a group cannot be edited without merging the group."); +} diff --git a/crates/ui-egui/src/hidden_layer_tests.rs b/crates/ui-egui/src/hidden_layer_tests.rs index 338d4fe76b..8c7e152eae 100644 --- a/crates/ui-egui/src/hidden_layer_tests.rs +++ b/crates/ui-egui/src/hidden_layer_tests.rs @@ -82,11 +82,11 @@ fn painting_a_hidden_layer_is_refused_at_the_press() { let n = steps(&app); tool_event(&mut app, ToolEvent::Down { x: 30.0, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE); assert!(!crate::canvas::testing::in_gesture(&app) && app.live_stroke.is_none(), "{tool:?}: no stroke started"); - // The Brush (#11898) and the Classic gradient (#12017) say so in an alert, the other tools - // in the status bar. + // The Brush (#11898), the Classic gradient (#12017) and Color Replacement (#11924) say so in + // an alert, the other tools in the status bar. let alerts = alerts(&app); let alert = alerts.iter().any(|m| m.contains("target layer is hidden")); - if matches!(tool, Tool::Brush | Tool::Gradient) { + if matches!(tool, Tool::Brush | Tool::Gradient | Tool::ColorReplacement) { assert!(alert, "{tool:?}: {alerts:?}"); } else { assert!(app.ui.status.contains("target layer is hidden") && alerts.is_empty(), "{tool:?}: {}", app.ui.status); diff --git a/crates/ui-egui/src/tools/brush_refusal.rs b/crates/ui-egui/src/tools/brush_refusal.rs index ed76093d49..310f6d20d1 100644 --- a/crates/ui-egui/src/tools/brush_refusal.rs +++ b/crates/ui-egui/src/tools/brush_refusal.rs @@ -14,6 +14,8 @@ use crate::state::Tool; pub const HIDDEN: &str = "Could not use the {tool} because the target layer is hidden."; 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 Color Replacement tool's group alert (#11924): its own wording, not the Brush's [`GROUP`]. +pub const TARGET_GROUP: &str = "Could not use the {tool} because the target is a group."; /// The Remove tool's one measured alert, Lock All (#12552): it doesn't name the tool. pub const REMOVE_LOCKED: &str = "Can't use the tool because the layer is locked."; @@ -39,7 +41,8 @@ fn mode_tool_name(tool: Tool) -> Option<&'static str> { /// The tool's name in the alerts, for the tools whose wording was measured: the Brush, the /// Sharpen tool (#12952), the Sponge (#13030), the Smudge tool (#12965), the Art History Brush -/// (#12638) and the Classic gradient (the live Gradient makes a fill layer and isn't measured, #12017). +/// (#12638), the Color Replacement tool (#11924) and the Classic gradient (the live Gradient makes a +/// fill layer and isn't measured, #12017). pub fn tool_name(app: &PhotocraftApp, tool: Tool) -> Option<&'static str> { match tool { Tool::Brush => Some(tl!("brush tool")), @@ -47,6 +50,7 @@ pub fn tool_name(app: &PhotocraftApp, tool: Tool) -> Option<&'static str> { Tool::Sponge => Some(tl!("sponge tool")), Tool::Smudge => Some(tl!("smudge tool")), Tool::ArtHistoryBrush => Some(tl!("art history brush")), + Tool::ColorReplacement => Some(tl!("Color Replacement tool")), Tool::Gradient if app.ui.tool_options.gradient_classic => Some(tl!("gradient tool")), _ => None, } @@ -82,6 +86,8 @@ pub fn alert(app: &PhotocraftApp, tool: Tool, hidden: bool) -> Option { } let name = tool_name(app, tool)?; let template = if hidden { HIDDEN } else { layer_refusal(app, tool)? }; + // The Color Replacement tool says a group differently (#11924). + let template = if tool == Tool::ColorReplacement && template == GROUP { TARGET_GROUP } else { template }; Some(crate::i18n::fmt(tl!(template), &[("tool", name)])) } @@ -119,6 +125,10 @@ mod retouch_mode_refusal_alert_tests; #[path = "../smudge_refusal_alert_tests.rs"] mod smudge_refusal_alert_tests; +#[cfg(test)] +#[path = "../color_replacement_refusal_alert_tests.rs"] +mod color_replacement_refusal_alert_tests; + #[cfg(test)] #[path = "remove_hidden_layer_tests.rs"] mod remove_hidden_layer_tests; From 8796c756dd8fdf25e434de66ea41c26e29d772d2 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 21 Nov 2026 14:39:34 -0600 Subject: [PATCH 3/7] Color Replacement: the hidden-layer alert is for pixel targets only (CodeRabbit on #16279) Test: a_hidden_layer_with_a_mask_target_keeps_the_status_bar_message. --- .../src/color_replacement_refusal_alert_tests.rs | 14 ++++++++++++++ crates/ui-egui/src/tools/brush_refusal.rs | 5 ++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/color_replacement_refusal_alert_tests.rs b/crates/ui-egui/src/color_replacement_refusal_alert_tests.rs index 2ebbeb5b82..26beacdade 100644 --- a/crates/ui-egui/src/color_replacement_refusal_alert_tests.rs +++ b/crates/ui-egui/src/color_replacement_refusal_alert_tests.rs @@ -18,6 +18,20 @@ fn a_hidden_target_layer() { assert_eq!(photocraft_engine::hidden_target::HIDDEN, "Could not complete your request because the target layer is hidden.", "the shared text is unchanged"); } +/// A targeted layer mask on a hidden layer keeps the status-bar refusal: only a pixel target gets the alert (CodeRabbit +/// on #16279). +#[test] +fn a_hidden_layer_with_a_mask_target_keeps_the_status_bar_message() { + let mut app = app(Tool::ColorReplacement); + app.run("layer.layerMask.revealAll", json!({})).unwrap(); + let id = active(&app); + app.run("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + app.ui.mask_target = true; + stroke(&mut app); + assert!(alerts(&app).is_empty(), "no alert: {:?}", alerts(&app)); + assert!(app.ui.status_error, "the status bar says it: {:?}", app.ui.status); +} + #[test] fn lock_all() { let mut app = app(Tool::ColorReplacement); diff --git a/crates/ui-egui/src/tools/brush_refusal.rs b/crates/ui-egui/src/tools/brush_refusal.rs index 310f6d20d1..43e2eeca3e 100644 --- a/crates/ui-egui/src/tools/brush_refusal.rs +++ b/crates/ui-egui/src/tools/brush_refusal.rs @@ -85,7 +85,10 @@ pub fn alert(app: &PhotocraftApp, tool: Tool, hidden: bool) -> Option { return (!hidden && pixel_layer && layer_refusal(app, tool) == Some(LOCKED)).then(|| tl!(REMOVE_LOCKED).to_string()); } let name = tool_name(app, tool)?; - let template = if hidden { HIDDEN } else { layer_refusal(app, tool)? }; + // The Color Replacement tool's hidden-layer alert is for a pixel target only: a mask target keeps the status-bar + // message it had (CodeRabbit on #16279). + let hidden_alert = hidden && (tool != Tool::ColorReplacement || crate::canvas::paint_target(app) == json!("pixels")); + let template = if hidden_alert { HIDDEN } else { layer_refusal(app, tool)? }; // The Color Replacement tool says a group differently (#11924). let template = if tool == Tool::ColorReplacement && template == GROUP { TARGET_GROUP } else { template }; Some(crate::i18n::fmt(tl!(template), &[("tool", name)])) From e146acf767c6396c98ab2858e5fd96b58feeb120 Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 23:15:15 +0100 Subject: [PATCH 4/7] Color Replacement: check block 7 with a stroke past it (#12368, #11916) At Tolerance 100 the table's block 7 is the stroke's end point, where the reference's value depends on where its last dabs land (measured: 176,169,181 ending at 750, 195,178,69 ending at 740). A stroke on to x 790 leaves the block's centre at 166,165,238 in the reference; compare against that within 6 levels instead of only checking it is partly replaced. Co-Authored-By: Claude Opus 5.5 --- .../src/replace_tolerance_falloff_tests.rs | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/crates/paint/src/replace_tolerance_falloff_tests.rs b/crates/paint/src/replace_tolerance_falloff_tests.rs index 4c044250cd..fbc15f2833 100644 --- a/crates/paint/src/replace_tolerance_falloff_tests.rs +++ b/crates/paint/src/replace_tolerance_falloff_tests.rs @@ -6,8 +6,8 @@ use crate::{BrushSettings, StrokePoint}; use photocraft_color::PixelFormat; /// Eight 100 px blocks at y 300..600, block i = (200, 40 + 20 i, 40); a 40 px hard round tip -/// swept from x 50 to 750 at y 450 in Color mode with blue, Sampling Once, Discontiguous. -fn blocks_after(tolerance: f32) -> Vec<[f32; 3]> { +/// swept from x 50 to `end_x` at y 450 in Color mode with blue, Sampling Once, Discontiguous. +fn blocks_after(tolerance: f32, end_x: f64) -> Vec<[f32; 3]> { let fmt = PixelFormat::RGBA8; let mut s = Surface::new(fmt); for i in 0..8 { @@ -16,7 +16,7 @@ fn blocks_after(tolerance: f32) -> Vec<[f32; 3]> { } let stroke = Stroke { brush: BrushSettings { size: 40.0, hardness: 1.0, spacing: 0.25, pressure_size: false, ..Default::default() }, - points: vec![StrokePoint::new(50.0, 450.0, 1.0), StrokePoint::new(750.0, 450.0, 1.0)], + points: vec![StrokePoint::new(50.0, 450.0, 1.0), StrokePoint::new(end_x, 450.0, 1.0)], }; let rs = ReplaceSettings { mode: ReplaceMode::Color, @@ -37,8 +37,9 @@ fn untouched(i: usize) -> [f32; 3] { #[test] fn the_tolerance_fades_over_its_whole_range() { - // The reference's table; None = untouched. Tolerance 100's block 7 sits under the stroke's - // last dab, which the reference's stroke reached fewer times than ours, so it isn't compared. + // The reference's table for a stroke from x 50 to 750; None = untouched. Block 7's centre is + // the stroke's end, where the reference's value depends on where its last dabs land, so + // block 7 is checked below with a stroke that runs past it. let table: [(f32, [Option<[f32; 3]>; 7]); 4] = [ (10.0, [Some([71.0, 71.0, 255.0]), Some([122.0, 74.0, 183.0]), None, None, None, None, None]), (30.0, [Some([71.0, 71.0, 255.0]), Some([85.0, 85.0, 254.0]), Some([102.0, 95.0, 242.0]), Some([141.0, 106.0, 181.0]), None, None, None]), @@ -68,7 +69,7 @@ fn the_tolerance_fades_over_its_whole_range() { ), ]; for (tol, row) in table { - let got = blocks_after(tol); + let got = blocks_after(tol, 750.0); for (i, want) in row.iter().enumerate() { let want = want.unwrap_or_else(|| untouched(i)); let within = if row[i].is_some() { 6.0 } else { 0.5 }; @@ -77,10 +78,13 @@ fn the_tolerance_fades_over_its_whole_range() { } } } - // Block 7 lies outside Tolerance 50 and below, and is still only partly replaced at 100. - assert_eq!(blocks_after(50.0)[7].map(f32::round), untouched(7)); - let b7 = blocks_after(100.0)[7]; - assert!(b7[0] > 160.0 && b7[2] < 230.0, "tolerance 100, block 7 partly replaced: {b7:?}"); + // Block 7 lies outside Tolerance 50 and below. At 100 a stroke on to x 790 leaves its centre + // only partly replaced: the reference measures 166,165,238 there. + assert_eq!(blocks_after(50.0, 750.0)[7].map(f32::round), untouched(7)); + let b7 = blocks_after(100.0, 790.0)[7]; + for (k, want) in [166.0, 165.0, 238.0].into_iter().enumerate() { + assert!((b7[k] - want).abs() <= 6.0, "tolerance 100, block 7: {b7:?}, expected 166,165,238"); + } } /// A click on the border between red above and block 1 below samples red: the red is replaced From 7994d077175234239090025afa891484af8e77aa Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 23:32:56 +0100 Subject: [PATCH 5/7] Color Replacement: handle the stroke result in the falloff tests (#12368) Co-Authored-By: Claude Opus 5.5 --- crates/paint/src/replace_tolerance_falloff_tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/paint/src/replace_tolerance_falloff_tests.rs b/crates/paint/src/replace_tolerance_falloff_tests.rs index fbc15f2833..8c06187f3a 100644 --- a/crates/paint/src/replace_tolerance_falloff_tests.rs +++ b/crates/paint/src/replace_tolerance_falloff_tests.rs @@ -27,7 +27,7 @@ fn blocks_after(tolerance: f32, end_x: f64) -> Vec<[f32; 3]> { color: [0.0, 0.0, 1.0, 1.0], ..Default::default() }; - apply_color_replacement(&mut s, &stroke, &rs, None, false); + apply_color_replacement(&mut s, &stroke, &rs, None, false).expect("the stroke fits"); (0..8).map(|i| s.rgba(100 * i + 50, 450)).map(|c| [c[0] * 255.0, c[1] * 255.0, c[2] * 255.0]).collect() } @@ -107,7 +107,7 @@ fn a_border_click_partly_replaces_the_neighbouring_colour() { color: [0.0, 0.0, 1.0, 1.0], ..Default::default() }; - apply_color_replacement(&mut s, &stroke, &rs, None, false); + apply_color_replacement(&mut s, &stroke, &rs, None, false).expect("the stroke fits"); let (red, below) = (s.rgba(100, 440).map(|v| v * 255.0), s.rgba(100, 455).map(|v| v * 255.0)); assert!(red[0] < 75.0 && red[2] > 250.0, "red replaced fully: {red:?}"); assert!(below[0] > 120.0 && below[0] < 199.0 && below[2] > 41.0, "block 1 partly replaced: {below:?}"); From 73318789a7f9493e149380285c89e8e3678f5000 Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 23:27:03 +0100 Subject: [PATCH 6/7] Color Replacement: Angle field and Pressure for Size toggle on the options bar (#11929) Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/canvas/paint_mouse.rs | 4 +- .../src/color_replacement_bar_angle_tests.rs | 140 ++++++++++++++++++ .../ui-egui/src/tools/retouch_ui/bar/tone.rs | 61 ++++++-- 3 files changed, 191 insertions(+), 14 deletions(-) create mode 100644 crates/ui-egui/src/color_replacement_bar_angle_tests.rs diff --git a/crates/ui-egui/src/canvas/paint_mouse.rs b/crates/ui-egui/src/canvas/paint_mouse.rs index c50a15c7b0..641ed22244 100644 --- a/crates/ui-egui/src/canvas/paint_mouse.rs +++ b/crates/ui-egui/src/canvas/paint_mouse.rs @@ -82,7 +82,9 @@ pub fn sync_tool_brush(app: &mut PhotocraftApp) { }; // The Remove tool starts at its measured 19 px; the rest is the current brush (#12548). let size = if tool == Tool::Remove { REMOVE_TOOL_SIZE } else { current.size }; - BrushSettings { smoothing: first_smoothing(), mode: photocraft_color::BlendMode::Normal, opacity, flow, size, ..current } + // Color Replacement's Angle and Pressure for Size are its own bar controls: 0° and off (#11929). + let (angle, pressure_size) = if tool == Tool::ColorReplacement { (0.0, false) } else { (current.angle, current.pressure_size) }; + BrushSettings { smoothing: first_smoothing(), mode: photocraft_color::BlendMode::Normal, opacity, flow, size, angle, pressure_size, ..current } } }; // The picked preset was the outgoing tool's brush: forget it, so Update Brush can't overwrite diff --git a/crates/ui-egui/src/color_replacement_bar_angle_tests.rs b/crates/ui-egui/src/color_replacement_bar_angle_tests.rs new file mode 100644 index 0000000000..441184202f --- /dev/null +++ b/crates/ui-egui/src/color_replacement_bar_angle_tests.rs @@ -0,0 +1,140 @@ +//! #11929: the Color Replacement options bar ends with the brush Angle field and the Pressure for Size +//! toggle, after Anti-alias. Both edit the tool's own brush, the one the Brush Settings panel shows; +//! typed angles are whole degrees -180..180 with the reference application's alerts. + +use egui::accesskit::{Role, Toggled}; +use egui::{Key, Modifiers, vec2}; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use serde_json::json; + +use crate::{PhotocraftApp, Tool}; + +type H = Harness<'static, Box>; + +const PRESSURE: &str = "Always use pressure for size"; +const RANGE_ALERT: &str = "An integer between -180 ° and 180 ° is required. Closest value inserted."; +const INVALID_ALERT: &str = "Invalid numeric entry. An integer between -180 ° and 180 ° is required. Restored last valid value."; + +fn bar() -> H { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 80, "height": 80})).unwrap(); + crate::theme::install_fonts(app.ctx()); + crate::theme::apply(app.ctx(), &crate::theme::AppTheme::pro()); + let mut h = Harness::builder().with_context(app.ctx().clone()).with_size(vec2(1800.0, 300.0)).with_step_dt(1.0 / 60.0).build_ui_state( + |ui, app: &mut Box| { + crate::panels::options_bar(app, ui); + crate::dialogs::run(app, ui); + }, + app, + ); + h.run_steps(4); + h +} + +fn select(h: &mut H, tool: Tool) { + h.state_mut().ui.tool = tool; + h.run_steps(3); +} + +fn angle_field<'a>(h: &'a H) -> egui_kittest::Node<'a> { + h.query_all_by_role(Role::SpinButton).find(|n| n.accesskit_node().label().is_some_and(|l| l.trim_end_matches(':') == "Angle")).expect("the Angle field") +} + +fn shown_angle(h: &H) -> Option { + angle_field(h).accesskit_node().numeric_value() +} + +fn pressure_on(h: &H) -> bool { + h.get_by_role_and_label(Role::Button, PRESSURE).accesskit_node().toggled() == Some(Toggled::True) +} + +fn type_angle(h: &mut H, text: &str) { + angle_field(h).click(); + h.run_steps(1); + h.key_press_modifiers(Modifiers::COMMAND, Key::A); + h.event(egui::Event::Text(text.into())); + h.run_steps(1); + h.key_press(Key::Enter); + h.run_steps(4); +} + +/// `number` with the system's decimal mark, which the field reads. +fn local(number: &str) -> String { + let mark = craft::i18n::Region::system().decimal.filter(|c| *c == ',').unwrap_or('.'); + number.replace('.', &mark.to_string()) +} + +/// The messages of the open alerts. +fn alerts(h: &H) -> Vec { + h.state().framework.dialogs.open_dialogs().iter().filter_map(|d| d.fields.as_ref()?.get("message")?.as_str().map(str::to_owned)).collect() +} + +fn dismiss(h: &mut H) { + h.get_by_label("OK").click(); + h.run_steps(4); + assert!(alerts(h).is_empty(), "OK closes the alert"); +} + +#[test] +fn angle_and_pressure_for_size_follow_anti_alias_with_the_reference_defaults_and_tooltips() { + let mut h = bar(); + select(&mut h, Tool::ColorReplacement); + let anti_alias = h.get_by_label("Anti-alias").rect(); + let angle = angle_field(&h); + let pressure = h.get_by_role_and_label(Role::Button, PRESSURE); + assert!(anti_alias.right() <= angle.rect().left(), "Anti-alias, then Angle"); + assert!(angle.rect().right() <= pressure.rect().left(), "Angle, then Pressure for Size"); + assert_eq!(angle.accesskit_node().description().as_deref(), Some("Set the brush angle")); + assert_eq!(pressure.accesskit_node().description().as_deref(), Some("Always use Pressure for Size. When off, Brush Preset controls pressure")); + assert_eq!(shown_angle(&h), Some(0.0), "0° by default"); + assert!(!pressure_on(&h), "Pressure for Size is off by default"); +} + +#[test] +fn typed_angles_are_clamped_and_truncated_with_the_reference_alerts() { + let mut h = bar(); + select(&mut h, Tool::ColorReplacement); + for (typed, kept) in [("400", 180.0), ("-200", -180.0)] { + type_angle(&mut h, typed); + assert_eq!(h.state().session.tools.brush.angle, kept, "{typed}: the closest value is kept"); + assert_eq!(alerts(&h), [RANGE_ALERT], "{typed}"); + dismiss(&mut h); + assert_eq!(h.state().session.tools.brush.angle, kept, "{typed}: the value stays after OK"); + } + type_angle(&mut h, &local("45.5")); + assert_eq!(h.state().session.tools.brush.angle, 45.0, "a fraction is truncated"); + assert!(alerts(&h).is_empty(), "45.5 is in range: no alert"); + + type_angle(&mut h, "abc"); + assert_eq!(alerts(&h), [INVALID_ALERT], "a non-number"); + dismiss(&mut h); + assert_eq!(h.state().session.tools.brush.angle, 45.0, "a non-number keeps the last value"); + assert_eq!(shown_angle(&h), Some(45.0), "the field shows the last valid value again"); +} + +#[test] +fn both_controls_write_the_color_replacement_brush_only_and_share_it_with_brush_settings() { + let mut h = bar(); + h.state_mut().run("tools.setBrush", json!({"brush": {"angle": -30.0, "pressureSize": true}})).unwrap(); + select(&mut h, Tool::ColorReplacement); + assert_eq!(shown_angle(&h), Some(0.0), "Color Replacement shows its own brush, not the Brush's"); + assert!(!pressure_on(&h)); + + // The Brush Settings panel edits the active tool's brush through this command. + h.state_mut().run("tools.setBrush", json!({"brush": {"angle": 60.0}})).unwrap(); + h.run_steps(2); + assert_eq!(shown_angle(&h), Some(60.0), "the field shows the panel's angle"); + type_angle(&mut h, "25"); + h.get_by_role_and_label(Role::Button, PRESSURE).click(); + h.run_steps(2); + let brush = &h.state().session.tools.brush; + assert_eq!((brush.angle, brush.pressure_size), (25.0, true), "the panel reads the same brush"); + + select(&mut h, Tool::Brush); + let brush = &h.state().session.tools.brush; + assert_eq!((brush.angle, brush.pressure_size), (-30.0, true), "the Brush keeps its own values"); + select(&mut h, Tool::ColorReplacement); + assert_eq!(shown_angle(&h), Some(25.0)); + assert!(pressure_on(&h), "Color Replacement keeps its own values"); +} diff --git a/crates/ui-egui/src/tools/retouch_ui/bar/tone.rs b/crates/ui-egui/src/tools/retouch_ui/bar/tone.rs index 2d3222b100..bea5e13a94 100644 --- a/crates/ui-egui/src/tools/retouch_ui/bar/tone.rs +++ b/crates/ui-egui/src/tools/retouch_ui/bar/tone.rs @@ -3,12 +3,12 @@ use craft::behavior::{IconId, NumberCodec}; use craft::ui::buttons::ToggleButton; use craft::ui::choice::{Checkbox, Segmented}; -use craft::ui::contract::{Bind, Entry, FieldLayout, Props as _, Show as _, ShowContainer as _, SideLabel, Sizing, bind_tracked}; +use craft::ui::contract::{Bind, EditResponse, Entry, FieldLayout, Props as _, Show as _, ShowContainer as _, SideLabel, Sizing, bind_tracked}; use craft::ui::display::Divider; use craft::ui::form::Field; use craft::ui::primitives::IconSpec; use craft::ui::runtime::env::Env; -use craft::ui::text::{NumberField, NumberValue, RangeAlert}; +use craft::ui::text::{NumberField, NumberValue, RangeAlert, TextWords}; use photocraft_color::BlendMode; use super::heal::{check, choose, string_entries}; @@ -75,12 +75,12 @@ pub(super) fn sponge(ui: &mut egui::Ui, app: &mut PhotocraftApp) -> Option Option { Some(f64::from(self.0)) } @@ -98,10 +98,13 @@ impl NumberValue for SmudgeAngle { } } -fn smudge_angle(ui: &mut egui::Ui, app: &mut PhotocraftApp) -> Option { +/// The truncating Angle field over the active tool's own brush. `invalid_alert`: text that does not +/// parse shows the reference's "Invalid numeric entry" alert when the edit ends (Color Replacement, +/// #11929); without it the field keeps the last value silently, as Smudge's always did. +fn truncated_angle(ui: &mut egui::Ui, app: &mut PhotocraftApp, invalid_alert: bool) -> Option { let before = (*app.session.tools.brush).clone(); let mut brush = before.clone(); - let mut angle = SmudgeAngle(brush.angle); + let mut angle = TruncatedAngle(brush.angle); let shown = Field::new(tl!("Angle")) .side(SideLabel::new().scrub(true).icon(IconSpec::svg(IconId::new("rotate_cw"))).layout(FieldLayout::Inside)) .sizing(Sizing::Content) @@ -122,14 +125,39 @@ fn smudge_angle(ui: &mut egui::Ui, app: &mut PhotocraftApp) -> Option(ui: &egui::Ui, app: &mut PhotocraftApp, edit: &EditResponse) { + let id = edit.response.id.with("invalid_angle_entry"); + let was_unparsed = ui.data(|d| d.get_temp::(id)).unwrap_or(false); + ui.data_mut(|d| d.insert_temp(id, edit.unparsed)); + let entered = edit.unparsed && ui.input(|i| i.key_pressed(egui::Key::Enter)); + if entered { + ui.memory_mut(|m| m.surrender_focus(edit.response.id)); + } + if !entered && !(was_unparsed && edit.text_lost_focus) { + return; + } + let env = Env::of(ui); + let end = |n: f64| env.words::().range_end.fill(&[("n", env.units().number(n, 0..=0).as_str()), ("unit", "°")]); + let message = crate::i18n::fmt( + tl!("Invalid numeric entry. An integer between {low} and {high} is required. Restored last valid value."), + &[("low", end(-180.0).as_str()), ("high", end(180.0).as_str())], + ); + crate::dialogs::alerts::alert(app, message); +} + +/// The pressure-for-size switch of Smudge and Color Replacement edits the active tool's own brush +/// through the same command and gesture path as the current Background Eraser controls. +fn brush_pressure_size(ui: &mut egui::Ui, app: &mut PhotocraftApp) { let before = (*app.session.tools.brush).clone(); let mut brush = before.clone(); let pressure = ToggleButton::new(tl!("Always use pressure for size"), Bind::given(brush.pressure_size)) @@ -152,7 +180,7 @@ pub(super) fn blur_sharpen_smudge(ui: &mut egui::Ui, app: &mut PhotocraftApp, to // The cell writes only on a commit, so a shared fraction is not rounded (#12650). let strength = number(tl!("Strength"), bind_tracked!(app.ui.tool_options => strength), 1.0..=100.0).whole().unit("%").show(ui).out_of_range; let angle = match tool { - Tool::Smudge => smudge_angle(ui, app), + Tool::Smudge => truncated_angle(ui, app, false), Tool::Sharpen => brush_angle(ui, app), _ => None, }; @@ -167,7 +195,7 @@ pub(super) fn blur_sharpen_smudge(ui: &mut egui::Ui, app: &mut PhotocraftApp, to } if tool == Tool::Smudge { check(ui, tl!("Finger Painting"), bind_tracked!(app.ui.tool_options => finger_painting), true, None); - smudge_pressure_size(ui, app); + brush_pressure_size(ui, app); } strength.or(angle) } @@ -212,9 +240,16 @@ pub(super) fn color_replacement(ui: &mut egui::Ui, app: &mut PhotocraftApp) -> O .show(ui) .out_of_range; check(ui, tl!("Anti-alias"), bind_tracked!(app.ui.tool_options => cr_anti_alias), true, Some(tl!("Anti-alias brush application"))); - tolerance + // The tool's own brush tip, as the Brush Settings panel shows it (#11929). + let angle = truncated_angle(ui, app, true); + brush_pressure_size(ui, app); + tolerance.or(angle) } #[cfg(test)] #[path = "../../../sharpen_bar_angle_tests.rs"] mod sharpen_bar_angle_tests; + +#[cfg(test)] +#[path = "../../../color_replacement_bar_angle_tests.rs"] +mod color_replacement_bar_angle_tests; From 6ac592c2d07bbd5ab8f616e109523667a5bdc11c Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 1 Dec 2026 00:51:30 +0100 Subject: [PATCH 7/7] Color Replacement: keep the first use's live tip; the bar test visits the tool first (#11929) Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/canvas/paint_mouse.rs | 4 +--- crates/ui-egui/src/color_replacement_bar_angle_tests.rs | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/canvas/paint_mouse.rs b/crates/ui-egui/src/canvas/paint_mouse.rs index 641ed22244..c50a15c7b0 100644 --- a/crates/ui-egui/src/canvas/paint_mouse.rs +++ b/crates/ui-egui/src/canvas/paint_mouse.rs @@ -82,9 +82,7 @@ pub fn sync_tool_brush(app: &mut PhotocraftApp) { }; // The Remove tool starts at its measured 19 px; the rest is the current brush (#12548). let size = if tool == Tool::Remove { REMOVE_TOOL_SIZE } else { current.size }; - // Color Replacement's Angle and Pressure for Size are its own bar controls: 0° and off (#11929). - let (angle, pressure_size) = if tool == Tool::ColorReplacement { (0.0, false) } else { (current.angle, current.pressure_size) }; - BrushSettings { smoothing: first_smoothing(), mode: photocraft_color::BlendMode::Normal, opacity, flow, size, angle, pressure_size, ..current } + BrushSettings { smoothing: first_smoothing(), mode: photocraft_color::BlendMode::Normal, opacity, flow, size, ..current } } }; // The picked preset was the outgoing tool's brush: forget it, so Update Brush can't overwrite diff --git a/crates/ui-egui/src/color_replacement_bar_angle_tests.rs b/crates/ui-egui/src/color_replacement_bar_angle_tests.rs index 441184202f..ae245c129b 100644 --- a/crates/ui-egui/src/color_replacement_bar_angle_tests.rs +++ b/crates/ui-egui/src/color_replacement_bar_angle_tests.rs @@ -116,6 +116,10 @@ fn typed_angles_are_clamped_and_truncated_with_the_reference_alerts() { #[test] fn both_controls_write_the_color_replacement_brush_only_and_share_it_with_brush_settings() { let mut h = bar(); + // Color Replacement's brush exists from its first use on (that use takes the live tip); only + // then does the Brush get values of its own. + select(&mut h, Tool::ColorReplacement); + select(&mut h, Tool::Brush); h.state_mut().run("tools.setBrush", json!({"brush": {"angle": -30.0, "pressureSize": true}})).unwrap(); select(&mut h, Tool::ColorReplacement); assert_eq!(shown_angle(&h), Some(0.0), "Color Replacement shows its own brush, not the Brush's");