Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/runtime/color.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 7 мая 2020 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/test-fw/utest.h>
23 #include <lsp-plug.in/test-fw/helpers.h>
24 #include <lsp-plug.in/runtime/Color.h>
25
26 UTEST_BEGIN("runtime.runtime", color)
27
28 status_t parse_rgb(const char *text, uint32_t value)
29 {
30 Color c;
31 printf(" parsing value %s -> 0x%08lx\n", text, (long)value);
32 status_t res = c.parse_rgb(text);
33 if (res == STATUS_OK)
34 {
35 uint32_t v = c.rgba32();
36 printf(" parsed: 0x%08lx\n", (long)v);
37 res = (v == value) ? STATUS_OK : STATUS_INVALID_VALUE;
38 }
39 return res;
40 }
41
42 status_t parse_rgba(const char *text, uint32_t value)
43 {
44 Color c;
45 printf(" parsing value %s -> %08lx\n", text, (long)value);
46 status_t res = c.parse_rgba(text);
47 if (res == STATUS_OK)
48 {
49 uint32_t v = c.rgba32();
50 printf(" parsed: 0x%08lx\n", (long)v);
51 res = (v == value) ? STATUS_OK : STATUS_INVALID_VALUE;
52 }
53 return res;
54 }
55
56 status_t parse_hsl(const char *text, uint32_t value)
57 {
58 Color c;
59 printf(" parsing value %s -> %08lx\n", text, (long)value);
60 status_t res = c.parse_hsl(text);
61 if (res == STATUS_OK)
62 {
63 uint32_t v = c.hsla32();
64 printf(" parsed: 0x%08lx\n", (long)v);
65 res = (v == value) ? STATUS_OK : STATUS_INVALID_VALUE;
66 }
67 return res;
68 }
69
70 status_t parse_hsla(const char *text, uint32_t value)
71 {
72 Color c;
73 printf(" parsing value %s -> %08lx\n", text, (long)value);
74 status_t res = c.parse_hsla(text);
75 if (res == STATUS_OK)
76 {
77 uint32_t v = c.hsla32();
78 printf(" parsed: 0x%08lx\n", (long)v);
79 res = (v == value) ? STATUS_OK : STATUS_INVALID_VALUE;
80 }
81 return res;
82 }
83
84 status_t parse(Color &c, const char *text)
85 {
86 printf(" parsing value %s\n", text);
87 status_t res = c.parse(text);
88 if (res != STATUS_OK)
89 return res;
90
91 if (c.is_rgb())
92 printf(" parsed rgba: (%f, %f, %f, %f)\n", c.red(), c.green(), c.blue(), c.alpha());
93 else if (c.is_hsl())
94 printf(" parsed hsla: (%f, %f, %f, %f)\n", c.hsl_hue(), c.hsl_saturation(), c.hsl_lightness(), c.alpha());
95 else if (c.is_xyz())
96 printf(" parsed xyz: (%f, %f, %f, %f)\n", c.xyz_x(), c.xyz_y(), c.xyz_z(), c.alpha());
97 return STATUS_OK;
98 }
99
100 bool match4(float a, float b, float c, float d, float ma, float mb, float mc, float md)
101 {
102 return (float_equals_adaptive(a, ma, 1e-4f)) &&
103 (float_equals_adaptive(b, mb, 1e-4f)) &&
104 (float_equals_adaptive(c, mc, 1e-4f)) &&
105 (float_equals_adaptive(d, md, 1e-4f));
106 }
107
108 void test_parse_rgb()
109 {
110 printf("Testing parse_rgb...\n");
111
112 UTEST_ASSERT(parse_rgb("#123", 0x00112233) == STATUS_OK);
113 UTEST_ASSERT(parse_rgb("#112233", 0x00112233) == STATUS_OK);
114 UTEST_ASSERT(parse_rgb("#111222333", 0x00112233) == STATUS_OK);
115 UTEST_ASSERT(parse_rgb("#111122223333", 0x00112233) == STATUS_OK);
116 UTEST_ASSERT(parse_rgb(" #112233\t", 0x00112233) == STATUS_OK);
117
118 UTEST_ASSERT(parse_rgb("#0000", 0) == STATUS_BAD_FORMAT);
119 UTEST_ASSERT(parse_rgb("112233", 0) == STATUS_BAD_FORMAT);
120 UTEST_ASSERT(parse_rgb("@112233", 0) == STATUS_BAD_FORMAT);
121 UTEST_ASSERT(parse_rgb("# 112233", 0) == STATUS_BAD_FORMAT);
122 UTEST_ASSERT(parse_rgb("", 0) == STATUS_NO_DATA);
123 UTEST_ASSERT(parse_rgb(" ", 0) == STATUS_NO_DATA);
124 }
125
126 void test_parse_rgba()
127 {
128 printf("Testing parse_rgba...\n");
129
130 UTEST_ASSERT(parse_rgba("#1234", 0x11223344) == STATUS_OK);
131 UTEST_ASSERT(parse_rgba("#11223344", 0x11223344) == STATUS_OK);
132 UTEST_ASSERT(parse_rgba("#111222333444", 0x11223344) == STATUS_OK);
133 UTEST_ASSERT(parse_rgba("#1111222233334444", 0x11223344) == STATUS_OK);
134 UTEST_ASSERT(parse_rgba(" #11223344\t", 0x11223344) == STATUS_OK);
135
136 UTEST_ASSERT(parse_rgba("#000", 0) == STATUS_BAD_FORMAT);
137 UTEST_ASSERT(parse_rgba("11223344", 0) == STATUS_BAD_FORMAT);
138 UTEST_ASSERT(parse_rgba("@11223344", 0) == STATUS_BAD_FORMAT);
139 UTEST_ASSERT(parse_rgba("# 11223344", 0) == STATUS_BAD_FORMAT);
140 UTEST_ASSERT(parse_rgba("", 0) == STATUS_NO_DATA);
141 UTEST_ASSERT(parse_rgba(" ", 0) == STATUS_NO_DATA);
142 }
143
144 void test_parse_hsl()
145 {
146 printf("Testing parse_hsl...\n");
147
148 UTEST_ASSERT(parse_hsl("@123", 0x00112233) == STATUS_OK);
149 UTEST_ASSERT(parse_hsl("@112233", 0x00112233) == STATUS_OK);
150 UTEST_ASSERT(parse_hsl("@111222333", 0x00112233) == STATUS_OK);
151 UTEST_ASSERT(parse_hsl("@111122223333", 0x00112233) == STATUS_OK);
152 UTEST_ASSERT(parse_hsl(" @112233\t", 0x00112233) == STATUS_OK);
153
154 UTEST_ASSERT(parse_hsl("@0000", 0) == STATUS_BAD_FORMAT);
155 UTEST_ASSERT(parse_hsl("112233", 0) == STATUS_BAD_FORMAT);
156 UTEST_ASSERT(parse_hsl("#112233", 0) == STATUS_BAD_FORMAT);
157 UTEST_ASSERT(parse_hsl("@ 112233", 0) == STATUS_BAD_FORMAT);
158 UTEST_ASSERT(parse_hsl("", 0) == STATUS_NO_DATA);
159 UTEST_ASSERT(parse_hsl(" ", 0) == STATUS_NO_DATA);
160 }
161
162 void test_parse_hsla()
163 {
164 printf("Testing parse_hsla...\n");
165
166 UTEST_ASSERT(parse_hsla("@1234", 0x11223344) == STATUS_OK);
167 UTEST_ASSERT(parse_hsla("@11223344", 0x11223344) == STATUS_OK);
168 UTEST_ASSERT(parse_hsla("@111222333444", 0x11223344) == STATUS_OK);
169 UTEST_ASSERT(parse_hsla("@1111222233334444", 0x11223344) == STATUS_OK);
170 UTEST_ASSERT(parse_hsla(" @11223344\t", 0x11223344) == STATUS_OK);
171
172 UTEST_ASSERT(parse_hsla("@000", 0) == STATUS_BAD_FORMAT);
173 UTEST_ASSERT(parse_hsla("11223344", 0) == STATUS_BAD_FORMAT);
174 UTEST_ASSERT(parse_hsla("#11223344", 0) == STATUS_BAD_FORMAT);
175 UTEST_ASSERT(parse_hsla("@ 11223344", 0) == STATUS_BAD_FORMAT);
176 UTEST_ASSERT(parse_hsla("", 0) == STATUS_NO_DATA);
177 UTEST_ASSERT(parse_hsla(" ", 0) == STATUS_NO_DATA);
178 }
179
180 void test_parse()
181 {
182 printf("Testing parse_hsla...\n");
183 Color c;
184
185 // Match RGB parse
186 UTEST_ASSERT(parse(c, "rgb(0.1,0.2,0.3)") == STATUS_OK);
187 UTEST_ASSERT(c.is_rgb());
188 UTEST_ASSERT(match4(c.red(), c.green(), c.blue(), c.alpha(), 0.1f, 0.2f, 0.3f, 0.0f));
189
190 UTEST_ASSERT(parse(c, "RGBA(0.2, 0.3, 0.4, 0.5)") == STATUS_OK);
191 UTEST_ASSERT(c.is_rgb());
192 UTEST_ASSERT(match4(c.red(), c.green(), c.blue(), c.alpha(), 0.2f, 0.3f, 0.4f, 0.5f));
193
194 // Match HSL parse
195 UTEST_ASSERT(parse(c, "hsl (72, 50, 150) ") == STATUS_OK);
196 UTEST_ASSERT(c.is_hsl());
197 UTEST_ASSERT(match4(c.hsl_hue(), c.hsl_saturation(), c.hsl_lightness(), c.alpha(), 0.2f, 0.5f, 0.75f, 0.0f));
198
199 UTEST_ASSERT(parse(c, "HSLA ( 144, 100, 50, 0.3 ) ") == STATUS_OK);
200 UTEST_ASSERT(c.is_hsl());
201 UTEST_ASSERT(match4(c.hsl_hue(), c.hsl_saturation(), c.hsl_lightness(), c.alpha(), 0.4f, 1.0f, 0.25f, 0.3f));
202
203 // Match XYZ parse
204 UTEST_ASSERT(parse(c, "xyz(10.0, 20.0, 30.5)") == STATUS_OK);
205 UTEST_ASSERT(c.is_xyz());
206 UTEST_ASSERT(match4(c.xyz_x(), c.xyz_y(), c.xyz_z(), c.alpha(), 10.0f, 20.0f, 30.5f, 0.0f));
207
208 UTEST_ASSERT(parse(c, "XYZA(20.5, 30.0, 10.1, 0.7)") == STATUS_OK);
209 UTEST_ASSERT(c.is_xyz());
210 UTEST_ASSERT(match4(c.xyz_x(), c.xyz_y(), c.xyz_z(), c.alpha(), 20.5f, 30.0f, 10.1f, 0.7f));
211
212 // Match LAB parse
213 UTEST_ASSERT(parse(c, "lab(11.0, 21.0, 31.5)") == STATUS_OK);
214 UTEST_ASSERT(c.is_lab());
215 UTEST_ASSERT(match4(c.lab_l(), c.lab_a(), c.lab_b(), c.alpha(), 11.0f, 21.0f, 31.5f, 0.0f));
216
217 UTEST_ASSERT(parse(c, "LABA(21.5, 31.0, 11.1, 0.8)") == STATUS_OK);
218 UTEST_ASSERT(c.is_lab());
219 UTEST_ASSERT(match4(c.lab_l(), c.lab_a(), c.lab_b(), c.alpha(), 21.5f, 31.0f, 11.1f, 0.8f));
220
221 // Match LCH parse
222 UTEST_ASSERT(parse(c, "hcl(90.0, 50.0, 51.0)") == STATUS_OK);
223 UTEST_ASSERT(c.is_lch());
224 UTEST_ASSERT(match4(c.lch_h(), c.lch_c(), c.lch_l(), c.alpha(), 90.0f, 50.0f, 51.0f, 0.0f));
225
226 UTEST_ASSERT(parse(c, "LCH(50.5, 51.5, 90.5)") == STATUS_OK);
227 UTEST_ASSERT(c.is_lch());
228 UTEST_ASSERT(match4(c.lch_h(), c.lch_c(), c.lch_l(), c.alpha(), 90.5f, 51.5f, 50.5f, 0.0f));
229
230 UTEST_ASSERT(parse(c, "HCLA(270.0, 52.0, 12.0, 0.5)") == STATUS_OK);
231 UTEST_ASSERT(c.is_lch());
232 UTEST_ASSERT(match4(c.lch_h(), c.lch_c(), c.lch_l(), c.alpha(), 270.0f, 52.0f, 12.0f, 0.5f));
233
234 UTEST_ASSERT(parse(c, "lcha(52.5, 12.5, 270.5, 0.6)") == STATUS_OK);
235 UTEST_ASSERT(c.is_lch());
236 UTEST_ASSERT(match4(c.lch_h(), c.lch_c(), c.lch_l(), c.alpha(), 270.5f, 12.5f, 52.5f, 0.6f));
237
238 // Invalid parses
239 UTEST_ASSERT(parse(c, "blablabla(1, 2, 3)") == STATUS_BAD_FORMAT);
240 UTEST_ASSERT(parse(c, "rgb(1)") == STATUS_BAD_FORMAT);
241 UTEST_ASSERT(parse(c, "rgb(1, 2)") == STATUS_BAD_FORMAT);
242 UTEST_ASSERT(parse(c, "hsl(1, 2, 3, 4)") == STATUS_BAD_FORMAT);
243 UTEST_ASSERT(parse(c, "hsla(1, 2, 3)") == STATUS_BAD_FORMAT);
244 UTEST_ASSERT(parse(c, "xyz(") == STATUS_BAD_FORMAT);
245 UTEST_ASSERT(parse(c, "xyz( )") == STATUS_BAD_FORMAT);
246 UTEST_ASSERT(parse(c, "xyz(1)") == STATUS_BAD_FORMAT);
247 UTEST_ASSERT(parse(c, "xyz(1, 2, )") == STATUS_BAD_FORMAT);
248 UTEST_ASSERT(parse(c, "xyz(1; 2; 3)") == STATUS_BAD_FORMAT);
249 UTEST_ASSERT(parse(c, "xyza(1 2 3)") == STATUS_BAD_FORMAT);
250
251 // Empty parses
252 UTEST_ASSERT(parse(c, "") == STATUS_NO_DATA);
253 UTEST_ASSERT(parse(c, " ") == STATUS_NO_DATA);
254 UTEST_ASSERT(parse(c, "\t\n\r") == STATUS_NO_DATA);
255 }
256
257 bool check_component(int c1, int c2, int delta = 5)
258 {
259 c1 -= c2;
260 return (c1 >= -delta) && (c1 <= delta);
261 }
262
263 bool test_rgb2hsl(const char *value)
264 {
265 Color c1, c2;
266 char buf[32];
267
268 UTEST_ASSERT(c1.parse3(value) == STATUS_OK);
269 printf(" col = %s\n", value);
270 c1.format_hsl(buf, sizeof(buf), 2);
271 printf(" hsl = %s\n", buf);
272 c2.set_hsl24(c1.hsl24());
273 c2.format_rgb(buf, sizeof(buf), 2);
274 printf(" rgb = %s\n", buf);
275
276 uint32_t v1 = c1.rgb24(), v2 = c2.rgb24();
277
278 return
279 check_component(v1 & 0xff, v2 & 0xff) &&
280 check_component((v1 >> 8) & 0xff, (v2 >> 8) & 0xff) &&
281 check_component((v1 >> 16) & 0xff, (v2 >> 16) & 0xff);
282 }
283
284 void test_convert_hsl()
285 {
286 printf("Testing RGB <-> HSL conversion...\n");
287
288 UTEST_ASSERT(test_rgb2hsl("#ff0000"));
289 UTEST_ASSERT(test_rgb2hsl("#00ff00"));
290 UTEST_ASSERT(test_rgb2hsl("#0000ff"));
291 UTEST_ASSERT(test_rgb2hsl("#ffff00"));
292 UTEST_ASSERT(test_rgb2hsl("#ff00ff"));
293 UTEST_ASSERT(test_rgb2hsl("#00ffff"));
294 UTEST_ASSERT(test_rgb2hsl("#ffffff"));
295 UTEST_ASSERT(test_rgb2hsl("#000000"));
296 UTEST_ASSERT(test_rgb2hsl("#cccccc"));
297 }
298
299 bool test_rgb2xyz(const char *value, float x, float y, float z)
300 {
301 Color c1, c2, c3;
302 char buf1[32], buf2[32];
303 float cx[2], cy[2], cz[2];
304
305 // Perform RGB -> XYZ conversion
306 UTEST_ASSERT(c1.parse3(value) == STATUS_OK);
307 printf(" col = %s\n", value);
308 printf(" chk = xyz(%f, %f, %f)\n", x, y, z);
309
310 cx[0] = c1.xyz_x();
311 cy[0] = c1.xyz_y();
312 cz[0] = c1.xyz_z();
313 c1.get_xyz(cx[1], cy[1], cz[1]);
314
315 printf(" xyz = (%f, %f, %f)\n", cx[0], cy[0], cz[0]);
316 if ((cx[0] != cx[1]) || (cy[0] != cy[1]) || (cz[0] != cz[1]))
317 return false;
318 if ((!float_equals_adaptive(x, cx[0], 1e-4f)) ||
319 (!float_equals_adaptive(y, cy[0], 1e-4f)) ||
320 (!float_equals_adaptive(z, cz[0], 1e-4f)))
321 return false;
322
323 // Perform XYZ -> RGB conversion
324 c2.xyz_x(x);
325 c2.xyz_y(y);
326 c2.xyz_z(z);
327 c2.format_rgb(buf1, sizeof(buf1), 2);
328 printf(" rgb = %s\n", buf1);
329
330 c3.set_xyz(x, y, z);
331 c3.format_rgb(buf2, sizeof(buf2), 2);
332 if (strcmp(buf1, buf2) != 0)
333 return false;
334
335 // Check components
336 uint32_t v1 = c1.rgb24(), v2 = c2.rgb24();
337
338 return
339 check_component(v1 & 0xff, v2 & 0xff, 1) &&
340 check_component((v1 >> 8) & 0xff, (v2 >> 8) & 0xff, 1) &&
341 check_component((v1 >> 16) & 0xff, (v2 >> 16) & 0xff, 1);
342 }
343
344 void test_convert_xyz()
345 {
346 printf("Testing RGB <-> XYZ conversion...\n");
347
348 UTEST_ASSERT(test_rgb2xyz("#000000", 0.0f, 0.0f, 0.0f));
349
350 UTEST_ASSERT(test_rgb2xyz("#ff0000", 41.24f, 21.26f, 1.93f ));
351 UTEST_ASSERT(test_rgb2xyz("#00ff00", 35.76f, 71.52f, 11.92f ));
352 UTEST_ASSERT(test_rgb2xyz("#0000ff", 18.05f, 7.22f, 95.05f ));
353 UTEST_ASSERT(test_rgb2xyz("#ffff00", 77.00f, 92.78f, 13.85f ));
354 UTEST_ASSERT(test_rgb2xyz("#ff00ff", 59.29f, 28.48f, 96.98f ));
355 UTEST_ASSERT(test_rgb2xyz("#00ffff", 53.81f, 78.74f, 106.97f ));
356 UTEST_ASSERT(test_rgb2xyz("#ffffff", 95.05f, 100.00f, 108.90f ));
357
358 UTEST_ASSERT(test_rgb2xyz("#880000", 10.15334f, 5.23424f, 0.47517f ));
359 UTEST_ASSERT(test_rgb2xyz("#008800", 8.80416f, 17.60831f, 2.93472f ));
360 UTEST_ASSERT(test_rgb2xyz("#000088", 4.44393f, 1.77757f, 23.40144f ));
361 UTEST_ASSERT(test_rgb2xyz("#888800", 18.95750f, 22.84256f, 3.40989f ));
362 UTEST_ASSERT(test_rgb2xyz("#880088", 14.59728f, 7.01181f, 23.87660f ));
363 UTEST_ASSERT(test_rgb2xyz("#008888", 13.24809f, 19.38589f, 26.33616f ));
364 UTEST_ASSERT(test_rgb2xyz("#888888", 23.40144f, 24.62013f, 26.81132f ));
365
366 UTEST_ASSERT(test_rgb2xyz("#ffcc00", 62.83287f, 64.44573f, 9.12762f ));
367 UTEST_ASSERT(test_rgb2xyz("#ff00cc", 52.13908f, 25.61963f, 59.32379f ));
368 UTEST_ASSERT(test_rgb2xyz("#00ffcc", 46.65908f, 75.87963f, 69.31379f ));
369 UTEST_ASSERT(test_rgb2xyz("#ccff00", 60.66184f, 84.35737f, 13.08539f ));
370 UTEST_ASSERT(test_rgb2xyz("#cc00ff", 42.95184f, 20.05737f, 96.21539f ));
371 UTEST_ASSERT(test_rgb2xyz("#00ccff", 39.64287f, 50.40573f, 102.24762f ));
372 }
373
374 bool test_rgb2lab(const char *value, float l, float a, float b)
375 {
376 Color c1, c2, c3;
377 char buf1[32], buf2[32];
378 float cl[2], ca[2], cb[2];
379
380 // Perform RGB -> XYZ conversion
381 UTEST_ASSERT(c1.parse3(value) == STATUS_OK);
382 printf(" col = %s\n", value);
383 printf(" chk = lab(%f, %f, %f)\n", l, a, b);
384
385 cl[0] = c1.lab_l();
386 ca[0] = c1.lab_a();
387 cb[0] = c1.lab_b();
388 c1.get_lab(cl[1], ca[1], cb[1]);
389
390 printf(" lab = (%f, %f, %f)\n", cl[0], ca[0], cb[0]);
391 if ((cl[0] != cl[1]) || (ca[0] != ca[1]) || (cb[0] != cb[1]))
392 return false;
393 if ((!float_equals_adaptive(l, cl[0], 1e-4f)) ||
394 (!float_equals_adaptive(a, ca[0], 1e-4f)) ||
395 (!float_equals_adaptive(b, cb[0], 1e-4f)))
396 return false;
397
398 // Perform XYZ -> RGB conversion
399 c2.lab_l(l);
400 c2.lab_a(a);
401 c2.lab_b(b);
402 c2.format_rgb(buf1, sizeof(buf1), 2);
403 printf(" rgb = %s\n", buf1);
404
405 c3.set_lab(l, a, b);
406 c3.format_rgb(buf2, sizeof(buf2), 2);
407 if (strcmp(buf1, buf2) != 0)
408 return false;
409
410 // Check components
411 uint32_t v1 = c1.rgb24(), v2 = c2.rgb24();
412
413 return
414 check_component(v1 & 0xff, v2 & 0xff, 1) &&
415 check_component((v1 >> 8) & 0xff, (v2 >> 8) & 0xff, 1) &&
416 check_component((v1 >> 16) & 0xff, (v2 >> 16) & 0xff, 1);
417 }
418
419 void test_convert_lab()
420 {
421 printf("Testing RGB <-> LAB conversion...\n");
422
423 UTEST_ASSERT(test_rgb2lab("#000000", 0.00000f, 0.00000f, 0.00000f));
424
425 UTEST_ASSERT(test_rgb2lab("#ff0000", 53.23288f, 80.10933f, 67.22010f ));
426 UTEST_ASSERT(test_rgb2lab("#00ff00", 87.73705f, -86.18465f, 83.18118f ));
427 UTEST_ASSERT(test_rgb2lab("#0000ff", 32.30259f, 79.19666f, -107.86369f ));
428 UTEST_ASSERT(test_rgb2lab("#ffff00", 97.13825f, -21.55590f, 94.48249f ));
429 UTEST_ASSERT(test_rgb2lab("#ff00ff", 60.31994f, 98.25423f, -60.84298f ));
430 UTEST_ASSERT(test_rgb2lab("#00ffff", 91.11653f, -48.07964f, -14.13812f ));
431 UTEST_ASSERT(test_rgb2lab("#ffffff", 100.00000f, 0.00531f, -0.01042f ));
432
433 UTEST_ASSERT(test_rgb2lab("#880000", 27.39196f, 50.20879f, 40.43097f ));
434 UTEST_ASSERT(test_rgb2lab("#008800", 49.01756f, -54.01651f, 52.13409f ));
435 UTEST_ASSERT(test_rgb2lab("#000088", 14.27381f, 49.63680f, -67.60395f ));
436 UTEST_ASSERT(test_rgb2lab("#888800", 54.90981f, -13.51026f, 59.21724f ));
437 UTEST_ASSERT(test_rgb2lab("#880088", 31.83379f, 61.58120f, -38.13356f ));
438 UTEST_ASSERT(test_rgb2lab("#008888", 51.13567f, -30.13408f, -8.86111f ));
439 UTEST_ASSERT(test_rgb2lab("#888888", 56.70342f, 0.00331f, -0.00652f ));
440
441 UTEST_ASSERT(test_rgb2lab("#ffcc00", 84.19730f, 3.67990f, 85.22286f ));
442 UTEST_ASSERT(test_rgb2lab("#ff00cc", 57.67424f, 91.74174f, -36.32563f ));
443 UTEST_ASSERT(test_rgb2lab("#00ffcc", 89.80343f, -61.61985f, 10.37161f ));
444 UTEST_ASSERT(test_rgb2lab("#ccff00", 93.60550f, -41.94814f, 90.27765f ));
445 UTEST_ASSERT(test_rgb2lab("#cc00ff", 51.90202f, 91.01245f, -74.84959f ));
446 UTEST_ASSERT(test_rgb2lab("#00ccff", 76.31763f, -24.34486f, -36.68355f ));
447 }
448
449 bool test_rgb2cmyk(const char *value, float c, float m, float y, float k)
450 {
451 Color c1, c2, c3;
452 char buf1[32], buf2[32];
453 float cc[2], cm[2], cy[2], ck[2];
454
455 // Perform RGB -> XYZ conversion
456 UTEST_ASSERT(c1.parse3(value) == STATUS_OK);
457 printf(" col = %s\n", value);
458 printf(" chk = cmyk(%f, %f, %f, %f)\n", c, m, y, k);
459
460 cc[0] = c1.cyan();
461 cm[0] = c1.magenta();
462 cy[0] = c1.yellow();
463 ck[0] = c1.black();
464 c1.get_cmyk(cc[1], cm[1], cy[1], ck[1]);
465
466 printf(" cmyk = (%f, %f, %f, %f)\n", cc[0], cm[0], cy[0], ck[0]);
467 if ((cc[0] != cc[1]) || (cm[0] != cm[1]) || (cy[0] != cy[1]) || (ck[0] != ck[1]))
468 return false;
469 if ((!float_equals_adaptive(c, cc[0], 1e-4f)) ||
470 (!float_equals_adaptive(m, cm[0], 1e-4f)) ||
471 (!float_equals_adaptive(y, cy[0], 1e-4f)) ||
472 (!float_equals_adaptive(k, ck[0], 1e-4f)))
473 return false;
474
475 // Perform XYZ -> RGB conversion
476 c2.cyan(c);
477 c2.magenta(m);
478 c2.yellow(y);
479 c2.black(k);
480 c2.format_rgb(buf1, sizeof(buf1), 2);
481 printf(" rgb = %s\n", buf1);
482
483 c3.set_cmyk(c, m, y, k);
484 c3.format_rgb(buf2, sizeof(buf2), 2);
485 if (strcmp(buf1, buf2) != 0)
486 return false;
487
488 // Check components
489 uint32_t v1 = c1.rgb24(), v2 = c2.rgb24();
490
491 return
492 check_component(v1 & 0xff, v2 & 0xff, 1) &&
493 check_component((v1 >> 8) & 0xff, (v2 >> 8) & 0xff, 1) &&
494 check_component((v1 >> 16) & 0xff, (v2 >> 16) & 0xff, 1);
495 }
496
497 void test_convert_cmyk()
498 {
499 printf("Testing RGB <-> CMYK conversion...\n");
500
501 UTEST_ASSERT(test_rgb2cmyk("#000000", 0.0f, 0.0f, 0.0f, 1.0f));
502
503 UTEST_ASSERT(test_rgb2cmyk("#ff0000", 0.000000f, 1.000000f, 1.000000f, 0.000000f ));
504 UTEST_ASSERT(test_rgb2cmyk("#00ff00", 1.000000f, 0.000000f, 1.000000f, 0.000000f ));
505 UTEST_ASSERT(test_rgb2cmyk("#0000ff", 1.000000f, 1.000000f, 0.000000f, 0.000000f ));
506 UTEST_ASSERT(test_rgb2cmyk("#ffff00", 0.000000f, 0.000000f, 1.000000f, 0.000000f ));
507 UTEST_ASSERT(test_rgb2cmyk("#ff00ff", 0.000000f, 1.000000f, 0.000000f, 0.000000f ));
508 UTEST_ASSERT(test_rgb2cmyk("#00ffff", 1.000000f, 0.000000f, 0.000000f, 0.000000f ));
509 UTEST_ASSERT(test_rgb2cmyk("#ffffff", 0.000000f, 0.000000f, 0.000000f, 0.000000f ));
510
511 UTEST_ASSERT(test_rgb2cmyk("#880000", 0.000000f, 1.000000f, 1.000000f, 0.466667f ));
512 UTEST_ASSERT(test_rgb2cmyk("#008800", 1.000000f, 0.000000f, 1.000000f, 0.466667f ));
513 UTEST_ASSERT(test_rgb2cmyk("#000088", 1.000000f, 1.000000f, 0.000000f, 0.466667f ));
514 UTEST_ASSERT(test_rgb2cmyk("#888800", 0.000000f, 0.000000f, 1.000000f, 0.466667f ));
515 UTEST_ASSERT(test_rgb2cmyk("#880088", 0.000000f, 1.000000f, 0.000000f, 0.466667f ));
516 UTEST_ASSERT(test_rgb2cmyk("#008888", 1.000000f, 0.000000f, 0.000000f, 0.466667f ));
517 UTEST_ASSERT(test_rgb2cmyk("#888888", 0.000000f, 0.000000f, 0.000000f, 0.466667f ));
518
519 UTEST_ASSERT(test_rgb2cmyk("#ffcc00", 0.000000f, 0.200000f, 1.000000f, 0.000000f ));
520 UTEST_ASSERT(test_rgb2cmyk("#ff00cc", 0.000000f, 1.000000f, 0.200000f, 0.000000f ));
521 UTEST_ASSERT(test_rgb2cmyk("#00ffcc", 1.000000f, 0.000000f, 0.200000f, 0.000000f ));
522 UTEST_ASSERT(test_rgb2cmyk("#ccff00", 0.200000f, 0.000000f, 1.000000f, 0.000000f ));
523 UTEST_ASSERT(test_rgb2cmyk("#cc00ff", 0.200000f, 1.000000f, 0.000000f, 0.000000f ));
524 UTEST_ASSERT(test_rgb2cmyk("#00ccff", 1.000000f, 0.200000f, 0.000000f, 0.000000f ));
525 }
526
527 UTEST_MAIN
528 {
529 test_parse_rgb();
530 test_parse_rgba();
531 test_parse_hsl();
532 test_parse_hsla();
533 test_parse();
534
535 test_convert_hsl();
536 test_convert_xyz();
537 test_convert_lab();
538 test_convert_cmyk();
539 }
540
541 UTEST_END
542