Why Euclidean sRGB Fails at Palette Quantization (and Implementing CIEDE2000 in Pure JS)
Mapping arbitrary 24-bit photographs onto a discrete physical palette—such as 454 cotton embroidery skeins—exposes immediate perceptual flaws in naive Euclidean sRGB distance (Math.hypot(dr, dg, db)). Because sRGB applies a non-linear gamma curve and human vision weights lightness, chroma, and blue-hue rotation non-uniformly, Euclidean RGB frequently maps coral skin tones onto dull browns and shifts deep navy blues toward violet.
1. Linearize sRGB → CIE XYZ (D65) → CIE L*a*b*
Each 8-bit channel must first be inverse-companded (v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4), transformed into CIE 1931 XYZ under a standard 2° D65 illuminant, and projected into CIE 1976 L*a*b*.
2. Sharma–Wu–Dalal (2005) CIEDE2000
Unlike CIE76, CIEDE2000 applies parametric weighting functions (S_L, S_C, S_H) plus an interaction rotation term R_T around 275° to correct high-chroma blue distortions. We verified our zero-dependency implementation against Table 1 of Sharma, Wu & Dalal (2005) to within 1e-4.
Interactive Browser Tools
- HEX / RGB to DMC Thread Matcher — top-8
CIEDE2000matches across 454 embroidery shades - Photo to Cross-Stitch Pattern PDF Maker & Aida / Linen Fabric & Skein Calculator
- Pre-decode camera reference images locally with HEICExport (HEIC to PNG), AVIFExport (AVIF & JXL to JPG), and ImageCompressorTo20KB.