From e1ccb3187d629c4d4f0e8337ed8eee1bd2634dce Mon Sep 17 00:00:00 2001 From: SirLynix Date: Sun, 10 Sep 2023 14:43:57 +0200 Subject: [PATCH] Core/Color: Add ApproxEqual and make equality exact --- include/Nazara/Core/Color.hpp | 4 ++++ include/Nazara/Core/Color.inl | 21 +++++++++++++++++---- tests/UnitTests/Engine/Core/ColorTest.cpp | 5 +---- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/include/Nazara/Core/Color.hpp b/include/Nazara/Core/Color.hpp index d88476422..59a08c86c 100644 --- a/include/Nazara/Core/Color.hpp +++ b/include/Nazara/Core/Color.hpp @@ -9,6 +9,7 @@ #include #include +#include #include namespace Nz @@ -25,6 +26,8 @@ namespace Nz constexpr Color(Color&&) = default; ~Color() = default; + constexpr bool ApproxEqual(const Color& color, float maxDifference = std::numeric_limits::epsilon()) const; + constexpr bool IsOpaque() const; inline std::string ToString() const; @@ -41,6 +44,7 @@ namespace Nz constexpr bool operator==(const Color& color) const; constexpr bool operator!=(const Color& color) const; + static constexpr bool ApproxEqual(const Color& lhs, const Color& rhs, float maxDifference = std::numeric_limits::epsilon()); static constexpr Color FromCMY(float cyan, float magenta, float yellow); static constexpr Color FromCMYK(float cyan, float magenta, float yellow, float black); static constexpr Color FromHSL(float hue, float saturation, float lightness); diff --git a/include/Nazara/Core/Color.inl b/include/Nazara/Core/Color.inl index 952b4abdd..24bea998b 100644 --- a/include/Nazara/Core/Color.inl +++ b/include/Nazara/Core/Color.inl @@ -44,6 +44,14 @@ namespace Nz { } + constexpr bool Color::ApproxEqual(const Color& color, float maxDifference) const + { + return NumberEquals(r, color.r, maxDifference) && + NumberEquals(g, color.g, maxDifference) && + NumberEquals(b, color.b, maxDifference) && + NumberEquals(a, color.a, maxDifference); + } + /*! * \brief Return true is the color has no degree of transparency * \return true if the color has an alpha value of 255 @@ -123,10 +131,10 @@ namespace Nz */ constexpr bool Color::operator==(const Color& color) const { - return NumberEquals(r, color.r) && - NumberEquals(g, color.g) && - NumberEquals(b, color.b) && - NumberEquals(a, color.a); + return r == color.r && + g == color.g && + b == color.b && + a == color.a; } /*! @@ -142,6 +150,11 @@ namespace Nz // Algorithm coming from http://www.easyrgb.com/index.php?X=MATH + constexpr bool Color::ApproxEqual(const Color& lhs, const Color& rhs, float maxDifference) + { + return lhs.ApproxEqual(rhs, maxDifference); + } + /*! * \brief Converts CMY representation to RGB * \return Color resulting diff --git a/tests/UnitTests/Engine/Core/ColorTest.cpp b/tests/UnitTests/Engine/Core/ColorTest.cpp index f0070261a..ba2ea775f 100644 --- a/tests/UnitTests/Engine/Core/ColorTest.cpp +++ b/tests/UnitTests/Engine/Core/ColorTest.cpp @@ -6,10 +6,7 @@ void CompareColor(const Nz::Color& lhs, const Nz::Color& rhs) { constexpr float epsilon = 0.1f; - REQUIRE(lhs.r == Catch::Approx(rhs.r).margin(epsilon)); - REQUIRE(lhs.g == Catch::Approx(rhs.g).margin(epsilon)); - REQUIRE(lhs.b == Catch::Approx(rhs.b).margin(epsilon)); - REQUIRE(lhs.a == Catch::Approx(rhs.a).margin(epsilon)); + CHECK(lhs.ApproxEqual(rhs, epsilon)); } constexpr float epsilon = 1.f;