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

The 8-bit stroke dither (render.rs `dither8`) ran on every stroke, including
aliased ones. An aliased stroke paints each pixel by the same amount, so it
has no gradient to band; dithering only scattered it over neighbouring
levels. A 1 px Pencil line of 219/41/61 at 30% opacity over white came out
as four colours (244/191/197, 245/191/197, 244/190/196, ...), and a
foreground between 8-bit levels did the same at 100%. Photoshop's Pencil
paints one value, and pixel art relies on it: a tolerance-0 Magic Wand or
Paint Bucket and Indexed Color palettes trip on the grain.

Composite now skips the dither when the brush is aliased (Pencil, Eraser
Pencil and Block modes). Soft brushes keep it.

Test: an_aliased_stroke_at_partial_opacity_is_one_flat_colour (fails
before the fix at pixel 5). photocraft-paint 96/96, photocraft-engine
1529/1529, ui-egui pencil 13/13, clippy -D warnings and fmt clean.

Co-authored-by: cloudalister <ngnlif3@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-10-12 18:10:29 -04:00
1 parent e753a71851
commit ddf6696dce
2 files changed
+20 -2

No files matched your search

+15
View File
@@ -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");
}
}
+5 -2
View File
@@ -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 {