Why RGB distance gives you the wrong beads: quantizing a photo to a pegboard palette in JavaScript
Most people picture the hard part of turning a photo into a bead pattern as the art. It isn't. The hard part is a small piece of colour maths: you have millions of possible pixel colours, and a pegboard where every peg takes one of a few dozen bead colours. Whatever sits between those two facts decides whether a face looks like a face or like a bruise.
We run a small site for printable fuse-bead patterns, and one of its tools converts a picture into a bead grid. This post is the part of that problem you can reproduce in about forty lines of JavaScript. It is a toy version, not a copy of our production code, and the numbers below come from a script I ran while writing this.
The setup: grid first, colours second
A standard small pegboard is 29 × 29 pegs, which is 841 beads. Larger boards and tiled boards give you 40 × 40 (1,600), 50 × 50 (2,500) or 64 × 64 (4,096). Those are the grid sizes the photo-to-pattern converter offers, along with a custom size.
The order of operations matters:
- Centre-crop the photo to a square.
- Downscale it to the grid size. Each output pixel becomes one bead.
- Replace every pixel with the nearest colour in the bead palette.
- Count the beads per colour so you know what to buy.
Step 3 is where people go wrong, because "nearest" depends on what space you measure distance in.
Why RGB distance misleads you
The obvious implementation is Euclidean distance in RGB. It is easy, and it is wrong in a way you can see. RGB is not perceptually uniform: equal numeric steps do not look like equal steps to an eye. Greens are spread out, dark blues are crushed together.
CIE Lab was designed so that Euclidean distance roughly tracks perceived difference. Converting costs a few multiplications per palette colour, once, and a few per pixel.
const srgb = c => { c /= 255; return c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4); }; function lab([r, g, b]) { const R = srgb(r), G = srgb(g), B = srgb(b); const x = (0.4124*R + 0.3576*G + 0.1805*B) / 0.95047; const y = 0.2126*R + 0.7152*G + 0.0722*B; const z = (0.0193*R + 0.1192*G + 0.9505*B) / 1.08883; const f = t => t > 0.008856 ? Math.cbrt(t) : 7.787*t + 16/116; return [116*f(y) - 16, 500*(f(x) - f(y)), 200*(f(y) - f(z))]; } const d2 = (a, b) => (a[0]-b[0])**2 + (a[1]-b[1])**2 + (a[2]-b[2])**2; function nearest(pixel, paletteLab) { const p = lab(pixel); let best = 0, bestD = Infinity; paletteLab.forEach((q, i) => { const d = d2(p, q); if (d < bestD) { bestD = d; best = i; } }); return best; } Precompute paletteLab once. Never convert the palette inside the per-pixel loop.
A test you can run yourself
To see the difference I built a 29 × 29 synthetic image: a smooth ramp from light peach to brown, a stand-in for skin shading. I quantised it against a deliberately small ten-colour toy palette (black, white, red, orange, yellow, green, blue, purple, pink, brown). It is not the real bead list. A real palette has far more colours and the gap would be smaller.
Results over the 841 cells:
- The RGB and Lab methods picked a different bead for 553 of the 841 cells, about 66%.
- RGB: 541 cells became pink, 231 brown, 69 orange, and none white.
- Lab: 631 cells became brown, 153 white, 57 pink, and no orange.
Neither answer is pretty with ten colours, but they fail differently. RGB never reached for white, so the light end of the ramp turns pink. Lab pushes the light end to white and holds the shadows in brown, which is closer to how a pixel artist would shade it by hand. The practical lesson is that the choice of colour space changes your bead shopping list by hundreds of beads on a single small board.
Things that are easy to forget
Palette size is a trade-off. More colours means smoother gradients and a longer shopping list. For a first project, a design that uses six or seven colours is much easier to place than one that uses thirty.
Downscaling blurs. If you shrink a photo with plain averaging, edges go soft and the quantiser invents in-between colours that exist nowhere in the source. Boosting contrast before step 3, or removing the background, tends to help more than a cleverer distance function.
Dithering is optional and usually unkind to beads. Error-diffusion dithering looks great on a screen and looks like noise on a pegboard, because each bead is physically large. For small grids, flat regions read better.
Bead counts are the real output. A pattern is only useful if it tells you how many of each colour to buy. Count after quantising, not before.
Brand compatibility is mostly about spacing. Perler, Hama, Pyssla, Nabbi and Artkal beads in the 5 mm size share the same peg spacing, so a grid works across brands. Colour names and SKU codes differ though, so a palette is tied to one brand's list.
If you would rather skip the maths
Not everyone wants to calibrate a quantiser. If you only want something to put on a board this evening, we also publish a free 12-design cat starter pack sized for 29 × 29 pegs, where the colour choices are already made.
For the code above: swap the toy palette for a real brand list, add the crop and downscale steps, and you have the core of a converter. The part worth spending your time on is the test image. Build a ramp, a flat colour and a high-contrast edge, and look at what your quantiser does to each. Most bugs show up there before they show up in someone's finished bead portrait.