nouveau.hidd: disable warnings about not initialized variables
[AROS.git] / workbench / classes / gadgets / colorwheel / convertrgbtohsb.c
blob2b32aedd2cbd77746629cabc59e4c713c5d2a54e
1 /*
2 Copyright © 1995-2015, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: ColorWheel function ConvertRGBToHSB()
6 Lang: english
7 */
8 #include <gadgets/colorwheel.h>
9 #include <math.h>
10 #include "colorwheel_intern.h"
12 #undef MIN
13 #define MIN(a, b) ((a) < (b) ? (a) : (b))
15 #undef MAX
16 #define MAX(a, b) ((a) > (b) ? (a) : (b))
18 /*****************************************************************************
20 NAME */
21 #include <proto/colorwheel.h>
23 AROS_LH2(void, ConvertRGBToHSB,
25 /* SYNOPSIS */
26 AROS_LHA(struct ColorWheelRGB *, rgb, A0),
27 AROS_LHA(struct ColorWheelHSB *, hsb, A1),
29 /* LOCATION */
30 struct Library *, ColorWheelBase, 6, ColorWheel)
32 /* FUNCTION
33 Converts a color from an RGB representation to an
34 HSB representation
36 INPUTS
37 rgb - filled-in ColorWheelRGB structure containing
38 the values to convert
39 hsb - structure to receive the converted values
41 RESULT
43 NOTES
45 EXAMPLE
47 BUGS
49 SEE ALSO
51 INTERNALS
53 ******************************************************************************/
55 AROS_LIBFUNC_INIT
57 #if FIXED_MATH
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);
67 I = max;
69 S = (max != 0) ? ( (ULONG) ( ( max - min ) << 16 ) / (ULONG) max ) : 0;
70 if( S >= (FIXED_ONE-1) ) S = FIXED_ONE-1;
72 if (S == 0)
74 H = 0; /* -1.0; */
76 else
78 delta = max - min;
80 if (R == max)
82 H = FixDiv( (G - B), delta );
84 else if (G == max)
86 H = INT_TO_FIXED( 2 ) + FixDiv( (B - R), delta );
88 else if (B == max)
90 H = INT_TO_FIXED( 4 ) + FixDiv( (R - G), delta );
93 H *= 60;
95 if (H < 0 )
97 H += INT_TO_FIXED( 360 );
100 H /= 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 */
109 #if 1
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);
120 I = max;
122 if (max != 0.0)
124 S = (max - min) / max;
126 else
128 S = 0.0;
131 if (S == 0.0)
133 H = 0.0; /* -1.0; */
135 else
137 delta = max - min;
139 if (R == max)
141 H = (G - B) / delta;
143 else if (G == max)
145 H = 2.0 + (B - R) / delta;
147 else if (B == max)
149 H = 4.0 + (R - G) / delta;
152 H = H * 60.0;
154 if (H < 0.0)
156 H = H + 360;
159 H = H / 360.0;
162 hsb->cw_Hue = (ULONG) rint (H * 0xFFFFFFFF);
163 hsb->cw_Saturation = (ULONG) rint (S * 0xFFFFFFFF);
164 hsb->cw_Brightness = (ULONG) rint (I * 0xFFFFFFFF);
166 #else
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);
175 S = 1.0 - (a/I);
176 H = acos ((0.5*(R-G)+(R-B))/sqrt(pow(R-G, 2.0) + (R-B)*(G-B)));
177 if (B/I > G/I)
178 H = PI2 - H;
179 H /= PI2;
181 hsb->cw_Hue = (ULONG) rint (H * 0xFFFFFFFF);
182 hsb->cw_Saturation = (ULONG) rint (S * 0xFFFFFFFF);
183 hsb->cw_Brightness = (ULONG) rint (I * 0xFFFFFFFF);
185 #endif
187 #endif /* FIXED_MATH */
189 AROS_LIBFUNC_EXIT
191 } /* ConvertRGBToHSB */