diff --git a/include/SFML/Graphics/Color.hpp b/include/SFML/Graphics/Color.hpp index fb2aa2dca..6bc570b07 100644 --- a/include/SFML/Graphics/Color.hpp +++ b/include/SFML/Graphics/Color.hpp @@ -126,6 +126,21 @@ SFML_GRAPHICS_API bool operator !=(const Color& left, const Color& right); //////////////////////////////////////////////////////////// SFML_GRAPHICS_API Color operator +(const Color& left, const Color& right); +//////////////////////////////////////////////////////////// +/// \relates Color +/// \brief Overload of the binary - operator +/// +/// This operator returns the component-wise subtraction of two colors. +/// Components below 0 are clamped to 0. +/// +/// \param left Left operand +/// \param right Right operand +/// +/// \return Result of \a left - \a right +/// +//////////////////////////////////////////////////////////// +SFML_GRAPHICS_API Color operator -(const Color& left, const Color& right); + //////////////////////////////////////////////////////////// /// \relates Color /// \brief Overload of the binary * operator @@ -159,6 +174,22 @@ SFML_GRAPHICS_API Color operator *(const Color& left, const Color& right); //////////////////////////////////////////////////////////// SFML_GRAPHICS_API Color& operator +=(Color& left, const Color& right); +//////////////////////////////////////////////////////////// +/// \relates Color +/// \brief Overload of the binary -= operator +/// +/// This operator computes the component-wise subtraction of two colors, +/// and assigns the result to the left operand. +/// Components below 0 are clamped to 0. +/// +/// \param left Left operand +/// \param right Right operand +/// +/// \return Reference to \a left +/// +//////////////////////////////////////////////////////////// +SFML_GRAPHICS_API Color& operator -=(Color& left, const Color& right); + //////////////////////////////////////////////////////////// /// \relates Color /// \brief Overload of the binary *= operator diff --git a/src/SFML/Graphics/Color.cpp b/src/SFML/Graphics/Color.cpp index d016a9d47..2a6f05eb7 100644 --- a/src/SFML/Graphics/Color.cpp +++ b/src/SFML/Graphics/Color.cpp @@ -87,20 +87,30 @@ bool operator !=(const Color& left, const Color& right) //////////////////////////////////////////////////////////// Color operator +(const Color& left, const Color& right) { - return Color(static_cast(std::min(left.r + right.r, 255)), - static_cast(std::min(left.g + right.g, 255)), - static_cast(std::min(left.b + right.b, 255)), - static_cast(std::min(left.a + right.a, 255))); + return Color(Uint8(std::min(int(left.r) + right.r, 255)), + Uint8(std::min(int(left.g) + right.g, 255)), + Uint8(std::min(int(left.b) + right.b, 255)), + Uint8(std::min(int(left.a) + right.a, 255))); +} + + +//////////////////////////////////////////////////////////// +Color operator -(const Color& left, const Color& right) +{ + return Color(Uint8(std::max(int(left.r) - right.r, 0)), + Uint8(std::max(int(left.g) - right.g, 0)), + Uint8(std::max(int(left.b) - right.b, 0)), + Uint8(std::max(int(left.a) - right.a, 0))); } //////////////////////////////////////////////////////////// Color operator *(const Color& left, const Color& right) { - return Color(static_cast(left.r * right.r / 255), - static_cast(left.g * right.g / 255), - static_cast(left.b * right.b / 255), - static_cast(left.a * right.a / 255)); + return Color(Uint8(int(left.r) * right.r / 255), + Uint8(int(left.g) * right.g / 255), + Uint8(int(left.b) * right.b / 255), + Uint8(int(left.a) * right.a / 255)); } @@ -111,6 +121,13 @@ Color& operator +=(Color& left, const Color& right) } +//////////////////////////////////////////////////////////// +Color& operator -=(Color& left, const Color& right) +{ + return left = left - right; +} + + //////////////////////////////////////////////////////////// Color& operator *=(Color& left, const Color& right) {