The Error Diffusion Algorithm
Floyd-Steinberg dithering works by processing each pixel left-to-right, top-to-bottom. When a pixel is quantized to the nearest available palette color, the quantization error (difference between original and quantized) is distributed to four neighboring unprocessed pixels.
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const old = pixels[y][x];
const new_ = nearestPaletteColor(old);
pixels[y][x] = new_;
const err = old - new_;
pixels[y][x+1] += err * 7/16;
pixels[y+1][x-1] += err * 3/16;
pixels[y+1][x] += err * 5/16;
pixels[y+1][x+1] += err * 1/16;
}
}Why It Works
The distribution pattern creates organic, noise-like patterns that the human eye interprets as smooth gradations. The key insight is that error is never lost — it is always pushed forward to unprocessed pixels, so the overall brightness and color of regions is preserved even with a tiny palette.
Aesthetic Applications
Used extensively in early computer graphics (Mac OS, Game Boy, newspaper print), Floyd-Steinberg dithering is now prized in algorithmic art for its ability to create rich textures from limited palettes. Combined with intentional palette selection, it produces a distinctive retro aesthetic that is both nostalgic and visually striking.
