Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygencardcolor.cppGo to the documentation of this file.00001 // 00002 // Colour support 00003 // 00004 #include <windows.h> 00005 00006 #define MakeRGB RGB 00007 00008 #define MIN3(a,b,c) ( (a)<=(b) ? (a)<=(c)?(a):(c) : (b)<=(c)?(b):(c) ) 00009 #define MAX3(a,b,c) ( (a)>=(b) ? (a)>=(c)?(a):(c) : (b)>=(c)?(b):(c) ) 00010 00011 inline double fMax(double a, double b) 00012 { 00013 return a < b ? b : a; 00014 } 00015 00016 inline double fMin(double a, double b) 00017 { 00018 return a < b ? a : b; 00019 } 00020 /****************************************************************************** 00021 FUNCTION: RGBtoHLS 00022 PURPOSE: Convert from RGB to HLS 00023 IN: RGB color (0xBBGGRR) 00024 OUT: Hue, Saturation, Luminance from 0 to 1 00025 COPYRIGHT:1995-1997 Robert Mashlan 00026 Modified for LabWindows/CVI, 1999 Guillaume Dargaud 00027 ******************************************************************************/ 00028 void RGBtoHLS(const COLORREF rgb, double *H, double *L, double *S ) 00029 { 00030 double delta; 00031 double r = (double)((rgb )&0xFF)/255; 00032 double g = (double)((rgb>> 8)&0xFF)/255; 00033 double b = (double)((rgb>>16)&0xFF)/255; 00034 double cmax = MAX3(r,g,b); 00035 double cmin = MIN3(r,g,b); 00036 *L=(cmax+cmin)/2.0; 00037 00038 if(cmax == cmin) 00039 { 00040 *S = *H = 0; // it's really undefined 00041 } 00042 else 00043 { 00044 if(*L < 0.5) *S = (cmax-cmin)/(cmax+cmin); 00045 else *S = (cmax-cmin)/(2.0-cmax-cmin); 00046 00047 delta = cmax - cmin; 00048 00049 if(r == cmax) 00050 { 00051 *H = (g - b) / delta; 00052 } 00053 else 00054 { 00055 if(g == cmax) *H = 2.0 + (b-r) / delta; 00056 else *H = 4.0 + (r-g) / delta; 00057 } 00058 *H /= 6.0; 00059 if (*H < 0.0) *H += 1; 00060 } 00061 } 00062 00063 /****************************************************************************** 00064 FUNCTION: HueToRGB 00065 PURPOSE: Convert a hue (color) to RGB 00066 COPYRIGHT:1995-1997 Robert Mashlan 00067 Modified for LabWindows/CVI, 1999 Guillaume Dargaud 00068 ******************************************************************************/ 00069 double HueToRGB(const double m1, const double m2, double h ) 00070 { 00071 if (h<0) h+=1.0; 00072 if (h>1) h-=1.0; 00073 if (6.0*h < 1 ) return (m1+(m2-m1)*h*6.0); 00074 if (2.0*h < 1 ) return m2; 00075 if (3.0*h < 2.0) return (m1+(m2-m1)*((2.0/3.0)-h)*6.0); 00076 return m1; 00077 } 00078 00079 00080 /****************************************************************************** 00081 FUNCTION: HLStoRGB 00082 PURPOSE: Convert from HSL to RGB 00083 IN: Hue, Saturation, Luminance from 0 to 1 00084 RETURN: RGB color (0xBBGGRR) 00085 COPYRIGHT:1995-1997 Robert Mashlan 00086 Modified for LabWindows/CVI, 1999 Guillaume Dargaud 00087 ******************************************************************************/ 00088 00089 COLORREF HLStoRGB(const double H, const double L, const double S ) 00090 { 00091 double r,g,b; 00092 double m1, m2; 00093 00094 if(S == 0) 00095 { 00096 r = g = b = L; 00097 } 00098 else 00099 { 00100 if (L <= 0.5) 00101 m2 = L * (1.0 + S); 00102 else 00103 m2 = L + S - L * S; 00104 00105 m1 = 2.0 * L - m2; 00106 00107 r = HueToRGB(m1,m2,H+1.0/3.0); 00108 g = HueToRGB(m1,m2,H); 00109 b = HueToRGB(m1,m2,H-1.0/3.0); 00110 } 00111 00112 return RGB(r*255, g*255, b*255); 00113 } 00114 00115 00116 00117 /****************************************************************************** 00118 FUNCTION: ColorScaleHSL 00119 PURPOSE: Returns the HSL linear interpolated color between 2 colors 00120 (more natural looking than RGB interpolation) 00121 For instance if the luminance is the same in Col1 and Col2, 00122 then the luminance of the result will be the same 00123 If Ratio=0, you get Col1, 00124 If Ratio=1, you get Col2 00125 IN: Col1: low color in hex 0xBBGGRR format 00126 Col2: high color in hex 0xBBGGRR format 00127 Ratio: 0 for low color, 1 for high color, or in between 00128 EXAMPLE: Col1=0, Col2=0xFF00FF, Ratio=0.5 returns 0x1F5F3F 00129 ******************************************************************************/ 00130 COLORREF ColorScaleHSL( const COLORREF Col1, const COLORREF Col2, const double Ratio) 00131 { 00132 static double H1, H2, S1, S2, L1, L2; 00133 00134 if (Ratio<=0) return Col1; // Ratio parameter must be between 0 and 1 00135 else if (Ratio>=1) return Col2; 00136 00137 RGBtoHLS( Col1, &H1, &L1, &S1); 00138 RGBtoHLS( Col2, &H2, &L2, &S2); 00139 return HLStoRGB( H1+(H2-H1)*Ratio, L1+(L2-L1)*Ratio, S1+(S2-S1)*Ratio ); 00140 } 00141 00142 00143 /****************************************************************************** 00144 FUNCTION: ColorScaleRGB 00145 PURPOSE: Returns the RGB linear interpolated color between 2 colors 00146 If Ratio=0, you get Col1, 00147 If Ratio=1, you get Col2 00148 IN: Col1: low color in hex 0xBBGGRR format 00149 Col2: high color in hex 0xBBGGRR format 00150 Ratio: 0 for low color, 1 for high color, or in between 00151 EXAMPLE: Col1=0, Col2=0xFF00FF, Ratio=0.5 returns 0x800080 00152 ******************************************************************************/ 00153 COLORREF ColorScaleRGB( const COLORREF Col1, 00154 const COLORREF Col2, 00155 const double Ratio) { 00156 int R1=(Col1)&0xFF, G1=(Col1>>8)&0xFF, B1=(Col1>>16)&0xFF; 00157 int R2=(Col2)&0xFF, G2=(Col2>>8)&0xFF, B2=(Col2>>16)&0xFF; 00158 00159 if (Ratio<=0) return Col1; // Ratio parameter must be between 0 and 1 00160 else if (Ratio>=1) return Col2; 00161 00162 /* return RGB( 00163 (R1 + (R2 - R1) * (Ratio + 0.02) + 0.5), // rounding 00164 (G1 + (G2 - G1) * (Ratio - 0.00) + 0.5), 00165 (B1 + (B2 - B1) * (Ratio + 0.05) + 0.5) 00166 );*/ 00167 00168 /*double r = Ratio; 00169 if(Col2 == 0) 00170 r = 1-Ratio; 00171 else 00172 r = 1+Ratio; 00173 R1 = (int)(double(R1) * r + 0.5); 00174 G1 = (int)(double(G1) * r + 0.5); 00175 B1 = (int)(double(B1) * r + 0.5); 00176 return RGB(R1,G1,B1);*/ 00177 00178 return RGB( 00179 (R1 + (R2 - R1) * (Ratio + 0.02) + 0.5), // rounding 00180 (G1 + (G2 - G1) * (Ratio - 0.00) + 0.5), 00181 (B1 + (B2 - B1) * (Ratio + 0.05) + 0.5) 00182 ); 00183 } 00184 00185 00186 00187 COLORREF ColorDarker(COLORREF col, double ratio) 00188 { 00189 return ColorScaleHSL(col, RGB(0,0,0), ratio); 00190 } 00191 00192 COLORREF ColorLighter(COLORREF col, double ratio) 00193 { 00194 return ColorScaleHSL(col, RGB(255,255,255), ratio); 00195 } 00196 00197 // 00198 // Experimental!!! 00199 // 00200 #if 0 00201 00202 typedef enum { Red, Green, Blue }; 00203 00204 void RGBtoHLS(COLORREF rgb, double *Hue, double *Lum, double *Sat) 00205 { 00206 double mn, mx; 00207 int major; 00208 00209 BYTE red, green, blue; 00210 00211 red = GetRValue(rgb); 00212 green = GetGValue(rgb); 00213 blue = GetBValue(rgb); 00214 00215 if(red < green) 00216 { 00217 mn = red; mx = green; major = Green; 00218 } 00219 else 00220 { 00221 mn = green; mx = red; major = Red; 00222 } 00223 00224 if(blue < mn) 00225 { 00226 mn = blue; 00227 } 00228 else if(blue > mx) 00229 { 00230 mx = blue; major = Blue; 00231 } 00232 00233 if(mn == mx) 00234 { 00235 *Lum = mn / 255; 00236 *Sat = 0; 00237 *Hue = 0; 00238 } 00239 else 00240 { 00241 *Lum = (mx + mn) / 510; 00242 00243 if(*Lum <= 0.5) 00244 *Sat = (mx-mn) / (mn+mx); 00245 else 00246 *Sat = (mx-mn) / (510-mn-mx); 00247 00248 switch(major) 00249 { 00250 case Red: *Hue = (green-blue) * 60.0 / (mx-mn) + 360.0; 00251 break; 00252 00253 case Green: *Hue = (blue-red) * 60.0 / (mx-mn) + 120.0; 00254 break; 00255 00256 case Blue: *Hue = (red-green) * 60.0 / (mx-mn) + 240.0; 00257 break; 00258 00259 } 00260 00261 if(*Hue > 360.0) 00262 *Hue -= 360.0; 00263 } 00264 } 00265 00266 static BYTE Value(double m1, double m2, double hue) 00267 { 00268 00269 if(hue > 360) hue -= 360; 00270 else if(hue < 0) hue += 360; 00271 00272 if(hue < 60) 00273 m1 = m1 + (m2 - m1) * hue / 60; 00274 else if(hue < 180) 00275 m1 = m2; 00276 else if(hue < 240) 00277 m1 = m1 + (m2 - m1) * (240 - hue) / 60; 00278 00279 return (BYTE)(m1 * 255); 00280 } 00281 00282 COLORREF HLStoRGB(const double Hue, const double Lum, const double Sat) 00283 { 00284 BYTE red, green, blue; 00285 00286 if(Sat == 0) 00287 { 00288 red = green = blue = (BYTE)(Lum * 255); 00289 } 00290 else 00291 { 00292 double m1, m2; 00293 00294 if(Lum <= 0.5) 00295 m2 = Lum + Lum * Sat; 00296 else 00297 m2 = Lum + Sat - Lum * Sat; 00298 00299 m1 = 2 * Lum - m2; 00300 00301 red = Value(m1, m2, Hue + 120); 00302 green = Value(m1, m2, Hue); 00303 blue = Value(m1, m2, Hue - 120); 00304 } 00305 00306 return RGB(red, green, blue); 00307 } 00308 00309 COLORREF ScaleLumRGB(COLORREF col1, double ratio) 00310 { 00311 double H1, L1, S1; 00312 00313 RGBtoHLS(col1, &H1, &L1, &S1); 00314 00315 L1 += L1 * ratio; 00316 00317 return HLStoRGB(H1, L1, S1); 00318 } 00319 00320 COLORREF ColorScaleHSL(const COLORREF Col1, const COLORREF Col2, const double Ratio) 00321 { 00322 static double H1, H2, S1, S2, L1, L2; 00323 00324 if(Ratio <= 0) return Col1; // Ratio parameter must be between 0 and 1 00325 else if(Ratio >= 1) return Col2; 00326 00327 RGBtoHLS( Col1, &H1, &L1, &S1); 00328 RGBtoHLS( Col2, &H2, &L2, &S2); 00329 00330 return HLStoRGB( H1 /*+ (H2 - H1 ) * Ratio*/, L1 + (L2 - L1) * Ratio, S1 + (S2 - S1) * Ratio * 2); 00331 } 00332 00333 COLORREF ColorScaleRGB(const COLORREF Col1, const COLORREF Col2, const double Ratio) 00334 { 00335 return ColorScaleHSL(Col1, Col2, Ratio); 00336 } 00337 #endif Generated on Thu Mar 18 06:50:43 2010 for ReactOS by
1.6.2
|