Skip to content

Instantly share code, notes, and snippets.

@bayleedev
Created October 15, 2022 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayleedev/a373fde2c0a0857111ce47cd7a8b6cea to your computer and use it in GitHub Desktop.
Save bayleedev/a373fde2c0a0857111ce47cd7a8b6cea to your computer and use it in GitHub Desktop.
Understanding color difference in LAB vs sRGB using "@thi.ng/color"
import { distCIEDE2000 } from "@thi.ng/color";
import distance from 'euclidean-distance';
const difference = (color1, color2) => {
return distCIEDE2000()(
'#' + (color1.red).toString(16) + (color1.green).toString(16) + (color1.blue).toString(16),
'#' + (color2.red).toString(16) + (color2.green).toString(16) + (color2.blue).toString(16),
)
}
console.log('lab difference')
console.log('black to white', distCIEDE2000()(
'#000000',
'#ffffff',
))
console.log('two blue hex', distCIEDE2000()(
'#4287f5',
'#429bf5',
))
console.log('two blue rgb', difference({
red: 66,
green: 135,
blue: 245,
}, {
red: 66,
green: 155,
blue: 245,
}))
console.log('rgb difference')
console.log('black to white', distance([0,0,0], [255,255,255])/4.41672956)
console.log('two blues', distance([66,135,245], [66,155,245])/4.41672956)
console.log('rgba difference')
console.log('black to white', distance([0,0,0,255], [255,255,255,255])/4.41672956)
console.log('two blues', distance([66,135,245,255], [66,155,245,125])/4.41672956)
▶ node test.js
lab difference
black to white 99.99999999999999
two blue hex 6.9116634623669375
two blue rgb 6.9116634623669375
rgb difference
black to white 99.99999998416558
two blues 4.52823740469181
rgba difference
black to white 99.99999998416558
two blues 29.779832021152558
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment