So it turns out that comparing the difference between RGB colors is actually a really shitty way to determine color distance. There are colors which have similar RGB representations while being perceptually distant, and perceptually similar colors with vastly different representations.
So I decided to run a series of large scale double-blind (heh) experiments on human perception, analyzing the output with a dizzying array of statistical techniques and-- oh wait, no I didn't, because scientists have gotten that all figured out since the late '70s.
npm i rgb-lab
const { rgb2lab, lab2rgb, deltaE } = require('rgb-lab')
The first function is rgb2lab, it does kind of exactly what you'd expect, making the usual sRGB assumptions.
The second one is lab2rgb, and it's kind of the inverse of lab2rgb. That is, in an ideal world, you could expect that lab2rgb(rgb2lab([a, b, c]))
would return [a, b, c]
, which probably won't be the case due to quirks of floating point arithmetic.
But if you feed functions values which lie squarely within the acceptable range (R, G, B between 0 and 255), things should be okay.
This function calculates DeltaE, the perceptual color distance between any two random colors in the Lab* space.
Return value is float.