From dea19673443ae53ae9f7c0546b21a9c26f7d21b4 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 13 Sep 2013 18:23:25 -0700 Subject: [PATCH] Add operator overloads for Color taking Color references --- src/basics.cc | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/basics.h | 12 +++++++++ 2 files changed, 87 insertions(+) diff --git a/src/basics.cc b/src/basics.cc index 7f8b114..e35a48f 100644 --- a/src/basics.cc +++ b/src/basics.cc @@ -391,3 +391,78 @@ Color::operator=(const Color &rhs) alpha = rhs.alpha; return *this; } + + +Color & +Color::operator*=(const Color &rhs) +{ + red *= rhs.red; + green *= rhs.green; + blue *= rhs.blue; + return *this; +} + +Color & +Color::operator/=(const Color &rhs) +{ + red *= (1.0 / rhs.red); + green *= (1.0 / rhs.green); + blue *= (1.0 / rhs.blue); + return *this; +} + +Color & +Color::operator+=(const Color &rhs) +{ + red += rhs.red; + green += rhs.green; + blue += rhs.blue; + alpha += rhs.alpha; + return *this; +} + +Color & +Color::operator-=(const Color &rhs) +{ + red -= rhs.red; + green -= rhs.green; + blue -= rhs.blue; + alpha -= rhs.alpha; + return *this; +} + + +Color +Color::operator*(const Color &rhs) + const +{ + return Color(*this) *= rhs; +} + +Color +Color::operator/(const Color &rhs) + const +{ + return Color(*this) /= rhs; +} + +Color +Color::operator+(const Color &rhs) + const +{ + return Color(*this) += rhs; +} + +Color +Color::operator-(const Color &rhs) + const +{ + return Color(*this) -= rhs; +} + + +const Color +operator*(const float &lhs, const Color &rhs) +{ + return rhs * lhs; +} diff --git a/src/basics.h b/src/basics.h index 38bdbeb..b323f6c 100644 --- a/src/basics.h +++ b/src/basics.h @@ -72,6 +72,16 @@ struct Color Color &operator=(const Color &rhs); + // These operators blend the two colors. + Color &operator*=(const Color &rhs); + Color &operator/=(const Color &rhs); + Color &operator+=(const Color &rhs); + Color &operator-=(const Color &rhs); + Color operator*(const Color &rhs) const; + Color operator/(const Color &rhs) const; + Color operator+(const Color &rhs) const; + Color operator-(const Color &rhs) const; + static const Color Black; static const Color White; static const Color Red; @@ -81,4 +91,6 @@ struct Color float red, green, blue, alpha; }; +const Color operator*(const float &lhs, const Color &rhs); + #endif