Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Pencil: no 8-bit dither on aliased strokes, one flat colour at partial opacity (#3990)

John Alisterddf6696dce5a
diff --git a/crates/paint/src/brush_tests.rs b/crates/paint/src/brush_tests.rs
index 664aca9c0..04f74ed84 100644
--- a/crates/paint/src/brush_tests.rs
+++ b/crates/paint/src/brush_tests.rs
@@ -1400,3 +1400,18 @@ fn dithering_never_moves_an_exact_colour_or_an_untouched_pixel() {
}
assert_eq!(s.rgba(50, 5), [1.0, 1.0, 1.0, 1.0], "far from the stroke: untouched");
}
+
+#[test]
+fn an_aliased_stroke_at_partial_opacity_is_one_flat_colour() {
+ // 30% of red 219/41/61 over white lands between 8-bit levels (244.2, 190.7, 196.8); dithering
+ // used to scatter the Pencil's line over several neighbouring levels.
+ let mut s = Surface::with_default(PixelFormat::RGBA8, &[1.0, 1.0, 1.0, 1.0]);
+ let color = [219.0 / 255.0, 41.0 / 255.0, 61.0 / 255.0, 1.0];
+ let b = BrushSettings { size: 1.0, aliased: true, opacity: 0.3, color, pressure_size: false, ..Default::default() };
+ render_stroke(&mut s, &b, &line(2.0, 60.0, 5.0), None, false, 1.0);
+ let first = s.rgba(3, 5);
+ assert_ne!(first, [1.0, 1.0, 1.0, 1.0], "the line was painted");
+ for x in 3..60 {
+ assert_eq!(s.rgba(x, 5), first, "pixel {x} of the Pencil line");
+ }
+}
diff --git a/crates/paint/src/render.rs b/crates/paint/src/render.rs
index 94ac63a79..688232c93 100644
--- a/crates/paint/src/render.rs
+++ b/crates/paint/src/render.rs
@@ -970,8 +970,11 @@ impl StrokeRenderer {
src[a] = b.color[3];
}
// 8-bit targets: the pixels the stroke changes are dithered as they are quantised, so
- // soft, low-flow strokes don't band (see `dither8`).
- let dither = fmt.sample == SampleType::U8;
+ // soft, low-flow strokes don't band (see `dither8`). Aliased strokes (the Pencil, the
+ // Eraser's Pencil and Block modes) paint every pixel by the same amount, so they have no
+ // gradient to band and are not dithered: a Pencil line at partial opacity is one flat
+ // colour, as in Photoshop, not grain that a tolerance-0 Magic Wand or Paint Bucket trips on.
+ let dither = fmt.sample == SampleType::U8 && !b.aliased;
let mut dmg = Rect::EMPTY;
let mut region = Vec::new();
for (tx, ty) in keys {