2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
5 Desc: ColorWheel function ConvertRGBToHSB()
8 #include <gadgets/colorwheel.h>
10 #include "colorwheel_intern.h"
13 #define MIN(a, b) ((a) < (b) ? (a) : (b))
16 #define MAX(a, b) ((a) > (b) ? (a) : (b))
18 /*****************************************************************************
21 #include <proto/colorwheel.h>
23 AROS_LH2(void, ConvertRGBToHSB
,
26 AROS_LHA(struct ColorWheelRGB
*, rgb
, A0
),
27 AROS_LHA(struct ColorWheelHSB
*, hsb
, A1
),
30 struct Library
*, ColorWheelBase
, 6, ColorWheel
)
33 Converts a color from an RGB representation to an
37 rgb - filled-in ColorWheelRGB structure containing
39 hsb - structure to recieive the converted values
54 27-04-2000 lbischoff implemented
56 ******************************************************************************/
61 Fixed32 R
, G
, B
, H
, S
, I
, max
, min
, delta
;
63 R
= rgb
->cw_Red
>> 16;
64 G
= rgb
->cw_Green
>> 16;
65 B
= rgb
->cw_Blue
>> 16;
67 max
= MAX(MAX(R
, G
), B
);
68 min
= MIN(MIN(R
, G
), B
);
72 S
= (max
!= 0) ? ( (ULONG
) ( ( max
- min
) << 16 ) / (ULONG
) max
) : 0;
73 if( S
>= (FIXED_ONE
-1) ) S
= FIXED_ONE
-1;
85 H
= FixDiv( (G
- B
), delta
);
89 H
= INT_TO_FIXED( 2 ) + FixDiv( (B
- R
), delta
);
93 H
= INT_TO_FIXED( 4 ) + FixDiv( (R
- G
), delta
);
100 H
+= INT_TO_FIXED( 360 );
106 hsb
->cw_Hue
= H
| ( H
<< 16 );
107 hsb
->cw_Saturation
= S
| ( S
<< 16 );
108 hsb
->cw_Brightness
= I
| ( I
<< 16 );
110 #else /* FIXED_MATH */
114 DOUBLE R
, G
, B
, H
, S
, I
, max
, min
, delta
;
116 R
= (DOUBLE
) rgb
->cw_Red
/ (DOUBLE
) 0xFFFFFFFF;
117 G
= (DOUBLE
) rgb
->cw_Green
/ (DOUBLE
) 0xFFFFFFFF;
118 B
= (DOUBLE
) rgb
->cw_Blue
/ (DOUBLE
) 0xFFFFFFFF;
120 max
= MAX(MAX(R
, G
), B
);
121 min
= MIN(MIN(R
, G
), B
);
127 S
= (max
- min
) / max
;
148 H
= 2.0 + (B
- R
) / delta
;
152 H
= 4.0 + (R
- G
) / delta
;
165 hsb
->cw_Hue
= (ULONG
) rint (H
* 0xFFFFFFFF);
166 hsb
->cw_Saturation
= (ULONG
) rint (S
* 0xFFFFFFFF);
167 hsb
->cw_Brightness
= (ULONG
) rint (I
* 0xFFFFFFFF);
170 DOUBLE R
, G
, B
, H
, S
, I
, a
;
172 R
= (DOUBLE
) rgb
->cw_Red
/ (DOUBLE
) 0xFFFFFFFF;
173 G
= (DOUBLE
) rgb
->cw_Green
/ (DOUBLE
) 0xFFFFFFFF;
174 B
= (DOUBLE
) rgb
->cw_Blue
/ (DOUBLE
) 0xFFFFFFFF;
176 I
= (R
+ G
+ B
) / 3.0;
177 a
= MIN (MIN(R
, G
), B
);
179 H
= acos ((0.5*(R
-G
)+(R
-B
))/sqrt(pow(R
-G
, 2.0) + (R
-B
)*(G
-B
)));
184 hsb
->cw_Hue
= (ULONG
) rint (H
* 0xFFFFFFFF);
185 hsb
->cw_Saturation
= (ULONG
) rint (S
* 0xFFFFFFFF);
186 hsb
->cw_Brightness
= (ULONG
) rint (I
* 0xFFFFFFFF);
190 #endif /* FIXED_MATH */
194 } /* ConvertRGBToHSB */