Skip to content

Instantly share code, notes, and snippets.

@Dammmien
Created February 5, 2016 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Dammmien/693a732222e8d2322294 to your computer and use it in GitHub Desktop.
Save Dammmien/693a732222e8d2322294 to your computer and use it in GitHub Desktop.
RGB to LAB
function rgbToLab( R, G, B ) {
R = ( R / 255 );
G = ( G / 255 );
B = ( 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;
var X = R * 0.4124 + G * 0.3576 + B * 0.1805;
var Y = R * 0.2126 + G * 0.7152 + B * 0.0722;
var Z = R * 0.0193 + G * 0.1192 + B * 0.9505;
X = X / 95.047;
Y = Y / 100.000;
Z = Z / 108.883;
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 );
var L = ( 116 * Y ) - 16;
var a = 500 * ( X - Y );
var b = 200 * ( Y - Z );
return [ L, a, b ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment