Useful color equations: RGB to LAB converter
2013-05-12 01:12
302 查看
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);
}
相关文章推荐
- RGB to LAB converter
- Convert RGB to L*a*b* (Lab Color space) in OpenCV
- Convert jQuery RGB output to Hex Color
- [UIDeviceRGBColor length]: unrecognized selector sent to instance
- YUVtoRGB RGB to YUV color convert formulas - ITU 601
- [UIDeviceRGBColor copyWithZone:]: unrecognized selector sent to instance
- VGA to RGB + composite sync -converter
- convert RGB colorspace to Homochromy(Black/whit)
- [UIDeviceRGBColor colorRGBA]: unrecognized selector sent to instance 0xa0791b0 的解决方案
- AE开发图层符号选择器的设计 ConvertIRgbColorToColor
- [UIDeviceRGBColor copyWithZone:]: unrecognized selector sent to instance
- -[UICachedDeviceRGBColor CGImage]: unrecognized selector sent to instance 0x130972b90
- SyntaxWarning: name 'x' is assigned to before global declaration global color Python
- RGB转为Lab空间
- rgb to yuv 互相转化
- opencv2学习 ,读取图片,rgb2 lab
- (转) Write Silverlight Application To Consume Currency Converter WCF Service
- 自定义View之RGB颜色变化Paint画笔颜色过滤器setColorFilter,LightingColorFilter光照过滤器
- system.drawing.color 字符串转换 blend wpf 16进制转rgb argb color
- Two useful transaction codes to search the T-TCODE where is in SAP menu tree