Dithering
First appeared 1976 · Robert W. Floyd, Louis Steinberg — Floyd–Steinberg error-diffusion dithering (1976). · timeline ↗
community / folk Name — Descriptive term predating computing (mechanical "dither"); the named eponymous variant is Floyd–Steinberg (1976). The general name is descriptive, not a personal eponym.
In one line Representing continuous tones with a limited palette by patterning the available colours so the eye blends them — via error diffusion (Floyd–Steinberg) or ordered/threshold matrices (Bayer).
The code
The actual sketch behind the demo above — sandboxed and reproducible. This is the whole thing.
dithering/index.html
const SEED = Math.floor(Math.random()*1e9);
// Floyd-Steinberg error-diffusion dithering of a smooth noise gradient
// down to a small palette.
const RES = 200;
function setup() {
noiseSeed(SEED); randomSeed(SEED);
const s = Math.min(windowWidth, windowHeight);
pixelDensity(1);
const cnv = createCanvas(RES, RES);
cnv.elt.style.width = s + 'px';
cnv.elt.style.height = s + 'px';
const PALETTES = [
[[20,20,30],[40,60,120],[230,90,70],[245,240,225]],
[[10,10,10],[80,80,80],[170,170,170],[245,245,245]],
[[18,38,32],[42,120,110],[225,200,90],[245,240,225]],
];
const palette = PALETTES[Math.floor(random(PALETTES.length))];
// Build a smooth source field (luminance from layered noise).
const FREQ = 0.012;
const src = new Float32Array(RES * RES * 3);
for (let y = 0; y < RES; y++) {
for (let x = 0; x < RES; x++) {
const v = noise(x * FREQ, y * FREQ) * 0.7 + noise(x * FREQ * 4, y * FREQ * 4) * 0.3;
const i = (y * RES + x) * 3;
// map scalar through the palette endpoints for a tinted gradient
const lo = palette[0], hi = palette[palette.length - 1];
src[i] = lo[0] + (hi[0] - lo[0]) * v;
src[i+1] = lo[1] + (hi[1] - lo[1]) * v;
src[i+2] = lo[2] + (hi[2] - lo[2]) * v;
}
}
const nearest = (r, g, b) => {
let best = palette[0], bd = Infinity;
for (const c of palette) {
const d = (r-c[0])**2 + (g-c[1])**2 + (b-c[2])**2;
if (d < bd) { bd = d; best = c; }
}
return best;
};
// Floyd-Steinberg
for (let y = 0; y < RES; y++) {
for (let x = 0; x < RES; x++) {
const i = (y * RES + x) * 3;
const old = [src[i], src[i+1], src[i+2]];
const nw = nearest(old[0], old[1], old[2]);
const err = [old[0]-nw[0], old[1]-nw[1], old[2]-nw[2]];
src[i] = nw[0]; src[i+1] = nw[1]; src[i+2] = nw[2];
const spread = (dx, dy, f) => {
const xx = x + dx, yy = y + dy;
if (xx < 0 || xx >= RES || yy < 0 || yy >= RES) return;
const j = (yy * RES + xx) * 3;
src[j] += err[0] * f;
src[j+1] += err[1] * f;
src[j+2] += err[2] * f;
};
spread(1, 0, 7/16);
spread(-1, 1, 3/16);
spread(0, 1, 5/16);
spread(1, 1, 1/16);
}
}
loadPixels();
for (let i = 0; i < RES * RES; i++) {
pixels[i*4] = src[i*3];
pixels[i*4+1] = src[i*3+1];
pixels[i*4+2] = src[i*3+2];
pixels[i*4+3] = 255;
}
updatePixels();
}How it works
Two main families: 1. Error diffusion (Floyd–Steinberg): scan pixels in order; for each, pick the nearest palette colour, compute the quantisation error (original − chosen), and spread that error to not-yet-processed neighbours by fixed weights (e.g. 7/16 right, 3/16 / 5/16 / 1/16 below). Errors thus "average out" and tones are preserved. 2. Ordered dithering (Bayer): compare each pixel's value against a tiled threshold matrix; output the lighter or darker palette colour depending on whether it exceeds the cell's threshold. This is parallel and produces the characteristic crosshatch/halftone texture. Both trade spatial resolution for apparent tonal depth — the basis of 1-bit and retro looks.
Parameters
method: string — Dithering technique — ordered (Bayer matrix) or error-diffusion (Floyd–Steinberg).matrixSize: number — Bayer matrix size for ordered dithering (e.g. 2, 4, 8).levels: number — Number of quantisation levels kept per channel.palette: array — The reduced set of output colours the image is mapped to.
Lineage
Frequently combined with
- Fragment Shader (GLSL) · 4 works
- Plotter-style Hatching · 4 works
- Perlin / Simplex Noise · 2 works
- Instanced Geometry · 1 work
- 3D Mesh / Geometry · 1 work
- Glitch / Datamosh · 1 work
- Modular Assembly / Parametric Character · 1 work
- HSL / Hue Cycle · 1 work
- Bezier / Catmull-Rom Splines · 1 work
Works using this algorithm (12)
Last updated 2026-07-16