You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

141 lines
5.7 KiB

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