Useful color equations: RGB to LAB converter

Problem

I am working a bit with images and I need to perform some calculations in Lab color space. For this reason I’ve created functions to convert RGB to Lab color space and Lab to RGB

Solution

There also functions to convert RGB to XYZ color space.

Detailed explanation

 public static function rgb2xyz(R:uint,G:uint,B:uint):Object

{
 //R from 0 to 255
 //G from 0 to 255
 //B from 0 to 255
 var r:Number = R/255;
 var g:Number = G/255;
 var b:Number = 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
var xyz:Object = {x:0, y:0, z:0};
xyz.x = r * 0.4124 + g * 0.3576 + b * 0.1805;
xyz.y = r * 0.2126 + g * 0.7152 + b * 0.0722;
xyz.z = r * 0.0193 + g * 0.1192 + b * 0.9505;
 
return xyz;
}
public static function xyz2lab(X:Number, Y:Number, Z:Number ):Object
{
const REF_X:Number = 95.047; // Observer= 2°, Illuminant= D65
const REF_Y:Number = 100.000; 
const REF_Z:Number = 108.883; 
var x:Number = X / REF_X;   
var y:Number = Y / REF_Y;  
var z:Number = Z / REF_Z;  
 
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 lab:Object = {l:0, a:0, b:0};
lab.l = ( 116 * y ) - 16;
lab.a = 500 * ( x - y );
lab.b = 200 * ( y - z );
 
return lab;
}
public static function lab2xyz( l:Number, a:Number, b:Number ):Object
{
const REF_X:Number = 95.047; // Observer= 2°, Illuminant= D65
const REF_Y:Number = 100.000; 
const REF_Z:Number = 108.883; 
var y:Number = (l + 16) / 116;
var x:Number = a / 500 + y;
var z:Number = y - b / 200;
 
if ( Math.pow( y , 3 ) > 0.008856 ) { y = Math.pow( y , 3 ); }
else { y = ( y - 16 / 116 ) / 7.787; }
if ( Math.pow( x , 3 ) > 0.008856 ) { x = Math.pow( x , 3 ); }
else { x = ( x - 16 / 116 ) / 7.787; }
if ( Math.pow( z , 3 ) > 0.008856 ) { z = Math.pow( z , 3 ); }
else { z = ( z - 16 / 116 ) / 7.787; }
 
var xyz:Object = {x:0, y:0, z:0};
xyz.x = REF_X * x;     
xyz.y = REF_Y * y; 
xyz.z = REF_Z * z; 
 
return xyz;
}
public static function xyz2rgb(X:Number, Y:Number, Z:Number):Object
{
//X from 0 to  95.047      (Observer = 2°, Illuminant = D65)
//Y from 0 to 100.000
//Z from 0 to 108.883
var x:Number = X / 100;        
var y:Number = Y / 100;        
var z:Number = Z / 100;        
 
var r:Number = x * 3.2406 + y * -1.5372 + z * -0.4986;
var g:Number = x * -0.9689 + y * 1.8758 + z * 0.0415;
var b:Number = x * 0.0557 + y * -0.2040 + z * 1.0570;
 
if ( r > 0.0031308 ) { r = 1.055 * Math.pow( r , ( 1 / 2.4 ) ) - 0.055; }
else { r = 12.92 * r; }
if ( g > 0.0031308 ) { g = 1.055 * Math.pow( g , ( 1 / 2.4 ) ) - 0.055; }
else { g = 12.92 * g; }
if ( b > 0.0031308 ) { b = 1.055 * Math.pow( b , ( 1 / 2.4 ) ) - 0.055; }
else { b = 12.92 * b; }
var rgb:Object = {r:0, g:0, b:0}
rgb.r = Math.round( r * 255 );
rgb.g = Math.round( g * 255 );
rgb.b = Math.round( b * 255 );
return rgb;
}
public static function rgb2lab(R:uint, G:uint, B:uint):Object
{
var xyz:Object = ColorUtils.rgb2xyz(R, G, B);
return ColorUtils.xyz2lab(xyz.x, xyz.y, xyz.z);
}

你可能感兴趣的:(Useful color equations: RGB to LAB converter)