Skip to content

Instantly share code, notes, and snippets.

@kfitfk
Created March 18, 2016 07:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kfitfk/e8a2a1eeb35e9b0882e4 to your computer and use it in GitHub Desktop.
Save kfitfk/e8a2a1eeb35e9b0882e4 to your computer and use it in GitHub Desktop.
Convert RGB color to CIE LAB color
function rgbToXyz(r, g, b) {
r /= 255
g /= 255
b /= 255
if (r > 0.04045) r = Math.pow(((r + 0.055) / 1.055), 2.4)
else r = r / 12.92
if (g > 0.04045) g = Math.pow(((g + 0.055) / 1.055), 2.4)
else g = g / 12.92
if (b > 0.04045) b = Math.pow(((b + 0.055) / 1.055), 2.4)
else b = b / 12.92
r = r * 100
g = g * 100
b = b * 100
//Observer. = 2°, Illuminant = D65
return {
x: r * 0.4124 + g * 0.3576 + b * 0.1805,
y: r * 0.2126 + g * 0.7152 + b * 0.0722,
z: r * 0.0193 + g * 0.1192 + b * 0.9505
}
}
function xyzToLab(x, y, z) {
var refX = 95.047
var refY = 100.000
var refZ = 108.883
x /= refX
y /= refY
z /= refZ
if (x > 0.008856) x = Math.pow(x, 1/3)
else x = 7.787 * x + 16 / 116
if (y > 0.008856) y = Math.pow(y, 1/3)
else y = 7.787 * y + 16 / 116
if (z > 0.008856) z = Math.pow(z, 1/3)
else z = 7.787 * z + 16 / 116
return {
l: (116 * y) - 16,
a: 500 * (x - y),
b: 200 * (y - z)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment