test_color.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*************************************************************************/
  2. /* test_color.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef TEST_COLOR_H
  31. #define TEST_COLOR_H
  32. #include "core/math/color.h"
  33. #include "thirdparty/doctest/doctest.h"
  34. namespace TestColor {
  35. TEST_CASE("[Color] Constructor methods") {
  36. const Color blue_rgba = Color(0.25098, 0.376471, 1, 0.501961);
  37. const Color blue_html = Color::html("#4060ff80");
  38. const Color blue_hex = Color::hex(0x4060ff80);
  39. const Color blue_hex64 = Color::hex64(0x4040'6060'ffff'8080);
  40. CHECK_MESSAGE(
  41. blue_rgba.is_equal_approx(blue_html),
  42. "Creation with HTML notation should result in components approximately equal to the default constructor.");
  43. CHECK_MESSAGE(
  44. blue_rgba.is_equal_approx(blue_hex),
  45. "Creation with a 32-bit hexadecimal number should result in components approximately equal to the default constructor.");
  46. CHECK_MESSAGE(
  47. blue_rgba.is_equal_approx(blue_hex64),
  48. "Creation with a 64-bit hexadecimal number should result in components approximately equal to the default constructor.");
  49. ERR_PRINT_OFF;
  50. const Color html_invalid = Color::html("invalid");
  51. ERR_PRINT_ON;
  52. CHECK_MESSAGE(
  53. html_invalid.is_equal_approx(Color()),
  54. "Creation with invalid HTML notation should result in a Color with the default values.");
  55. const Color green_rgba = Color(0, 1, 0, 0.25);
  56. const Color green_hsva = Color(0, 0, 0).from_hsv(120 / 360.0, 1, 1, 0.25);
  57. CHECK_MESSAGE(
  58. green_rgba.is_equal_approx(green_hsva),
  59. "Creation with HSV notation should result in components approximately equal to the default constructor.");
  60. }
  61. TEST_CASE("[Color] Operators") {
  62. const Color blue = Color(0.2, 0.2, 1);
  63. const Color dark_red = Color(0.3, 0.1, 0.1);
  64. // Color components may be negative. Also, the alpha component may be greater than 1.0.
  65. CHECK_MESSAGE(
  66. (blue + dark_red).is_equal_approx(Color(0.5, 0.3, 1.1, 2)),
  67. "Color addition should behave as expected.");
  68. CHECK_MESSAGE(
  69. (blue - dark_red).is_equal_approx(Color(-0.1, 0.1, 0.9, 0)),
  70. "Color subtraction should behave as expected.");
  71. CHECK_MESSAGE(
  72. (blue * 2).is_equal_approx(Color(0.4, 0.4, 2, 2)),
  73. "Color multiplication with a scalar should behave as expected.");
  74. CHECK_MESSAGE(
  75. (blue / 2).is_equal_approx(Color(0.1, 0.1, 0.5, 0.5)),
  76. "Color division with a scalar should behave as expected.");
  77. CHECK_MESSAGE(
  78. (blue * dark_red).is_equal_approx(Color(0.06, 0.02, 0.1)),
  79. "Color multiplication with another Color should behave as expected.");
  80. CHECK_MESSAGE(
  81. (blue / dark_red).is_equal_approx(Color(0.666667, 2, 10)),
  82. "Color division with another Color should behave as expected.");
  83. CHECK_MESSAGE(
  84. (-blue).is_equal_approx(Color(0.8, 0.8, 0, 0)),
  85. "Color negation should behave as expected (affecting the alpha channel, unlike `invert()`).");
  86. }
  87. TEST_CASE("[Color] Reading methods") {
  88. const Color dark_blue = Color(0, 0, 0.5, 0.4);
  89. CHECK_MESSAGE(
  90. Math::is_equal_approx(dark_blue.get_h(), 240 / 360.0),
  91. "The returned HSV hue should match the expected value.");
  92. CHECK_MESSAGE(
  93. Math::is_equal_approx(dark_blue.get_s(), 1),
  94. "The returned HSV saturation should match the expected value.");
  95. CHECK_MESSAGE(
  96. Math::is_equal_approx(dark_blue.get_v(), 0.5),
  97. "The returned HSV value should match the expected value.");
  98. }
  99. TEST_CASE("[Color] Conversion methods") {
  100. const Color cyan = Color(0, 1, 1);
  101. const Color cyan_transparent = Color(0, 1, 1, 0);
  102. CHECK_MESSAGE(
  103. cyan.to_html() == "00ffffff",
  104. "The returned RGB HTML color code should match the expected value.");
  105. CHECK_MESSAGE(
  106. cyan_transparent.to_html() == "00ffff00",
  107. "The returned RGBA HTML color code should match the expected value.");
  108. CHECK_MESSAGE(
  109. cyan.to_argb32() == 0xff00ffff,
  110. "The returned 32-bit RGB number should match the expected value.");
  111. CHECK_MESSAGE(
  112. cyan.to_abgr32() == 0xffffff00,
  113. "The returned 32-bit BGR number should match the expected value.");
  114. CHECK_MESSAGE(
  115. cyan.to_rgba32() == 0x00ffffff,
  116. "The returned 32-bit BGR number should match the expected value.");
  117. CHECK_MESSAGE(
  118. cyan.to_argb64() == 0xffff'0000'ffff'ffff,
  119. "The returned 64-bit RGB number should match the expected value.");
  120. CHECK_MESSAGE(
  121. cyan.to_abgr64() == 0xffff'ffff'ffff'0000,
  122. "The returned 64-bit BGR number should match the expected value.");
  123. CHECK_MESSAGE(
  124. cyan.to_rgba64() == 0x0000'ffff'ffff'ffff,
  125. "The returned 64-bit BGR number should match the expected value.");
  126. CHECK_MESSAGE(
  127. String(cyan) == "0, 1, 1, 1",
  128. "The string representation should match the expected value.");
  129. }
  130. TEST_CASE("[Color] Named colors") {
  131. CHECK_MESSAGE(
  132. Color::named("red").is_equal_approx(Color(1, 0, 0)),
  133. "The named color \"red\" should match the expected value.");
  134. // Named colors have their names automatically normalized.
  135. CHECK_MESSAGE(
  136. Color::named("white_smoke").is_equal_approx(Color(0.96, 0.96, 0.96)),
  137. "The named color \"white_smoke\" should match the expected value.");
  138. CHECK_MESSAGE(
  139. Color::named("Slate Blue").is_equal_approx(Color(0.42, 0.35, 0.80)),
  140. "The named color \"Slate Blue\" should match the expected value.");
  141. ERR_PRINT_OFF;
  142. CHECK_MESSAGE(
  143. Color::named("doesn't exist").is_equal_approx(Color()),
  144. "The invalid named color \"doesn't exist\" should result in a Color with the default values.");
  145. ERR_PRINT_ON;
  146. }
  147. TEST_CASE("[Color] Validation methods") {
  148. CHECK_MESSAGE(
  149. Color::html_is_valid("#4080ff"),
  150. "Valid HTML color (with leading #) should be considered valid.");
  151. CHECK_MESSAGE(
  152. Color::html_is_valid("4080ff"),
  153. "Valid HTML color (without leading #) should be considered valid.");
  154. CHECK_MESSAGE(
  155. !Color::html_is_valid("12345"),
  156. "Invalid HTML color should be considered invalid.");
  157. CHECK_MESSAGE(
  158. !Color::html_is_valid("#fuf"),
  159. "Invalid HTML color should be considered invalid.");
  160. }
  161. TEST_CASE("[Color] Manipulation methods") {
  162. const Color blue = Color(0, 0, 1, 0.4);
  163. CHECK_MESSAGE(
  164. blue.inverted().is_equal_approx(Color(1, 1, 0, 0.4)),
  165. "Inverted color should have its red, green and blue components inverted.");
  166. const Color purple = Color(0.5, 0.2, 0.5, 0.25);
  167. CHECK_MESSAGE(
  168. purple.lightened(0.2).is_equal_approx(Color(0.6, 0.36, 0.6, 0.25)),
  169. "Color should be lightened by the expected amount.");
  170. CHECK_MESSAGE(
  171. purple.darkened(0.2).is_equal_approx(Color(0.4, 0.16, 0.4, 0.25)),
  172. "Color should be darkened by the expected amount.");
  173. const Color red = Color(1, 0, 0, 0.2);
  174. const Color yellow = Color(1, 1, 0, 0.8);
  175. CHECK_MESSAGE(
  176. red.lerp(yellow, 0.5).is_equal_approx(Color(1, 0.5, 0, 0.5)),
  177. "Red interpolated with yellow should be orange (with interpolated alpha).");
  178. }
  179. } // namespace TestColor
  180. #endif // TEST_COLOR_H