ColorUtils.cpp
Go to the documentation of this file.00001 #include "ColorUtils.h" 00002 #include <common.h> 00003 #include <math.h> 00004 00005 static void Matrix3x3Multiply(float matrix[9], float a, float b, float c, float& x, float& y, float& z) { 00006 x = matrix[0]*a + matrix[1]*b + matrix[2]*c; 00007 y = matrix[3]*a + matrix[4]*b + matrix[5]*c; 00008 z = matrix[6]*a + matrix[7]*b + matrix[8]*c; 00009 } 00010 00011 void RGBtoHSV(float r, float g, float b, float& h, float& s, float& v) { 00012 float min, max, delta; 00013 00014 min = MIN3(r, g, b); 00015 max = MAX3(r, g, b); 00016 v = max; 00017 00018 delta = max - min; 00019 if (max != 0) { 00020 s = delta / max; 00021 } 00022 else { 00023 s = 0; 00024 h = -1; 00025 return; 00026 } 00027 00028 if (r == max) { 00029 h = (g - b) / delta; 00030 } 00031 else if (g == max) { 00032 h = 2 + (b-r) / delta; 00033 } 00034 else { 00035 h = 4 + (r-g) / delta; 00036 } 00037 00038 h *= 60; 00039 if (h < 0) 00040 h += 360; 00041 } 00042 00043 void HSVtoRGB(float h, float s, float v, float& r, float& g, float& b) { 00044 int i; 00045 float f, p, q, t; 00046 if (s == 0) { 00047 r = g = b = v; 00048 return; 00049 } 00050 00051 h /= 60; 00052 i = (int)floor(h); 00053 f = h - i; 00054 p = v * (1 - s); 00055 q = v * (1 - s*f); 00056 t = v * (1 - s*(1-f)); 00057 00058 switch (i) { 00059 case 0: 00060 r = v; 00061 g = t; 00062 b = p; 00063 break; 00064 case 1: 00065 r = q; 00066 g = v; 00067 b = p; 00068 break; 00069 case 2: 00070 r = p; 00071 g = v; 00072 b = t; 00073 break; 00074 case 3: 00075 r = p; 00076 g = p; 00077 b = v; 00078 case 4: 00079 r = t; 00080 g = p; 00081 b = v; 00082 default: 00083 r = v; 00084 g = p; 00085 b = q; 00086 break; 00087 } 00088 } 00089 00090 void RGBtoYIQ(float r, float g, float b, float& y, float& i, float& q) { 00091 static float matrix[] = { 00092 0.299f, 0.587f, 0.114f, 00093 0.596f, -0.275f, -0.321f, 00094 0.212f, -0.523f, 0.311f 00095 }; 00096 00097 Matrix3x3Multiply(matrix, r, g, b, y, i, q); 00098 } 00099 00100 void YIQtoRGB(float y, float i, float q, float& r, float& g, float& b) { 00101 static float matrix[] = { 00102 1.0f, 0.956f, 0.621f, 00103 1.0f, -0.272f, -0.647f, 00104 1.0f, -1.105f, 1.702f 00105 }; 00106 00107 Matrix3x3Multiply(matrix, y, i, q, r, g, b); 00108 } 00109 00110 void RGBtoXYZ(float r, float g, float b, float& x, float& y, float& z) { 00111 static float matrix[] = { 00112 0.412453f, 0.357580f, 0.180423f, 00113 0.212671f, 0.715160f, 0.072169f, 00114 0.019334f, 0.119193f, 0.950227f 00115 }; 00116 00117 Matrix3x3Multiply(matrix, r, g, b, x, y, z); 00118 } 00119 00120 void XYZtoRGB(float x, float y, float z, float& r, float& g, float& b) { 00121 static float matrix[] = { 00122 3.240479f, -1.537150f, -0.498535f, 00123 -0.969256f, 1.875992f, 0.041556f, 00124 0.055648f, -0.204043f, 1.057311f 00125 }; 00126 00127 Matrix3x3Multiply(matrix, x, y, z, r, g, b); 00128 } 00129
Generated on 28 Feb 2009 for Milton by
1.5.6