gka / chroma.js

JavaScript library for all kinds of color manipulations
https://gka.github.io/chroma.js/
Other
10.08k stars 543 forks source link

Hybrid Manhattan-Euclidean color difference #266

Open Artoria2e5 opened 3 years ago

Artoria2e5 commented 3 years ago

https://doi.org/10.1002/col.22451 describes a color-difference method that might work better than CIE 76 for large distances while still being quite easy to implement. It applies to all Euclidean color spaces with a lightness and two chroma systems, although it's originally defined for LAB only. An implementation would be:

// This generic implementation assumes that element 0 is lightness and the other 2 are chroma.
function dHyAB(color1, color2, mode='lab') {
  [lab1, lab2] = [color1, color2].map(e => e[mode]())
  [dl, da, db] = lab1.map((e, i) => e - lab2[i])
  return Math.sqrt(da ** 2 + db ** 2) + Math.abs(dl)
}