diff --git a/crates/paint/src/replace.rs b/crates/paint/src/replace.rs index 65cc2c745c..f6230719de 100644 --- a/crates/paint/src/replace.rs +++ b/crates/paint/src/replace.rs @@ -78,6 +78,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()) } @@ -138,8 +142,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..8c06187f3a --- /dev/null +++ b/crates/paint/src/replace_tolerance_falloff_tests.rs @@ -0,0 +1,114 @@ +//! #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 `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 { + 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(end_x, 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).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() +} + +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 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]), + ( + 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, 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 }; + 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. 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 +/// 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).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:?}"); +}