Core/Color: Add ApproxEqual and make equality exact

This commit is contained in:
SirLynix 2023-09-10 14:43:57 +02:00
parent 20ec1c40fc
commit e1ccb3187d
3 changed files with 22 additions and 8 deletions

View File

@ -9,6 +9,7 @@
#include <NazaraUtils/Prerequisites.hpp> #include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/Math/Vector3.hpp> #include <Nazara/Math/Vector3.hpp>
#include <limits>
#include <string> #include <string>
namespace Nz namespace Nz
@ -25,6 +26,8 @@ namespace Nz
constexpr Color(Color&&) = default; constexpr Color(Color&&) = default;
~Color() = default; ~Color() = default;
constexpr bool ApproxEqual(const Color& color, float maxDifference = std::numeric_limits<float>::epsilon()) const;
constexpr bool IsOpaque() const; constexpr bool IsOpaque() const;
inline std::string ToString() 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;
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<float>::epsilon());
static constexpr Color FromCMY(float cyan, float magenta, float yellow); 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 FromCMYK(float cyan, float magenta, float yellow, float black);
static constexpr Color FromHSL(float hue, float saturation, float lightness); static constexpr Color FromHSL(float hue, float saturation, float lightness);

View File

@ -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 * \brief Return true is the color has no degree of transparency
* \return true if the color has an alpha value of 255 * \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 constexpr bool Color::operator==(const Color& color) const
{ {
return NumberEquals(r, color.r) && return r == color.r &&
NumberEquals(g, color.g) && g == color.g &&
NumberEquals(b, color.b) && b == color.b &&
NumberEquals(a, color.a); a == color.a;
} }
/*! /*!
@ -142,6 +150,11 @@ namespace Nz
// Algorithm coming from http://www.easyrgb.com/index.php?X=MATH // 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 * \brief Converts CMY representation to RGB
* \return Color resulting * \return Color resulting

View File

@ -6,10 +6,7 @@ void CompareColor(const Nz::Color& lhs, const Nz::Color& rhs)
{ {
constexpr float epsilon = 0.1f; constexpr float epsilon = 0.1f;
REQUIRE(lhs.r == Catch::Approx(rhs.r).margin(epsilon)); CHECK(lhs.ApproxEqual(rhs, 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));
} }
constexpr float epsilon = 1.f; constexpr float epsilon = 1.f;