source: trunk/test/unit/color.cpp @ 2218

Last change on this file since 2218 was 2218, checked in by sam, 7 years ago

color: write the RGB to HSL routine and its associated unit tests.

  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "core.h"
16#include "lol/unit.h"
17
18namespace lol
19{
20
21/* Data taken from “The CIEDE2000 Color-Difference Formula: Implementation
22 * Notes, Supplementary Test Data, and Mathematical Observations” (2004)
23 * by Gaurav Sharma, Wencheng Wu, Edul N. Dalal. Some data is disabled
24 * because it is at the discontinuity limit. */
25static float const ciede2k[] =
26{
27    50.0000f,   2.6772f, -79.7751f, 50.0000f,   0.0000f, -82.7485f,  2.0425f,
28    50.0000f,   3.1571f, -77.2803f, 50.0000f,   0.0000f, -82.7485f,  2.8615f,
29    50.0000f,   2.8361f, -74.0200f, 50.0000f,   0.0000f, -82.7485f,  3.4412f,
30    50.0000f,  -1.3802f, -84.2814f, 50.0000f,   0.0000f, -82.7485f,  1.0000f,
31    50.0000f,  -1.1848f, -84.8006f, 50.0000f,   0.0000f, -82.7485f,  1.0000f,
32    50.0000f,  -0.9009f, -85.5211f, 50.0000f,   0.0000f, -82.7485f,  1.0000f,
33    50.0000f,   0.0000f,   0.0000f, 50.0000f,  -1.0000f,   2.0000f,  2.3669f,
34    50.0000f,  -1.0000f,   2.0000f, 50.0000f,   0.0000f,   0.0000f,  2.3669f,
35    50.0000f,   2.4900f,  -0.0010f, 50.0000f,  -2.4900f,   0.0009f,  7.1792f,
36    //50.0000f,   2.4900f,  -0.0010f, 50.0000f,  -2.4900f,   0.0010f,  7.1792f,
37    50.0000f,   2.4900f,  -0.0010f, 50.0000f,  -2.4900f,   0.0011f,  7.2195f,
38    50.0000f,   2.4900f,  -0.0010f, 50.0000f,  -2.4900f,   0.0012f,  7.2195f,
39    50.0000f,  -0.0010f,   2.4900f, 50.0000f,   0.0009f,  -2.4900f,  4.8045f,
40    //50.0000f,  -0.0010f,   2.4900f, 50.0000f,   0.0010f,  -2.4900f,  4.8045f,
41    50.0000f,  -0.0010f,   2.4900f, 50.0000f,   0.0011f,  -2.4900f,  4.7461f,
42    50.0000f,   2.5000f,   0.0000f, 50.0000f,   0.0000f,  -2.5000f,  4.3065f,
43    50.0000f,   2.5000f,   0.0000f, 73.0000f,  25.0000f, -18.0000f, 27.1492f,
44    50.0000f,   2.5000f,   0.0000f, 61.0000f,  -5.0000f,  29.0000f, 22.8977f,
45    50.0000f,   2.5000f,   0.0000f, 56.0000f, -27.0000f,  -3.0000f, 31.9030f,
46    50.0000f,   2.5000f,   0.0000f, 58.0000f,  24.0000f,  15.0000f, 19.4535f,
47    50.0000f,   2.5000f,   0.0000f, 50.0000f,   3.1736f,   0.5854f,  1.0000f,
48    50.0000f,   2.5000f,   0.0000f, 50.0000f,   3.2972f,   0.0000f,  1.0000f,
49    50.0000f,   2.5000f,   0.0000f, 50.0000f,   1.8634f,   0.5757f,  1.0000f,
50    50.0000f,   2.5000f,   0.0000f, 50.0000f,   3.2592f,   0.3350f,  1.0000f,
51    60.2574f, -34.0099f,  36.2677f, 60.4626f, -34.1751f,  39.4387f,  1.2644f,
52    63.0109f, -31.0961f,  -5.8663f, 62.8187f, -29.7946f,  -4.0864f,  1.2630f,
53    61.2901f,   3.7196f,  -5.3901f, 61.4292f,   2.2480f,  -4.9620f,  1.8731f,
54    35.0831f, -44.1164f,   3.7933f, 35.0232f, -40.0716f,   1.5901f,  1.8645f,
55    22.7233f,  20.0904f, -46.6940f, 23.0331f,  14.9730f, -42.5619f,  2.0373f,
56    36.4612f,  47.8580f,  18.3852f, 36.2715f,  50.5065f,  21.2231f,  1.4146f,
57    90.8027f,  -2.0831f,   1.4410f, 91.1528f,  -1.6435f,   0.0447f,  1.4441f,
58    90.9257f,  -0.5406f,  -0.9208f, 88.6381f,  -0.8985f,  -0.7239f,  1.5381f,
59     6.7747f,  -0.2908f,  -2.4247f,  5.8714f,  -0.0985f,  -2.2286f,  0.6377f,
60     2.0776f,   0.0795f,  -1.1350f,  0.9033f,  -0.0636f,  -0.5514f,  0.9082f,
61};
62
63LOLUNIT_FIXTURE(ColorTest)
64{
65    void SetUp() {}
66
67    void TearDown() {}
68
69    LOLUNIT_TEST(CIEDE2000)
70    {
71        size_t count = sizeof(ciede2k) / sizeof(*ciede2k);
72
73        for (size_t n = 0; n < count; n += 7)
74        {
75            vec3 a(ciede2k[n + 0], ciede2k[n + 1], ciede2k[n + 2]);
76            vec3 b(ciede2k[n + 3], ciede2k[n + 4], ciede2k[n + 5]);
77            float d1 = ciede2k[n + 6];
78
79            float d2 = Color::DistanceCIEDE2000(a, b);
80            float d3 = Color::DistanceCIEDE2000(b, a);
81
82            /* Check that our function works, and check that
83             * it is symmetrical. */
84            LOLUNIT_SET_CONTEXT(n / 7);
85            LOLUNIT_ASSERT_DOUBLES_EQUAL(d1, d2, 0.0001);
86            LOLUNIT_ASSERT_DOUBLES_EQUAL(d2, d3, 0.0001);
87        }
88    }
89
90    LOLUNIT_TEST(RGBToHSV)
91    {
92        for (int r = 0; r < 20; r++)
93        for (int g = 0; g < 20; g++)
94        for (int b = 0; b < 20; b++)
95        {
96            vec3 v1 = vec3(r / 20.f, g / 20.f, b / 20.f);
97            vec3 v2 = Color::HSVToRGB(Color::RGBToHSV(v1));
98
99            LOLUNIT_ASSERT_DOUBLES_EQUAL(v1.r, v2.r, 0.0001);
100            LOLUNIT_ASSERT_DOUBLES_EQUAL(v1.g, v2.g, 0.0001);
101            LOLUNIT_ASSERT_DOUBLES_EQUAL(v1.b, v2.b, 0.0001);
102        }
103    }
104
105    LOLUNIT_TEST(RGBToHSL)
106    {
107        for (int r = 0; r < 20; r++)
108        for (int g = 0; g < 20; g++)
109        for (int b = 0; b < 20; b++)
110        {
111            vec3 v1 = vec3(r / 20.f, g / 20.f, b / 20.f);
112            vec3 v2 = Color::RGBToHSL(v1);
113            vec3 v3 = Color::HSVToHSL(Color::RGBToHSV(v1));
114
115            LOLUNIT_ASSERT_DOUBLES_EQUAL(v2.x, v3.x, 0.0001);
116            LOLUNIT_ASSERT_DOUBLES_EQUAL(v2.y, v3.y, 0.0001);
117            LOLUNIT_ASSERT_DOUBLES_EQUAL(v2.z, v3.z, 0.0001);
118        }
119    }
120};
121
122} /* namespace lol */
123
Note: See TracBrowser for help on using the repository browser.