Math.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include <RmlUi/Core/Math.h>
  29. #include <RmlUi/Core/Types.h>
  30. #include <doctest.h>
  31. using namespace Rml;
  32. TEST_CASE("Math")
  33. {
  34. const Colourb c0(0, 0, 0, 255);
  35. const Colourb c1(255, 0, 0, 255);
  36. const Colourb c2(127, 0, 0, 255);
  37. Colourb c;
  38. c = Math::RoundedLerp(0.0f, c0, c1);
  39. REQUIRE(c.red == c0.red);
  40. c = Math::RoundedLerp(179.f / 255.f, c0, c1);
  41. REQUIRE(c.red == 179);
  42. c = Math::RoundedLerp(50.4f / 255.f, c0, c1);
  43. REQUIRE(c.red == 50);
  44. c = Math::RoundedLerp(50.6f / 255.f, c0, c1);
  45. REQUIRE(c.red == 51);
  46. c = Math::RoundedLerp(0.0f, c1, c0);
  47. REQUIRE(c.red == c1.red);
  48. c = Math::RoundedLerp(1.0f, c0, c1);
  49. REQUIRE(c.red == c1.red);
  50. c = Math::RoundedLerp(1.0f, c1, c0);
  51. REQUIRE(c.red == c0.red);
  52. c = Math::RoundedLerp(1.0f, c0, c2);
  53. REQUIRE(c.red == c2.red);
  54. c = Math::RoundedLerp(0.0f, c2, c1);
  55. REQUIRE(c.red == c2.red);
  56. // Full-range interpolation
  57. for (int i = 0; i <= 255; i++)
  58. {
  59. const float t = float(i) / 255.f;
  60. const auto r = Math::RoundedLerp(t, c0, c1).red;
  61. REQUIRE(r == i);
  62. }
  63. // Reverse full-range interpolation
  64. for (int i = 0; i <= 255; i++)
  65. {
  66. const float t = float(i) / 255.f;
  67. const auto r = Math::RoundedLerp(t, c1, c0).red;
  68. REQUIRE(r == 255 - i);
  69. }
  70. // Half-range interpolation
  71. for (int i = 0; i <= 255; i++)
  72. {
  73. const float t = float(i) / 255.f;
  74. const auto r = Math::RoundedLerp(t, c0, c2).red;
  75. REQUIRE(r == i / 2);
  76. }
  77. // Same-color interpolation with fractional t
  78. for (int i = 0; i <= 379; i++)
  79. {
  80. const float t = float(i) / 379.f;
  81. const auto r0 = Math::RoundedLerp(t, c0, c0).red;
  82. const auto r1 = Math::RoundedLerp(t, c1, c1).red;
  83. const auto r2 = Math::RoundedLerp(t, c2, c2).red;
  84. REQUIRE(r0 == c0.red);
  85. REQUIRE(r1 == c1.red);
  86. REQUIRE(r2 == c2.red);
  87. }
  88. }