Dithering

Look at the lower gradient from a little farther away. Where do individual dots start to look like a third colour?
Mixing two colours in a pattern to fake the shades in between.
Both halves use exactly two colours. The top switches from one to the other in a single hard step. The bottom scatters them in a regular pattern, and your eye blends the dots into a smooth fade.
Code
namespace dithering_lesson;
import stage;
import std;
import math;
var painted = false;
export "dithering" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
if !painted {
let bayer = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5];
buffer_start(160, 100, 1);
for row in 50 {
for col in 80 {
let value = col / 80.0;
let threshold = if row < 25 { 0.5 } else { (bayer[(row % 4) * 4 + col % 4] + 0.5) / 16 };
rect(col * 2, row * 2, 2, 2, if value > threshold { SUN } else { "#3a2a7a" });
}
}
buffer_end();
painted = true;
}
artwork("draft");
buffer_draw(9, 10, 142, 80);
rect(9, 49, 142, 2, INK);
rect(clamp(floor(px + 0.5), 9.0, 150.0), 10, 1, 80, MAG);
}

Comments
No comments yet.