mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Color Replacement: size follows pen pressure from the first use and after Reset Tool (#17785)
A first pick of the tool now starts its "Size:" control at Pen Pressure, as Reset Tool already did (#11931), so a pen stroke is the tip size times the pressure. Tests for both and for the stroke width at 0.25, 0.5 and full pressure; the Brush tool keeps its own default. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
061e54ad65
commit
7e43e330c4
3 files changed
+99
-2
No files matched your search
@@ -82,7 +82,12 @@ 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 }
|
||||
let mut b = BrushSettings { smoothing: first_smoothing(), mode: photocraft_color::BlendMode::Normal, opacity, flow, size, ..current };
|
||||
// Color Replacement's size follows pen pressure from its first use, as after Reset Tool (#17785).
|
||||
if tool == Tool::ColorReplacement {
|
||||
crate::bg_eraser_popup::set_size_control(&mut b, crate::bg_eraser_popup::PEN_PRESSURE);
|
||||
}
|
||||
b
|
||||
}
|
||||
};
|
||||
// The picked preset was the outgoing tool's brush: forget it, so Update Brush can't overwrite
|
||||
@@ -173,7 +178,7 @@ pub(crate) fn fresh_brush(tool: Tool, current: &BrushSettings) -> BrushSettings
|
||||
Tool::Smudge => (brush.size, brush.hardness, brush.spacing) = SMUDGE_TOOL_TIP,
|
||||
Tool::ColorReplacement => {
|
||||
(brush.size, brush.hardness, brush.spacing) = COLOR_REPLACEMENT_TIP;
|
||||
// Its popup's "Size:" starts at Pen Pressure (#11931).
|
||||
// Its popup's "Size:" starts at Pen Pressure, so strokes thin with lighter pressure (#11931, #17785).
|
||||
crate::bg_eraser_popup::set_size_control(&mut brush, crate::bg_eraser_popup::PEN_PRESSURE);
|
||||
}
|
||||
Tool::CloneStamp => {
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
//! #17785: the Color Replacement tool's size follows pen pressure out of the box and after Reset Tool, as in the
|
||||
//! reference application: a pen stroke's width is the tip size times the pressure (41 px at 0.5 -> about 23 rows, at
|
||||
//! 0.25 about 11), the mouse paints at full size. The Brush tool keeps its own default, which ignores pressure.
|
||||
|
||||
use egui::Modifiers;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::bg_eraser_popup::{PEN_PRESSURE, size_control};
|
||||
use crate::canvas::{ToolEvent, tool_event};
|
||||
use crate::state::Tool;
|
||||
|
||||
/// A 200×100 red document, foreground green, with `tool` picked and reset, then its tip set to 41 px with no
|
||||
/// smoothing (the rest of the reset brush kept).
|
||||
fn app(tool: Tool) -> Box<PhotocraftApp> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 200, "height": 100, "background": "#ff0000"})).unwrap();
|
||||
app.run("tools.setColors", json!({"foreground": "#00ff00", "background": "#0000ff"})).unwrap();
|
||||
app.ui.tool = tool;
|
||||
crate::paint_mouse::sync_tool_brush(&mut app);
|
||||
reset(&mut app);
|
||||
app.run("tools.setBrush", json!({"brush": {"size": 41, "smoothing": {"amount": 0.0}}})).unwrap();
|
||||
app
|
||||
}
|
||||
|
||||
fn reset(app: &mut PhotocraftApp) {
|
||||
let ctx = app.ctx().clone();
|
||||
crate::menus::invoke(app, &ctx, crate::tools::tool_reset::RESET, json!({})).unwrap();
|
||||
}
|
||||
|
||||
/// A horizontal stroke across the middle at a constant `pressure` (1.0 is what the mouse sends).
|
||||
fn stroke(app: &mut PhotocraftApp, pressure: f32) {
|
||||
let m = Modifiers::NONE;
|
||||
tool_event(app, ToolEvent::Down { x: 30.0, y: 50.0, pressure }, m);
|
||||
for x in (40..=170).step_by(10) {
|
||||
tool_event(app, ToolEvent::Move { x: f64::from(x), y: 50.0, pressure }, m);
|
||||
}
|
||||
tool_event(app, ToolEvent::Up { x: 170.0, y: 50.0 }, m);
|
||||
assert!(!app.ui.status_error, "{}", app.ui.status);
|
||||
}
|
||||
|
||||
/// How many rows of the column in the stroke's middle no longer hold the document's red.
|
||||
fn rows_changed(app: &PhotocraftApp) -> usize {
|
||||
let st = app.session.active().unwrap();
|
||||
let surface = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap();
|
||||
(0..100).filter(|&y| (surface.rgba(100, y)[1]) > 0.02).count()
|
||||
}
|
||||
|
||||
fn width_at(tool: Tool, pressure: f32) -> usize {
|
||||
let mut app = app(tool);
|
||||
stroke(&mut app, pressure);
|
||||
rows_changed(&app)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fresh_tool_and_reset_tool_put_size_on_pen_pressure() {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 50, "height": 50})).unwrap();
|
||||
app.ui.tool = Tool::ColorReplacement;
|
||||
crate::paint_mouse::sync_tool_brush(&mut app);
|
||||
assert_eq!(size_control(&app.session.tools.brush), PEN_PRESSURE, "a fresh tool");
|
||||
// Turned off, Reset Tool puts it back on Pen Pressure.
|
||||
let mut brush = app.session.tools.brush.clone();
|
||||
crate::bg_eraser_popup::set_size_control(&mut brush, crate::bg_eraser_popup::OFF);
|
||||
app.session.tools.brush = brush;
|
||||
app.session.tools.brush_changed();
|
||||
assert_ne!(size_control(&app.session.tools.brush), PEN_PRESSURE);
|
||||
reset(&mut app);
|
||||
assert_eq!(size_control(&app.session.tools.brush), PEN_PRESSURE, "after Reset Tool");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pen_stroke_is_the_tip_size_times_the_pressure() {
|
||||
let (quarter, half, mouse) = (width_at(Tool::ColorReplacement, 0.25), width_at(Tool::ColorReplacement, 0.5), width_at(Tool::ColorReplacement, 1.0));
|
||||
// The reference: 41 px at 0.25 -> 11 rows, 0.5 -> 23, the mouse 43.
|
||||
assert!(quarter.abs_diff(11) <= 2, "0.25 pressure paints about 11 rows, got {quarter}");
|
||||
assert!(half.abs_diff(23) <= 2, "0.5 pressure paints about 23 rows, got {half}");
|
||||
assert!(mouse.abs_diff(43) <= 3, "the mouse paints the full tip, got {mouse}");
|
||||
assert!(quarter < half && half < mouse, "lighter pressure paints thinner: {quarter} < {half} < {mouse}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_brush_tool_keeps_a_full_size_stroke_at_low_pressure() {
|
||||
let mut app = app(Tool::Brush);
|
||||
assert_ne!(size_control(&app.session.tools.brush), PEN_PRESSURE, "the Brush tool's size is not on Pen Pressure");
|
||||
assert!(!app.session.tools.brush.pressure_size);
|
||||
stroke(&mut app, 0.25);
|
||||
let light = rows_changed(&app);
|
||||
assert!(light >= width_at(Tool::Brush, 1.0) - 2, "the Brush tool ignores pressure by default: {light} rows at 0.25");
|
||||
}
|
||||
@@ -874,6 +874,8 @@ mod pen_variants_tests;
|
||||
#[cfg(test)]
|
||||
mod color_replacement_digit_keys_tests;
|
||||
#[cfg(test)]
|
||||
mod color_replacement_size_pressure_default_tests;
|
||||
#[cfg(test)]
|
||||
mod color_replacement_tests;
|
||||
#[cfg(test)]
|
||||
mod eyedropper_shift_sampler_tests;
|
||||
|
||||
Reference in new issue
Block a user