Color Conversion Library in ANSI C
The idea of this lib is to create a centralized color conversion lib, where most of the popular systems can communicate with each other.
The following conversions are offered:
XYZ to RGB and RGB to XYZ
XYZ to Yxy and Yxy to XYZ
XYZ to Hunter-Lab and Hunter-Lab to XYZ
XYZ to CIE-L*ab and CIE-L*ab to XYZ
CIE-L*ab to CIE-L*CHo and CIE-LCH to CIE_L*ab
XYZ to CIE-L*uv and bCIE-L*uv to XYZ
RGB to HSL and HSL to RGB
RGB to HSV and HSV to RGB
RGB to CMY and CMY to RGB
This lib is a project for PUC-RIO, by Bruno Glaser (glaser at k2sistemas.com.br) supervised by Marcelo Gattass(mgattass at tecgraf.puc-rio.br). It is based mainly on easyrgb’s conversion (http://www.easyrgb.com/math.php).
General notes:
Basics about each encoding sistem can be found here or through the bibliography
Every value is represented as a double variable with the apropriate range, for instance RGB is from 0 to 1.
There are macros defined for Tristimulus values shown here.
The complete implementation is found here (zip file).
Bibliography here
const double _ccTristimulusValues[ccNTristimulus][3] =
{
/* 2 degrees */
/*ccTA_2*/{109.850, 100.000, 35.585},
/*ccTC_2*/{98.074, 100.000, 118.232},
/* D50 */ {96.422, 100.000, 82.521},
/* D55 */ {95.682, 100.000, 92.149},
/* D65 */ {95.047, 100.000, 108.883},
/* D75 */ {94.972, 100.000, 122.638},
/* F2 */ {99.187, 100.000, 67.395},
/* F7 */ {95.044, 100.000, 108.755},
/* F11 */ {100.966, 100.000, 64.370},
/* 10 degrees */
/* A */ {111.144, 100.000, 35.200},
/* C */ {97.285, 100.000, 116.145},
/* D50 */ {96.720, 100.000, 81.427},
/* D55 */ {95.799, 100.000, 90.926},
/* D65 */ {94.811, 100.000, 107.304},
/* D75 */ {94.416, 100.000, 120.641},
/* F2 */ {103.280, 100.000, 69.026},
/* F7 */ {95.792, 100.000, 107.687},
/* F11 */ {103.866, 100.000, 65.627}
};
enum {
ccTA_2, ccTC_2, D50_2, D55_2, D65_2, D75_2, F2_2, F7_2, F11_2,
ccTA_10, ccTC_10, D50_10, D55_10, D65_10, D75_10, F2_10, F7_10, F11_10,
ccNTristimulus
};
RGB
The most famous color encoding is an aditive system with 3 components, Red, Green and Blue. Computer monitors emit color as RGB.
CMY
The complement of RGB, it’s a subtractive systems with 3 components: Cyan, Magenta and Yellow. The conversion from RGB is a simple subtraction, since they complement each other. When painting on a paper you are subtracting colors, meaning they will be reflected.
CMYK
Same as CMY with the added “K” parameter which is black. It is the system used by printers.
HSV
A system which represents color as Hue, Saturation and Value. Hue is what is seen as the color. Saturation is how “pure” the color is. 0% is grey and 100% is a pure color. Value represents the brightness. The conversion from RGB is simple. V is the biggest value of R,G and B ( max(r,g,b)). V = 100% is white and 0% is black. If V is different then 0, we have a chromatic color. Saturation is defined as a ratio of V and a delta of the minimum and maximum values. The Hue depends on V, and is obtained with simple math for each case.
HSL
Just like HSV but instead of Value we have lightness. This time, maximum saturation is at the mid-point (50%). 0% is black and 100% is white.
Both HSV and HSL are the best color systems for color adjustment and balance since the parameters are more clearly separated.
XYZ
The XYZ color system, also called “norm color system”. It is superset of the RGB color system. It uses tristimulus values when encoding. X, Y and Z are all calculated through color-matching functions and are always positive. It was a system created by CIE. The conversion is obtained through a matrix operation after a simple adjustment of R, G and B:
[r’ g’ b’] = [X Y Z] [M]-1
Yxy
Known as the Trichromatic coordinates. Y represents the brightness and (x,y) hue and saturation. x is X/(X+Y+Z) and y is Y/(X+Y+Z), both from the XYZ color encoding.
CIE Lab (1976)
This color scale is based on the opponent color theory. “L” represents Light-Dark, “a” represents red-green and “b” represents yellow-blue. L goes from 0 to 100, a is in and interval [-a,a] and b is in [-b,b]. It’s space is a cube. The values are obtained through cube roots of XYZ values.
CIE Lch
This is a polar coordinate color system. L is a z axis of light-dark, c is chroma, the distance from the axis, and h is the hue, the angle on the plane. It is calculated from CIE’s Lab. Basically C is
(a2 + b2)1/2
And H is arctan(b/a).
CIE Luv
The CIE Luv color space is designed to be perceptually uniform, meaning that a given change in value corresponds roughly to the same perceptual difference over any part of the space. It was designed for emissive colors, and is obtained from XYZ through simple multiplication of parameters.
Hunter Lab (1966)
The same theory of CIE’s Lab, but is calculated from square roots of XYZ. The main difference between the two is that besides the fact that none are ideally uniform, Hunter’s Lab contracts the yellow region of the cube and over-expands the blue region.
Foley, “Computer Graphics Principles and Practice.” Addison Wesley
http://www.normankoren.com/light_color.html
http://www.easyrgb.com/math.php
http://www.profc.udec.cl/~gabriel/tutoriales/rsnote/cp10/cp10-5.htm
License:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.