Few aesthetics bridge retro computing history and ultra-fast low-carbon web design like 1-bit and 2-color dithering (famously celebrated by Lucas Pope's Return of the Obra Dinn, Low-Tech Magazine's solar-powered site, and classic 1984 Macintosh HyperCard stacks).
Beyond nostalgic style, dithering solves a real performance problem: quantizing a 1200×800 photograph into a two-color brand palette (#1e3a8a cobalt ink on #ffffff paper) using a 4×4 Bayer matrix or Bill Atkinson's error-diffusion algorithm produces razor-sharp editorial illustrations that weigh 8 KB to 18 KB as lossless WebP or indexed PNG!
1. Comparing the Three Classic Dithering Algorithms
| Algorithm | Pattern Character | Parallelizable on GPU? | Best Visual Use Case |
|---|---|---|---|
4×4 Ordered Bayer Matrix |
Geometric crosshatch / checkerboard | Yes (O(1) per pixel) |
Technical diagrams, retro UI backdrops, WebGL shaders |
| Atkinson Error Diffusion (Mac 1984) | High-contrast organic stipple (75% error) | Sequential scanline | Editorial portraits, architectural photography |
| Floyd-Steinberg (1976) | Smooth full-tonal stipple (100% error) | Sequential scanline | Continuous photographic gradients |
2. Copy-Ready JavaScript 4×4 Bayer Matrix Dither on <canvas>
Because ordered Bayer dithering evaluates each (x, y) pixel independently against a normalized 0..15 threshold matrix, it processes a 1200×800 image in under 8 milliseconds in vanilla JavaScript:
// 4x4 Bayer threshold matrix (0..15)
const BAYER_4X4 = [
[ 0, 8, 2, 10],
[12, 4, 14, 6],
[ 3, 11, 1, 9],
[15, 7, 13, 5],
];
/**
* Dithers Canvas ImageData into two custom RGB colors [r, g, b]
*/
export function applyOrderedBayerDither(imageData, darkRgb = [30, 58, 138], lightRgb = [255, 255, 255]) {
const { data, width, height } = imageData;
for (let y = 0; y < height; y++) {
const row = BAYER_4X4[y & 3]; // Fast modulo 4 via bitwise AND
for (let x = 0; x < width; x++) {
const idx = (y * width + x) * 4;
// Perceptual Rec.709 luminance (0..255)
const lum = 0.2126 * data[idx] + 0.7152 * data[idx + 1] + 0.0722 * data[idx + 2];
const threshold = ((row[x & 3] + 0.5) / 16) * 255;
const chosen = lum < threshold ? darkRgb : lightRgb;
data[idx] = chosen[0];
data[idx + 1] = chosen[1];
data[idx + 2] = chosen[2];
data[idx + 3] = 255;
}
}
return imageData;
}
3. Bill Atkinson's 1984 Macintosh Dither in 25 Lines
Want organic stippled pen-and-ink grain instead of geometric crosshatching? Diffuse 1/8 (0.125) of each pixel's rounding error into six neighboring pixels (x+1, x+2, x-1 y+1, x y+1, x+1 y+1, y+2):
export function applyAtkinsonDither(imageData, threshold = 128) {
const { data, width, height } = imageData;
const lum = new Float32Array(width * height);
for (let i = 0; i < lum.length; i++) {
const p = i * 4;
lum[i] = 0.2126 * data[p] + 0.7152 * data[p + 1] + 0.0722 * data[p + 2];
}
const offsets = [
[1, 0], [2, 0],
[-1, 1], [0, 1], [1, 1],
[0, 2],
];
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const i = y * width + x;
const oldVal = lum[i];
const newVal = oldVal < threshold ? 0 : 255;
const err = (oldVal - newVal) / 8; // Retain 6/8 of error for high Mac contrast!
lum[i] = newVal;
for (const [dx, dy] of offsets) {
const nx = x + dx, ny = y + dy;
if (nx >= 0 && nx < width && ny < height) {
lum[ny * width + nx] += err;
}
}
const p = i * 4;
data[p] = data[p + 1] = data[p + 2] = newVal;
data[p + 3] = 255;
}
}
return imageData;
}
Export dithered canvases as lossless WebP (never lossy JPEG, which blurs 1px stipple dots!) and display with image-rendering: pixelated from our 88x31 & Crisp Pixel Scaling Guide.
4. Pre-Processing Contrast & Gamma Before Dithering (Why Raw Photos Look Flat)
If you feed an unadjusted daylight photograph straight into a 1-bit 128 threshold dither, midtone faces and clouds often collapse into uniform medium stipple with weak edge separation. Classic Macintosh HyperScan software and Game Boy Camera firmware ran two lightweight pre-filters before dithering:
- S-Curve Contrast Stretch (
1.15x–1.30x): Pushes dark shadows cleanly below25(pure ink) and bright highlights above235(pure paper) so stipple patterns concentrate along expressive midtone contours rather than peppering clean white backgrounds with stray dots! - Pre-Downscale to Virtual Pixel Grid (
2×Pixel Size): Dithering a1600×1000Retina image at1:1physical pixels makes stipple dots almost invisible to the naked eye (0.2mmpitch) while quadrupling file size! Downscaling to560×350first, dithering, and rendering at1120×700withimage-rendering: pixelatedproduces bold, tactile printmaking grain and cuts lossless WebP file size down to~7 KB!
// Apply gentle S-curve contrast boost (factor = 1.25) to luminance 0..255 before dithering
export function adjustLuminanceContrast(lum, contrastFactor = 1.25) {
const centered = (lum / 255 - 0.5) * contrastFactor + 0.5;
return Math.max(0, Math.min(255, centered * 255));
}
5. Multi-Level (4-Color) Indexed Retro Palettes Beyond 1-Bit
You aren't restricted to two colors! Classic Game Boy (4 shades) and CGA / Macintosh II (4–16 shades) graphics apply the exact same 4×4 Bayer matrix across palette intervals (N - 1 steps for N sorted palette colors).
For a bright editorial illustration using four cohesive slate-cobalt shades (#0f172a ink -> #1d4ed8 cobalt -> #93c5fd sky -> #ffffff paper):
const EDITORIAL_4_PALETTE = [
[15, 23, 42], // 0: Deep Slate Ink
[29, 78, 216], // 1: Royal Cobalt
[147, 197, 253], // 2: Soft Sky Tint
[255, 255, 255], // 3: Crisp White Paper
];
export function quantize4ColorBayer(lum0to255, bayer0to15) {
const levels = EDITORIAL_4_PALETTE.length - 1; // 3 intervals
const normalized = lum0to255 / 255;
const ditherOffset = (bayer0to15 / 16 - 0.5) * (1 / levels);
const index = Math.max(0, Math.min(levels, Math.round((normalized + ditherOffset) * levels)));
return EDITORIAL_4_PALETTE[index];
}
6. Exporting Dithered Art Without Destroying Crisp Dots
| Export Format | Preserves 1px Dither Dots? | Typical 640×400 Size |
Verdict |
|---|---|---|---|
| Lossless WebP | 100% Exact | 6.8 KB – 14.5 KB |
Best overall choice |
Indexed PNG-8 (2–4 colors) |
100% Exact | 8.2 KB – 17.0 KB |
Great universal fallback |
Lossy JPEG (q80) |
NO! Creates 8×8 DCT ringing blocks! |
42.0 KB (larger AND blurry!) |
Never use JPEG on dithered art! |
Notice that counterintuitive result in row 3: a lossy JPEG of a 1-bit dithered image is actually 3× larger in bytes AND visually ruined compared to lossless WebP, because 8×8 Discrete Cosine Transform blocks treat alternating black/white pixel stipples as extreme high-frequency noise! Always save pixel and dithered art losslessly, and check contrast pairs in our WCAG Contrast Checker.