Added operator - for sf::Color (#114)

This commit is contained in:
Laurent Gomila 2013-08-09 14:57:32 +02:00
parent c697623689
commit 6d4c844959
2 changed files with 56 additions and 8 deletions

View File

@ -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

View File

@ -87,20 +87,30 @@ bool operator !=(const Color& left, const Color& right)
////////////////////////////////////////////////////////////
Color operator +(const Color& left, const Color& right)
{
return Color(static_cast<Uint8>(std::min(left.r + right.r, 255)),
static_cast<Uint8>(std::min(left.g + right.g, 255)),
static_cast<Uint8>(std::min(left.b + right.b, 255)),
static_cast<Uint8>(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<Uint8>(left.r * right.r / 255),
static_cast<Uint8>(left.g * right.g / 255),
static_cast<Uint8>(left.b * right.b / 255),
static_cast<Uint8>(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)
{