eb76c42a3bb48d655de5d11d4517233ca1cf7d68
6 #define WIN32_NO_STATUS
12 #define MIN3(a,b,c) ( (a)<=(b) ? (a)<=(c)?(a):(c) : (b)<=(c)?(b):(c) )
13 #define MAX3(a,b,c) ( (a)>=(b) ? (a)>=(c)?(a):(c) : (b)>=(c)?(b):(c) )
15 inline double fMax(double a
, double b
)
20 inline double fMin(double a
, double b
)
24 /******************************************************************************
26 PURPOSE: Convert from RGB to HLS
27 IN: RGB color (0xBBGGRR)
28 OUT: Hue, Saturation, Luminance from 0 to 1
29 COPYRIGHT:1995-1997 Robert Mashlan
30 Modified for LabWindows/CVI, 1999 Guillaume Dargaud
31 ******************************************************************************/
32 void RGBtoHLS(const COLORREF rgb
, double *H
, double *L
, double *S
)
35 double r
= (double)((rgb
)&0xFF)/255;
36 double g
= (double)((rgb
>> 8)&0xFF)/255;
37 double b
= (double)((rgb
>>16)&0xFF)/255;
38 double cmax
= MAX3(r
,g
,b
);
39 double cmin
= MIN3(r
,g
,b
);
44 *S
= *H
= 0; // it's really undefined
48 if(*L
< 0.5) *S
= (cmax
-cmin
)/(cmax
+cmin
);
49 else *S
= (cmax
-cmin
)/(2.0-cmax
-cmin
);
59 if(g
== cmax
) *H
= 2.0 + (b
-r
) / delta
;
60 else *H
= 4.0 + (r
-g
) / delta
;
63 if (*H
< 0.0) *H
+= 1;
67 /******************************************************************************
69 PURPOSE: Convert a hue (color) to RGB
70 COPYRIGHT:1995-1997 Robert Mashlan
71 Modified for LabWindows/CVI, 1999 Guillaume Dargaud
72 ******************************************************************************/
73 double HueToRGB(const double m1
, const double m2
, double h
)
77 if (6.0*h
< 1 ) return (m1
+(m2
-m1
)*h
*6.0);
78 if (2.0*h
< 1 ) return m2
;
79 if (3.0*h
< 2.0) return (m1
+(m2
-m1
)*((2.0/3.0)-h
)*6.0);
84 /******************************************************************************
86 PURPOSE: Convert from HSL to RGB
87 IN: Hue, Saturation, Luminance from 0 to 1
88 RETURN: RGB color (0xBBGGRR)
89 COPYRIGHT:1995-1997 Robert Mashlan
90 Modified for LabWindows/CVI, 1999 Guillaume Dargaud
91 ******************************************************************************/
93 COLORREF
HLStoRGB(const double H
, const double L
, const double S
)
111 r
= HueToRGB(m1
,m2
,H
+1.0/3.0);
112 g
= HueToRGB(m1
,m2
,H
);
113 b
= HueToRGB(m1
,m2
,H
-1.0/3.0);
116 return RGB(r
*255, g
*255, b
*255);
121 /******************************************************************************
122 FUNCTION: ColorScaleHSL
123 PURPOSE: Returns the HSL linear interpolated color between 2 colors
124 (more natural looking than RGB interpolation)
125 For instance if the luminance is the same in Col1 and Col2,
126 then the luminance of the result will be the same
127 If Ratio=0, you get Col1,
128 If Ratio=1, you get Col2
129 IN: Col1: low color in hex 0xBBGGRR format
130 Col2: high color in hex 0xBBGGRR format
131 Ratio: 0 for low color, 1 for high color, or in between
132 EXAMPLE: Col1=0, Col2=0xFF00FF, Ratio=0.5 returns 0x1F5F3F
133 ******************************************************************************/
134 COLORREF
ColorScaleHSL( const COLORREF Col1
, const COLORREF Col2
, const double Ratio
)
136 static double H1
, H2
, S1
, S2
, L1
, L2
;
138 if (Ratio
<=0) return Col1
; // Ratio parameter must be between 0 and 1
139 else if (Ratio
>=1) return Col2
;
141 RGBtoHLS( Col1
, &H1
, &L1
, &S1
);
142 RGBtoHLS( Col2
, &H2
, &L2
, &S2
);
143 return HLStoRGB( H1
+(H2
-H1
)*Ratio
, L1
+(L2
-L1
)*Ratio
, S1
+(S2
-S1
)*Ratio
);
147 /******************************************************************************
148 FUNCTION: ColorScaleRGB
149 PURPOSE: Returns the RGB linear interpolated color between 2 colors
150 If Ratio=0, you get Col1,
151 If Ratio=1, you get Col2
152 IN: Col1: low color in hex 0xBBGGRR format
153 Col2: high color in hex 0xBBGGRR format
154 Ratio: 0 for low color, 1 for high color, or in between
155 EXAMPLE: Col1=0, Col2=0xFF00FF, Ratio=0.5 returns 0x800080
156 ******************************************************************************/
157 COLORREF
ColorScaleRGB( const COLORREF Col1
,
159 const double Ratio
) {
160 int R1
=(Col1
)&0xFF, G1
=(Col1
>>8)&0xFF, B1
=(Col1
>>16)&0xFF;
161 int R2
=(Col2
)&0xFF, G2
=(Col2
>>8)&0xFF, B2
=(Col2
>>16)&0xFF;
163 if (Ratio
<=0) return Col1
; // Ratio parameter must be between 0 and 1
164 else if (Ratio
>=1) return Col2
;
167 (R1 + (R2 - R1) * (Ratio + 0.02) + 0.5), // rounding
168 (G1 + (G2 - G1) * (Ratio - 0.00) + 0.5),
169 (B1 + (B2 - B1) * (Ratio + 0.05) + 0.5)
177 R1 = (int)(double(R1) * r + 0.5);
178 G1 = (int)(double(G1) * r + 0.5);
179 B1 = (int)(double(B1) * r + 0.5);
180 return RGB(R1,G1,B1);*/
183 (R1
+ (R2
- R1
) * (Ratio
+ 0.02) + 0.5), // rounding
184 (G1
+ (G2
- G1
) * (Ratio
- 0.00) + 0.5),
185 (B1
+ (B2
- B1
) * (Ratio
+ 0.05) + 0.5)
191 COLORREF
ColorDarker(COLORREF col
, double ratio
)
193 return ColorScaleHSL(col
, RGB(0,0,0), ratio
);
196 COLORREF
ColorLighter(COLORREF col
, double ratio
)
198 return ColorScaleHSL(col
, RGB(255,255,255), ratio
);
206 typedef enum { Red
, Green
, Blue
};
208 void RGBtoHLS(COLORREF rgb
, double *Hue
, double *Lum
, double *Sat
)
213 BYTE red
, green
, blue
;
215 red
= GetRValue(rgb
);
216 green
= GetGValue(rgb
);
217 blue
= GetBValue(rgb
);
221 mn
= red
; mx
= green
; major
= Green
;
225 mn
= green
; mx
= red
; major
= Red
;
234 mx
= blue
; major
= Blue
;
245 *Lum
= (mx
+ mn
) / 510;
248 *Sat
= (mx
-mn
) / (mn
+mx
);
250 *Sat
= (mx
-mn
) / (510-mn
-mx
);
254 case Red
: *Hue
= (green
-blue
) * 60.0 / (mx
-mn
) + 360.0;
257 case Green
: *Hue
= (blue
-red
) * 60.0 / (mx
-mn
) + 120.0;
260 case Blue
: *Hue
= (red
-green
) * 60.0 / (mx
-mn
) + 240.0;
270 static BYTE
Value(double m1
, double m2
, double hue
)
273 if(hue
> 360) hue
-= 360;
274 else if(hue
< 0) hue
+= 360;
277 m1
= m1
+ (m2
- m1
) * hue
/ 60;
281 m1
= m1
+ (m2
- m1
) * (240 - hue
) / 60;
283 return (BYTE
)(m1
* 255);
286 COLORREF
HLStoRGB(const double Hue
, const double Lum
, const double Sat
)
288 BYTE red
, green
, blue
;
292 red
= green
= blue
= (BYTE
)(Lum
* 255);
299 m2
= Lum
+ Lum
* Sat
;
301 m2
= Lum
+ Sat
- Lum
* Sat
;
305 red
= Value(m1
, m2
, Hue
+ 120);
306 green
= Value(m1
, m2
, Hue
);
307 blue
= Value(m1
, m2
, Hue
- 120);
310 return RGB(red
, green
, blue
);
313 COLORREF
ScaleLumRGB(COLORREF col1
, double ratio
)
317 RGBtoHLS(col1
, &H1
, &L1
, &S1
);
321 return HLStoRGB(H1
, L1
, S1
);
324 COLORREF
ColorScaleHSL(const COLORREF Col1
, const COLORREF Col2
, const double Ratio
)
326 static double H1
, H2
, S1
, S2
, L1
, L2
;
328 if(Ratio
<= 0) return Col1
; // Ratio parameter must be between 0 and 1
329 else if(Ratio
>= 1) return Col2
;
331 RGBtoHLS( Col1
, &H1
, &L1
, &S1
);
332 RGBtoHLS( Col2
, &H2
, &L2
, &S2
);
334 return HLStoRGB( H1
/*+ (H2 - H1 ) * Ratio*/, L1
+ (L2
- L1
) * Ratio
, S1
+ (S2
- S1
) * Ratio
* 2);
337 COLORREF
ColorScaleRGB(const COLORREF Col1
, const COLORREF Col2
, const double Ratio
)
339 return ColorScaleHSL(Col1
, Col2
, Ratio
);