2 Copyright © 1995-2015, 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 receive the converted values
53 ******************************************************************************/
58 Fixed32 R
, G
, B
, H
= 0, S
, I
, max
, min
, delta
;
60 R
= rgb
->cw_Red
>> 16;
61 G
= rgb
->cw_Green
>> 16;
62 B
= rgb
->cw_Blue
>> 16;
64 max
= MAX(MAX(R
, G
), B
);
65 min
= MIN(MIN(R
, G
), B
);
69 S
= (max
!= 0) ? ( (ULONG
) ( ( max
- min
) << 16 ) / (ULONG
) max
) : 0;
70 if( S
>= (FIXED_ONE
-1) ) S
= FIXED_ONE
-1;
82 H
= FixDiv( (G
- B
), delta
);
86 H
= INT_TO_FIXED( 2 ) + FixDiv( (B
- R
), delta
);
90 H
= INT_TO_FIXED( 4 ) + FixDiv( (R
- G
), delta
);
97 H
+= INT_TO_FIXED( 360 );
103 hsb
->cw_Hue
= H
| ( H
<< 16 );
104 hsb
->cw_Saturation
= S
| ( S
<< 16 );
105 hsb
->cw_Brightness
= I
| ( I
<< 16 );
107 #else /* FIXED_MATH */
111 DOUBLE R
, G
, B
, H
= 0.0, S
, I
, max
, min
, delta
;
113 R
= (DOUBLE
) rgb
->cw_Red
/ (DOUBLE
) 0xFFFFFFFF;
114 G
= (DOUBLE
) rgb
->cw_Green
/ (DOUBLE
) 0xFFFFFFFF;
115 B
= (DOUBLE
) rgb
->cw_Blue
/ (DOUBLE
) 0xFFFFFFFF;
117 max
= MAX(MAX(R
, G
), B
);
118 min
= MIN(MIN(R
, G
), B
);
124 S
= (max
- min
) / max
;
145 H
= 2.0 + (B
- R
) / delta
;
149 H
= 4.0 + (R
- G
) / delta
;
162 hsb
->cw_Hue
= (ULONG
) rint (H
* 0xFFFFFFFF);
163 hsb
->cw_Saturation
= (ULONG
) rint (S
* 0xFFFFFFFF);
164 hsb
->cw_Brightness
= (ULONG
) rint (I
* 0xFFFFFFFF);
167 DOUBLE R
, G
, B
, H
, S
, I
, a
;
169 R
= (DOUBLE
) rgb
->cw_Red
/ (DOUBLE
) 0xFFFFFFFF;
170 G
= (DOUBLE
) rgb
->cw_Green
/ (DOUBLE
) 0xFFFFFFFF;
171 B
= (DOUBLE
) rgb
->cw_Blue
/ (DOUBLE
) 0xFFFFFFFF;
173 I
= (R
+ G
+ B
) / 3.0;
174 a
= MIN (MIN(R
, G
), B
);
176 H
= acos ((0.5*(R
-G
)+(R
-B
))/sqrt(pow(R
-G
, 2.0) + (R
-B
)*(G
-B
)));
181 hsb
->cw_Hue
= (ULONG
) rint (H
* 0xFFFFFFFF);
182 hsb
->cw_Saturation
= (ULONG
) rint (S
* 0xFFFFFFFF);
183 hsb
->cw_Brightness
= (ULONG
) rint (I
* 0xFFFFFFFF);
187 #endif /* FIXED_MATH */
191 } /* ConvertRGBToHSB */