8 #define MIN3(a,b,c) ( (a)<=(b) ? (a)<=(c)?(a):(c) : (b)<=(c)?(b):(c) )
9 #define MAX3(a,b,c) ( (a)>=(b) ? (a)>=(c)?(a):(c) : (b)>=(c)?(b):(c) )
11 inline double fMax(double a
, double b
)
16 inline double fMin(double a
, double b
)
20 /******************************************************************************
22 PURPOSE: Convert from RGB to HLS
23 IN: RGB color (0xBBGGRR)
24 OUT: Hue, Saturation, Luminance from 0 to 1
25 COPYRIGHT:1995-1997 Robert Mashlan
26 Modified for LabWindows/CVI, 1999 Guillaume Dargaud
27 ******************************************************************************/
28 void RGBtoHLS(const COLORREF rgb
, double *H
, double *L
, double *S
)
31 double r
= (double)((rgb
)&0xFF)/255;
32 double g
= (double)((rgb
>> 8)&0xFF)/255;
33 double b
= (double)((rgb
>>16)&0xFF)/255;
34 double cmax
= MAX3(r
,g
,b
);
35 double cmin
= MIN3(r
,g
,b
);
40 *S
= *H
= 0; // it's really undefined
44 if(*L
< 0.5) *S
= (cmax
-cmin
)/(cmax
+cmin
);
45 else *S
= (cmax
-cmin
)/(2.0-cmax
-cmin
);
55 if(g
== cmax
) *H
= 2.0 + (b
-r
) / delta
;
56 else *H
= 4.0 + (r
-g
) / delta
;
59 if (*H
< 0.0) *H
+= 1;
63 /******************************************************************************
65 PURPOSE: Convert a hue (color) to RGB
66 COPYRIGHT:1995-1997 Robert Mashlan
67 Modified for LabWindows/CVI, 1999 Guillaume Dargaud
68 ******************************************************************************/
69 double HueToRGB(const double m1
, const double m2
, double h
)
73 if (6.0*h
< 1 ) return (m1
+(m2
-m1
)*h
*6.0);
74 if (2.0*h
< 1 ) return m2
;
75 if (3.0*h
< 2.0) return (m1
+(m2
-m1
)*((2.0/3.0)-h
)*6.0);
80 /******************************************************************************
82 PURPOSE: Convert from HSL to RGB
83 IN: Hue, Saturation, Luminance from 0 to 1
84 RETURN: RGB color (0xBBGGRR)
85 COPYRIGHT:1995-1997 Robert Mashlan
86 Modified for LabWindows/CVI, 1999 Guillaume Dargaud
87 ******************************************************************************/
89 COLORREF
HLStoRGB(const double H
, const double L
, const double S
)
107 r
= HueToRGB(m1
,m2
,H
+1.0/3.0);
108 g
= HueToRGB(m1
,m2
,H
);
109 b
= HueToRGB(m1
,m2
,H
-1.0/3.0);
112 return RGB(r
*255, g
*255, b
*255);
117 /******************************************************************************
118 FUNCTION: ColorScaleHSL
119 PURPOSE: Returns the HSL linear interpolated color between 2 colors
120 (more natural looking than RGB interpolation)
121 For instance if the luminance is the same in Col1 and Col2,
122 then the luminance of the result will be the same
123 If Ratio=0, you get Col1,
124 If Ratio=1, you get Col2
125 IN: Col1: low color in hex 0xBBGGRR format
126 Col2: high color in hex 0xBBGGRR format
127 Ratio: 0 for low color, 1 for high color, or in between
128 EXAMPLE: Col1=0, Col2=0xFF00FF, Ratio=0.5 returns 0x1F5F3F
129 ******************************************************************************/
130 COLORREF
ColorScaleHSL( const COLORREF Col1
, const COLORREF Col2
, const double Ratio
)
132 static double H1
, H2
, S1
, S2
, L1
, L2
;
134 if (Ratio
<=0) return Col1
; // Ratio parameter must be between 0 and 1
135 else if (Ratio
>=1) return Col2
;
137 RGBtoHLS( Col1
, &H1
, &L1
, &S1
);
138 RGBtoHLS( Col2
, &H2
, &L2
, &S2
);
139 return HLStoRGB( H1
+(H2
-H1
)*Ratio
, L1
+(L2
-L1
)*Ratio
, S1
+(S2
-S1
)*Ratio
);
143 /******************************************************************************
144 FUNCTION: ColorScaleRGB
145 PURPOSE: Returns the RGB linear interpolated color between 2 colors
146 If Ratio=0, you get Col1,
147 If Ratio=1, you get Col2
148 IN: Col1: low color in hex 0xBBGGRR format
149 Col2: high color in hex 0xBBGGRR format
150 Ratio: 0 for low color, 1 for high color, or in between
151 EXAMPLE: Col1=0, Col2=0xFF00FF, Ratio=0.5 returns 0x800080
152 ******************************************************************************/
153 COLORREF
ColorScaleRGB( const COLORREF Col1
,
155 const double Ratio
) {
156 int R1
=(Col1
)&0xFF, G1
=(Col1
>>8)&0xFF, B1
=(Col1
>>16)&0xFF;
157 int R2
=(Col2
)&0xFF, G2
=(Col2
>>8)&0xFF, B2
=(Col2
>>16)&0xFF;
159 if (Ratio
<=0) return Col1
; // Ratio parameter must be between 0 and 1
160 else if (Ratio
>=1) return Col2
;
163 (R1 + (R2 - R1) * (Ratio + 0.02) + 0.5), // rounding
164 (G1 + (G2 - G1) * (Ratio - 0.00) + 0.5),
165 (B1 + (B2 - B1) * (Ratio + 0.05) + 0.5)
173 R1 = (int)(double(R1) * r + 0.5);
174 G1 = (int)(double(G1) * r + 0.5);
175 B1 = (int)(double(B1) * r + 0.5);
176 return RGB(R1,G1,B1);*/
179 (R1
+ (R2
- R1
) * (Ratio
+ 0.02) + 0.5), // rounding
180 (G1
+ (G2
- G1
) * (Ratio
- 0.00) + 0.5),
181 (B1
+ (B2
- B1
) * (Ratio
+ 0.05) + 0.5)
187 COLORREF
ColorDarker(COLORREF col
, double ratio
)
189 return ColorScaleHSL(col
, RGB(0,0,0), ratio
);
192 COLORREF
ColorLighter(COLORREF col
, double ratio
)
194 return ColorScaleHSL(col
, RGB(255,255,255), ratio
);
202 typedef enum { Red
, Green
, Blue
};
204 void RGBtoHLS(COLORREF rgb
, double *Hue
, double *Lum
, double *Sat
)
209 BYTE red
, green
, blue
;
211 red
= GetRValue(rgb
);
212 green
= GetGValue(rgb
);
213 blue
= GetBValue(rgb
);
217 mn
= red
; mx
= green
; major
= Green
;
221 mn
= green
; mx
= red
; major
= Red
;
230 mx
= blue
; major
= Blue
;
241 *Lum
= (mx
+ mn
) / 510;
244 *Sat
= (mx
-mn
) / (mn
+mx
);
246 *Sat
= (mx
-mn
) / (510-mn
-mx
);
250 case Red
: *Hue
= (green
-blue
) * 60.0 / (mx
-mn
) + 360.0;
253 case Green
: *Hue
= (blue
-red
) * 60.0 / (mx
-mn
) + 120.0;
256 case Blue
: *Hue
= (red
-green
) * 60.0 / (mx
-mn
) + 240.0;
266 static BYTE
Value(double m1
, double m2
, double hue
)
269 if(hue
> 360) hue
-= 360;
270 else if(hue
< 0) hue
+= 360;
273 m1
= m1
+ (m2
- m1
) * hue
/ 60;
277 m1
= m1
+ (m2
- m1
) * (240 - hue
) / 60;
279 return (BYTE
)(m1
* 255);
282 COLORREF
HLStoRGB(const double Hue
, const double Lum
, const double Sat
)
284 BYTE red
, green
, blue
;
288 red
= green
= blue
= (BYTE
)(Lum
* 255);
295 m2
= Lum
+ Lum
* Sat
;
297 m2
= Lum
+ Sat
- Lum
* Sat
;
301 red
= Value(m1
, m2
, Hue
+ 120);
302 green
= Value(m1
, m2
, Hue
);
303 blue
= Value(m1
, m2
, Hue
- 120);
306 return RGB(red
, green
, blue
);
309 COLORREF
ScaleLumRGB(COLORREF col1
, double ratio
)
313 RGBtoHLS(col1
, &H1
, &L1
, &S1
);
317 return HLStoRGB(H1
, L1
, S1
);
320 COLORREF
ColorScaleHSL(const COLORREF Col1
, const COLORREF Col2
, const double Ratio
)
322 static double H1
, H2
, S1
, S2
, L1
, L2
;
324 if(Ratio
<= 0) return Col1
; // Ratio parameter must be between 0 and 1
325 else if(Ratio
>= 1) return Col2
;
327 RGBtoHLS( Col1
, &H1
, &L1
, &S1
);
328 RGBtoHLS( Col2
, &H2
, &L2
, &S2
);
330 return HLStoRGB( H1
/*+ (H2 - H1 ) * Ratio*/, L1
+ (L2
- L1
) * Ratio
, S1
+ (S2
- S1
) * Ratio
* 2);
333 COLORREF
ColorScaleRGB(const COLORREF Col1
, const COLORREF Col2
, const double Ratio
)
335 return ColorScaleHSL(Col1
, Col2
, Ratio
);