Debian Codegraph @

tests/conform/color.c

raw
1 #include <clutter/clutter.h>
2
3 static void
4 color_hls_roundtrip (void)
5 {
6 ClutterColor color;
7 gfloat hue, luminance, saturation;
8
9 /* test luminance only */
10 clutter_color_from_string (&color, "#7f7f7f");
11 g_assert_cmpuint (color.red, ==, 0x7f);
12 g_assert_cmpuint (color.green, ==, 0x7f);
13 g_assert_cmpuint (color.blue, ==, 0x7f);
14
15 clutter_color_to_hls (&color, &hue, &luminance, &saturation);
16 g_assert_cmpfloat (hue, ==, 0.0);
17 g_assert (luminance >= 0.0 && luminance <= 1.0);
18 g_assert_cmpfloat (saturation, ==, 0.0);
19 if (g_test_verbose ())
20 {
21 g_print ("RGB = { %x, %x, %x }, HLS = { %.2f, %.2f, %.2f }\n",
22 color.red,
23 color.green,
24 color.blue,
25 hue,
26 luminance,
27 saturation);
28 }
29
30 color.red = color.green = color.blue = 0;
31 clutter_color_from_hls (&color, hue, luminance, saturation);
32
33 g_assert_cmpuint (color.red, ==, 0x7f);
34 g_assert_cmpuint (color.green, ==, 0x7f);
35 g_assert_cmpuint (color.blue, ==, 0x7f);
36
37 /* full conversion */
38 clutter_color_from_string (&color, "#7f8f7f");
39 color.alpha = 255;
40
41 g_assert_cmpuint (color.red, ==, 0x7f);
42 g_assert_cmpuint (color.green, ==, 0x8f);
43 g_assert_cmpuint (color.blue, ==, 0x7f);
44
45 clutter_color_to_hls (&color, &hue, &luminance, &saturation);
46 g_assert (hue >= 0.0 && hue < 360.0);
47 g_assert (luminance >= 0.0 && luminance <= 1.0);
48 g_assert (saturation >= 0.0 && saturation <= 1.0);
49 if (g_test_verbose ())
50 {
51 g_print ("RGB = { %x, %x, %x }, HLS = { %.2f, %.2f, %.2f }\n",
52 color.red,
53 color.green,
54 color.blue,
55 hue,
56 luminance,
57 saturation);
58 }
59
60 color.red = color.green = color.blue = 0;
61 clutter_color_from_hls (&color, hue, luminance, saturation);
62
63 g_assert_cmpuint (color.red, ==, 0x7f);
64 g_assert_cmpuint (color.green, ==, 0x8f);
65 g_assert_cmpuint (color.blue, ==, 0x7f);
66
67 /* the alpha channel should be untouched */
68 g_assert_cmpuint (color.alpha, ==, 255);
69 }
70
71 static void
72 color_from_string_invalid (void)
73 {
74 ClutterColor color;
75
76 g_assert (!clutter_color_from_string (&color, "ff0000ff"));
77 g_assert (!clutter_color_from_string (&color, "#decaffbad"));
78 g_assert (!clutter_color_from_string (&color, "ponies"));
79 g_assert (!clutter_color_from_string (&color, "rgb(255, 0, 0, 0)"));
80 g_assert (!clutter_color_from_string (&color, "rgba(1.0, 0, 0)"));
81 g_assert (!clutter_color_from_string (&color, "hsl(100, 0, 0)"));
82 g_assert (!clutter_color_from_string (&color, "hsla(10%, 0%, 50%)"));
83 g_assert (!clutter_color_from_string (&color, "hsla(100%, 0%, 50%, 20%)"));
84 g_assert (!clutter_color_from_string (&color, "hsla(0.5, 0.9, 0.2, 0.4)"));
85 }
86
87 static void
88 color_from_string_valid (void)
89 {
90 ClutterColor color;
91
92 g_assert (clutter_color_from_string (&color, "#ff0000ff"));
93 if (g_test_verbose ())
94 {
95 g_print ("color = { %x, %x, %x, %x }, expected = { 0xff, 0, 0, 0xff }\n",
96 color.red,
97 color.green,
98 color.blue,
99 color.alpha);
100 }
101 g_assert_cmpuint (color.red, ==, 0xff);
102 g_assert_cmpuint (color.green, ==, 0);
103 g_assert_cmpuint (color.blue, ==, 0);
104 g_assert_cmpuint (color.alpha, ==, 0xff);
105
106 g_assert (clutter_color_from_string (&color, "#0f0f"));
107 if (g_test_verbose ())
108 {
109 g_print ("color = { %x, %x, %x, %x }, expected = { 0, 0xff, 0, 0xff }\n",
110 color.red,
111 color.green,
112 color.blue,
113 color.alpha);
114 }
115 g_assert_cmpuint (color.red, ==, 0);
116 g_assert_cmpuint (color.green, ==, 0xff);
117 g_assert_cmpuint (color.blue, ==, 0);
118 g_assert_cmpuint (color.alpha, ==, 0xff);
119
120 g_assert (clutter_color_from_string (&color, "#0000ff"));
121 if (g_test_verbose ())
122 {
123 g_print ("color = { %x, %x, %x, %x }, expected = { 0, 0, 0xff, 0xff }\n",
124 color.red,
125 color.green,
126 color.blue,
127 color.alpha);
128 }
129 g_assert_cmpuint (color.red, ==, 0);
130 g_assert_cmpuint (color.green, ==, 0);
131 g_assert_cmpuint (color.blue, ==, 0xff);
132 g_assert_cmpuint (color.alpha, ==, 0xff);
133
134 g_assert (clutter_color_from_string (&color, "#abc"));
135 if (g_test_verbose ())
136 {
137 g_print ("color = { %x, %x, %x, %x }, expected = { 0xaa, 0xbb, 0xcc, 0xff }\n",
138 color.red,
139 color.green,
140 color.blue,
141 color.alpha);
142 }
143 g_assert_cmpuint (color.red, ==, 0xaa);
144 g_assert_cmpuint (color.green, ==, 0xbb);
145 g_assert_cmpuint (color.blue, ==, 0xcc);
146 g_assert_cmpuint (color.alpha, ==, 0xff);
147
148 g_assert (clutter_color_from_string (&color, "#123abc"));
149 if (g_test_verbose ())
150 {
151 g_print ("color = { %x, %x, %x, %x }, expected = { 0x12, 0x3a, 0xbc, 0xff }\n",
152 color.red,
153 color.green,
154 color.blue,
155 color.alpha);
156 }
157 g_assert (color.red == 0x12);
158 g_assert (color.green == 0x3a);
159 g_assert (color.blue == 0xbc);
160 g_assert (color.alpha == 0xff);
161
162 g_assert (clutter_color_from_string (&color, "rgb(255, 128, 64)"));
163 if (g_test_verbose ())
164 {
165 g_print ("color = { %x, %x, %x, %x }, expected = { 255, 128, 64, 255 }\n",
166 color.red,
167 color.green,
168 color.blue,
169 color.alpha);
170 }
171 g_assert_cmpuint (color.red, ==, 255);
172 g_assert_cmpuint (color.green, ==, 128);
173 g_assert_cmpuint (color.blue, ==, 64);
174 g_assert_cmpuint (color.alpha, ==, 255);
175
176 g_assert (clutter_color_from_string (&color, "rgba ( 30%, 0, 25%, 0.5 ) "));
177 if (g_test_verbose ())
178 {
179 g_print ("color = { %x, %x, %x, %x }, expected = { %.1f, 0, %.1f, 128 }\n",
180 color.red,
181 color.green,
182 color.blue,
183 color.alpha,
184 CLAMP (255.0 / 100.0 * 30.0, 0, 255),
185 CLAMP (255.0 / 100.0 * 25.0, 0, 255));
186 }
187 g_assert_cmpuint (color.red, ==, (255.0 / 100.0 * 30.0));
188 g_assert_cmpuint (color.green, ==, 0);
189 g_assert_cmpuint (color.blue, ==, (255.0 / 100.0 * 25.0));
190 g_assert_cmpuint (color.alpha, ==, 127);
191
192 g_assert (clutter_color_from_string (&color, "rgb( 50%, -50%, 150% )"));
193 if (g_test_verbose ())
194 {
195 g_print ("color = { %x, %x, %x, %x }, expected = { 127, 0, 255, 255 }\n",
196 color.red,
197 color.green,
198 color.blue,
199 color.alpha);
200 }
201 g_assert_cmpuint (color.red, ==, 127);
202 g_assert_cmpuint (color.green, ==, 0);
203 g_assert_cmpuint (color.blue, ==, 255);
204 g_assert_cmpuint (color.alpha, ==, 255);
205
206 g_assert (clutter_color_from_string (&color, "hsl( 0, 100%, 50% )"));
207 if (g_test_verbose ())
208 {
209 g_print ("color = { %x, %x, %x, %x }, expected = { 255, 0, 0, 255 }\n",
210 color.red,
211 color.green,
212 color.blue,
213 color.alpha);
214 }
215 g_assert_cmpuint (color.red, ==, 255);
216 g_assert_cmpuint (color.green, ==, 0);
217 g_assert_cmpuint (color.blue, ==, 0);
218 g_assert_cmpuint (color.alpha, ==, 255);
219
220 g_assert (clutter_color_from_string (&color, "hsl( 0, 100%, 50% )"));
221
222 g_assert (clutter_color_from_string (&color, "hsla( 0, 100%, 50%, 0.5 )"));
223 if (g_test_verbose ())
224 {
225 g_print ("color = { %x, %x, %x, %x }, expected = { 255, 0, 0, 127 }\n",
226 color.red,
227 color.green,
228 color.blue,
229 color.alpha);
230 }
231 g_assert_cmpuint (color.red, ==, 255);
232 g_assert_cmpuint (color.green, ==, 0);
233 g_assert_cmpuint (color.blue, ==, 0);
234 g_assert_cmpuint (color.alpha, ==, 127);
235
236 g_test_bug ("662818");
237 g_assert (clutter_color_from_string (&color, "hsla(0,100%,50% , 0.5)"));
238 }
239
240 static void
241 color_to_string (void)
242 {
243 ClutterColor color;
244 gchar *str;
245
246 color.red = 0xcc;
247 color.green = 0xcc;
248 color.blue = 0xcc;
249 color.alpha = 0x22;
250
251 str = clutter_color_to_string (&color);
252 g_assert_cmpstr (str, ==, "#cccccc22");
253
254 g_free (str);
255 }
256
257 static void
258 color_operators (void)
259 {
260 ClutterColor op1, op2;
261 ClutterColor res;
262
263 clutter_color_from_pixel (&op1, 0xff0000ff);
264 g_assert_cmpuint (op1.red, ==, 0xff);
265 g_assert_cmpuint (op1.green, ==, 0);
266 g_assert_cmpuint (op1.blue, ==, 0);
267 g_assert_cmpuint (op1.alpha, ==, 0xff);
268
269 clutter_color_from_pixel (&op2, 0x00ff00ff);
270 g_assert_cmpuint (op2.red, ==, 0);
271 g_assert_cmpuint (op2.green, ==, 0xff);
272 g_assert_cmpuint (op2.blue, ==, 0);
273 g_assert_cmpuint (op2.alpha, ==, 0xff);
274
275 if (g_test_verbose ())
276 g_print ("Adding %x, %x; expected result: %x\n",
277 clutter_color_to_pixel (&op1),
278 clutter_color_to_pixel (&op2),
279 0xffff00ff);
280
281 clutter_color_add (&op1, &op2, &res);
282 g_assert_cmpuint (clutter_color_to_pixel (&res), ==, 0xffff00ff);
283
284 if (g_test_verbose ())
285 g_print ("Checking alpha channel on color add\n");
286
287 op1.alpha = 0xdd;
288 op2.alpha = 0xcc;
289 clutter_color_add (&op1, &op2, &res);
290 g_assert_cmpuint (clutter_color_to_pixel (&res), ==, 0xffff00dd);
291
292 clutter_color_from_pixel (&op1, 0xffffffff);
293 clutter_color_from_pixel (&op2, 0xff00ffff);
294
295 if (g_test_verbose ())
296 g_print ("Subtracting %x, %x; expected result: %x\n",
297 clutter_color_to_pixel (&op1),
298 clutter_color_to_pixel (&op2),
299 0x00ff00ff);
300
301 clutter_color_subtract (&op1, &op2, &res);
302 g_assert_cmpuint (clutter_color_to_pixel (&res), ==, 0x00ff00ff);
303
304 if (g_test_verbose ())
305 g_print ("Checking alpha channel on color subtract\n");
306
307 op1.alpha = 0xdd;
308 op2.alpha = 0xcc;
309 clutter_color_subtract (&op1, &op2, &res);
310 g_assert_cmpuint (clutter_color_to_pixel (&res), ==, 0x00ff00cc);
311 }
312
313 CLUTTER_TEST_SUITE (
314 CLUTTER_TEST_UNIT ("/color/hls-roundtrip", color_hls_roundtrip)
315 CLUTTER_TEST_UNIT ("/color/from-string/invalid", color_from_string_invalid)
316 CLUTTER_TEST_UNIT ("/color/from-string/valid", color_from_string_valid)
317 CLUTTER_TEST_UNIT ("/color/to-string", color_to_string)
318 CLUTTER_TEST_UNIT ("/color/operators", color_operators)
319 )