Converting arbitrary RGB pixel art or photographs into stitchable embroidery charts is much harder than nearest-neighbor RGB matching. Standard Euclidean distance in sRGB space over-weights luminance errors and produces muddy skin tones or shifted blues when restricted to a fixed 447-color palette like DMC stranded cotton.
To get perceptually accurate thread matches entirely in the browser, you need a three-stage color pipeline:
1. Linearize sRGB and Convert to CIE L*a*b*
First, undo the gamma curve on each 8-bit channel before transforming through XYZ (D65 illuminant) into perceptual CIE L*a*b* :
const toLinear = ( c ) => { const s = c / 255 ; return s <= 0.04045 ? s / 12.92 : Math . pow ( ( s + 0.055 ) / 1.055 , 2.4 ) ; } ;
Enter fullscreen mode
Exit fullscreen mode
2. Score Against Precomputed DMC Lab Vectors Using CIEDE2000
Precomputing L*a*b* values for all 447 DMC thread codes at startup keeps per-pixel lookup fast.…