diff --git a/crates/algo/src/paint.rs b/crates/algo/src/paint.rs index 4862304786..a7f57c337d 100644 --- a/crates/algo/src/paint.rs +++ b/crates/algo/src/paint.rs @@ -152,7 +152,8 @@ pub fn paint_gradient( FillMode::Blend(blend_mode), |x, y| opacity * selection.map_or(1.0, |m| m.sample_channel(x, y, 0)), |x, y| { - let t = tool_gradient_t(shape, from, to, x as f32 + 0.5, y as f32 + 0.5); + // Sampled one pixel up and left of the pixel centre, where the reference application samples (#12018). + let t = tool_gradient_t(shape, from, to, x as f32 - 0.5, y as f32 - 0.5); let mut c = sample_stops(stops, if reverse { 1.0 - t } else { t }); if dither { // One quantisation step of monochromatic noise breaks 8-bit banding without speckle. @@ -262,6 +263,10 @@ pub fn bucket_fill_region_inks( composite_area(s, b, mode, |x, y| region.at(x, y) * opacity * selection.map_or(1.0, |m| m.sample_channel(x, y, 0)), src, lock_transparency, inks); } +#[cfg(test)] +#[path = "paint_gradient_sample_tests.rs"] +mod paint_gradient_sample_tests; + #[cfg(test)] mod tests { use super::*; @@ -287,8 +292,8 @@ mod tests { paint_gradient( &mut s, a, - (0.5, 0.0), - (10.5, 0.0), + (-0.5, 0.0), + (9.5, 0.0), GradientShape::Linear, &[(0.0, [0.0, 0.0, 0.0, 1.0]), (1.0, [1.0, 1.0, 1.0, 1.0])], false, diff --git a/crates/algo/src/paint_gradient_sample_tests.rs b/crates/algo/src/paint_gradient_sample_tests.rs new file mode 100644 index 0000000000..2b30439ba4 --- /dev/null +++ b/crates/algo/src/paint_gradient_sample_tests.rs @@ -0,0 +1,66 @@ +//! #12018: the Gradient tool samples each pixel where the reference application does, so an Angle +//! gradient's start row and Radial values match its measurements. +//! +//! The ramp is the reference application's measured Classic black → white Linear curve (Smooth, +//! every tenth of the way), so every shape is checked against the same colour curve. + +use photocraft_color::PixelFormat; +use photocraft_raster::Surface; + +use super::{BlendMode, GradientShape, Rect, paint_gradient}; + +const LINEAR: [f32; 11] = [0.0, 2.0, 17.0, 42.0, 70.0, 99.0, 129.0, 162.0, 196.0, 228.0, 255.0]; + +fn ramp() -> Vec<(f32, [f32; 4])> { + LINEAR.iter().enumerate().map(|(i, v)| (i as f32 / 10.0, [v / 255.0, v / 255.0, v / 255.0, 1.0])).collect() +} + +/// An 800 × 600 RGB8 gradient from `from` to `to`, dither off. +fn painted(shape: GradientShape, from: (f32, f32), to: (f32, f32)) -> Surface { + let mut s = Surface::new(PixelFormat::RGBA8); + paint_gradient(&mut s, Rect::new(0, 0, 800, 600), from, to, shape, &ramp(), false, 1.0, BlendMode::Normal, false, None, false); + s +} + +fn check(s: &Surface, shape: GradientShape, expected: &[((i32, i32), f32, f32)]) { + for &((x, y), want, within) in expected { + let got = s.pixel(x, y)[0] * 255.0; + assert!((got - want).abs() <= within, "{shape:?} ({x},{y}): {got} expected {want} ± {within}"); + } +} + +#[test] +fn an_angle_gradient_starts_with_the_end_colour_east_of_the_start() { + let s = painted(GradientShape::Angle, (400.0, 300.0), (600.0, 300.0)); + check(&s, GradientShape::Angle, &[((450, 300), 254.5, 0.5), ((700, 300), 254.5, 0.5), ((500, 400), 4.0, 2.0), ((400, 200), 179.0, 2.0)]); + // Further round, clockwise on screen. + check(&s, GradientShape::Angle, &[((400, 400), 30.0, 1.0), ((200, 300), 99.5, 1.0), ((500, 200), 220.0, 2.0)]); +} + +#[test] +fn radial_values_match_the_reference() { + let s = painted(GradientShape::Radial, (400.0, 300.0), (600.0, 300.0)); + check(&s, GradientShape::Radial, &[((400, 300), 0.0, 1.0), ((450, 300), 28.0, 1.0), ((300, 300), 100.0, 1.0), ((400, 200), 100.0, 1.0)]); + check(&s, GradientShape::Radial, &[((500, 300), 98.0, 1.0), ((400, 400), 98.0, 1.0), ((700, 300), 255.0, 0.0)]); +} + +#[test] +fn linear_values_stay_within_one_level() { + let s = painted(GradientShape::Linear, (100.0, 300.0), (700.0, 300.0)); + let expected: Vec<_> = LINEAR.iter().enumerate().map(|(i, &v)| ((100 + 60 * i as i32, 300), v, 1.0)).collect(); + check(&s, GradientShape::Linear, &expected); +} + +#[test] +fn reflected_and_diamond_values_match_the_reference() { + let s = painted(GradientShape::Reflected, (400.0, 300.0), (600.0, 300.0)); + check(&s, GradientShape::Reflected, &[((300, 300), 100.0, 1.0), ((500, 300), 98.0, 1.0), ((400, 100), 0.0, 1.0), ((400, 500), 0.0, 1.0)]); + let s = painted(GradientShape::Diamond, (400.0, 300.0), (600.0, 300.0)); + // The ramp is straight between its tenths, so (300,300) and (400,200) land 1.5 below the + // reference's 102, where today's sample point lands 3 below. + check( + &s, + GradientShape::Diamond, + &[((300, 300), 102.0, 2.0), ((500, 300), 99.0, 1.0), ((400, 200), 102.0, 2.0), ((450, 250), 99.0, 1.0), ((500, 200), 255.0, 0.0)], + ); +} diff --git a/crates/engine/src/cmd/gradient_fill_cmds/tests.rs b/crates/engine/src/cmd/gradient_fill_cmds/tests.rs index 3c0c2f3c6a..12db3308d3 100644 --- a/crates/engine/src/cmd/gradient_fill_cmds/tests.rs +++ b/crates/engine/src/cmd/gradient_fill_cmds/tests.rs @@ -84,12 +84,14 @@ fn parity(depth: u32, background: &str, extra: Value, select: bool, tol: f32) { s.execute("select.rect", json!({"x": 6, "y": 3, "width": 30, "height": 22, "ellipse": true, "antiAlias": true})).unwrap(); } // Linear and Reflected fill layers sample each pixel's top-left corner - // (compose::effects::gradient_t, fitted on Photoshop's fills); the painted - // gradient samples centres, so its drag is half a pixel further along. + // (compose::effects::gradient_t, fitted on Photoshop's fills), the others its + // centre; the painted gradient samples a pixel up and left of the centre (#12018), + // so its drag is moved by the difference. let mut p = p.clone(); - if !live && matches!(style, "linear" | "reflected") { - p["from"] = json!([from[0] + 0.5, from[1] + 0.5]); - p["to"] = json!([to[0] + 0.5, to[1] + 0.5]); + if !live { + let d = if matches!(style, "linear" | "reflected") { -0.5 } else { -1.0 }; + p["from"] = json!([from[0] + d, from[1] + d]); + p["to"] = json!([to[0] + d, to[1] + d]); } s.execute(if live { CREATE } else { "paint.gradient" }, p).unwrap(); photocraft_compose::flatten(&s.active().unwrap().doc) diff --git a/crates/engine/src/cmd/paint_cmds/tests.rs b/crates/engine/src/cmd/paint_cmds/tests.rs index ba30e9d719..bbcc8f7012 100644 --- a/crates/engine/src/cmd/paint_cmds/tests.rs +++ b/crates/engine/src/cmd/paint_cmds/tests.rs @@ -248,7 +248,8 @@ fn bucket_fills_with_a_pattern() { #[test] fn gradient_tool_with_selection_and_reverse() { let mut s = session(); - s.execute("paint.gradient", json!({"from": [0, 0], "to": [20, 0], "colors": ["#000000", "#ffffff"]})).unwrap(); + // Pixels are sampled a pixel up and left of their centre (#12018), so pixel 19 reads 18.5 of 19. + s.execute("paint.gradient", json!({"from": [0, 0], "to": [19, 0], "colors": ["#000000", "#ffffff"]})).unwrap(); assert!(px(&s, 0, 5)[0] < 0.05 && px(&s, 19, 5)[0] > 0.95); s.execute("paint.gradient", json!({"from": [0, 0], "to": [20, 0], "colors": ["#000000", "#ffffff"], "reverse": true})).unwrap(); assert!(px(&s, 0, 5)[0] > 0.95); diff --git a/crates/engine/src/presets/tests.rs b/crates/engine/src/presets/tests.rs index 0bb3a13857..88647ef23e 100644 --- a/crates/engine/src/presets/tests.rs +++ b/crates/engine/src/presets/tests.rs @@ -246,7 +246,8 @@ fn paint_gradient_honours_explicit_transparency_stops() { ] { let mut s = Session::new(); s.execute("file.new", json!({"width": 16, "height": 16, "depth": depth, "background": "transparent"})).unwrap(); - let mut p = json!({"from": [0, 0], "to": [0, 15], "transparency": transparency}); + // Pixel 15 is sampled at 14.5 (#12018), so a drag to 14 ends on it. + let mut p = json!({"from": [0, 0], "to": [0, 14], "transparency": transparency}); for (k, v) in extra.as_object().unwrap() { p[k] = v.clone(); } diff --git a/crates/ui-egui/src/canvas/channel_view/tests.rs b/crates/ui-egui/src/canvas/channel_view/tests.rs index b7befd81a1..965ad38208 100644 --- a/crates/ui-egui/src/canvas/channel_view/tests.rs +++ b/crates/ui-egui/src/canvas/channel_view/tests.rs @@ -157,7 +157,9 @@ fn layer_mask_views_and_damage_on_the_downsampled_grid() { s.execute("layer.new.layer", json!({})).unwrap(); s.execute("edit.fill", json!({"color": "#0000ff"})).unwrap(); s.execute("layer.layerMask.revealAll", json!({})).unwrap(); - s.execute("paint.gradient", json!({"from": [0, 0], "to": [37, 0], "colors": ["#000000", "#ffffff"], "target": "mask"})).unwrap(); + // Pixels are sampled half a pixel up and left of their centre (#12018): a drag ending on 36 + // keeps the last column (sampled at 35.5) near white. + s.execute("paint.gradient", json!({"from": [0, 0], "to": [36, 0], "colors": ["#000000", "#ffffff"], "target": "mask"})).unwrap(); s.execute("view.layerMask", json!({"mode": "gray"})).unwrap(); let st = s.active().unwrap(); let b = st.doc.bounds();