From da87ac8dd2db785891b52fb9fb7739ab114bb49a Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 12 Sep 2013 15:51:08 -0700 Subject: [PATCH] Add operator overloads for Colors and floats --- src/basics.cc | 97 +++++++++++++++++++++++++++++++++++++++++++++------ src/basics.h | 12 +++++-- 2 files changed, 97 insertions(+), 12 deletions(-) diff --git a/src/basics.cc b/src/basics.cc index b9b8396..56f0a2e 100644 --- a/src/basics.cc +++ b/src/basics.cc @@ -37,6 +37,11 @@ Vector3::Vector3(float _x, float _y, float _z) { } +/* + * Vector3::operator= -- + * + * Copy the given vector's values into this vector. Return a reference to this vector. + */ Vector3 & Vector3::operator=(const Vector3 &v) { @@ -87,7 +92,6 @@ Vector3::operator-=(const Vector3 &rhs) } - /* * Vector3::operator* -- * Vector3::operator/ -- @@ -264,22 +268,95 @@ Color::Color(float r, float g, float b, float a) { } +/* + * Color::operator*= -- + * Color::operator/= -- + * Color::operator+= -- + * Color::operator-= -- + * + * Perform the corresponding arithmetic operation on this color and the given scalar. These methods are destructive and + * a reference to this color is returned. + */ Color & -Color::operator=(const Color &c) +Color::operator*=(const float &rhs) { - red = c.red; - green = c.green; - blue = c.blue; - alpha = c.alpha; + red *= rhs; + green *= rhs; + blue *= rhs; return *this; } +Color & +Color::operator/=(const float &rhs) +{ + return *this *= (1.0 / rhs); +} Color & -Color::operator*=(const float f) +Color::operator+=(const float &rhs) { - red *= f; - green *= f; - blue *= f; + red += rhs; + green += rhs; + blue += rhs; + alpha += rhs; + return *this; +} + +Color & +Color::operator-=(const float &rhs) +{ + return *this += -rhs; +} + + +/* + * Color::operator* -- + * Color::operator/ -- + * Color::operator+ -- + * Color::operator- -- + * + * Perform the corresponding operation on a copy of this color and the given scalar. Return a new vector. + */ +Color +Color::operator*(const float &rhs) + const +{ + return Color(*this) *= rhs; +} + +Color +Color::operator/(const float &rhs) + const +{ + return Color(*this) /= rhs; +} + +Color +Color::operator+(const float &rhs) + const +{ + return Color(*this) += rhs; +} + +Color +Color::operator-(const float &rhs) + const +{ + return Color(*this) -= rhs; +} + + +/* + * Color::operator= -- + * + * Copy the given color's values into this color. Return a reference to this color. + */ +Color & +Color::operator=(const Color &rhs) +{ + red = rhs.red; + green = rhs.green; + blue = rhs.blue; + alpha = rhs.alpha; return *this; } diff --git a/src/basics.h b/src/basics.h index 6f2fc9d..cb26e16 100644 --- a/src/basics.h +++ b/src/basics.h @@ -57,8 +57,16 @@ struct Color Color(); Color(float r, float g, float b, float a); - Color &operator=(const Color &c); - Color &operator*=(const float c); + Color &operator*=(const float &rhs); + Color &operator/=(const float &rhs); + Color &operator+=(const float &rhs); + Color &operator-=(const float &rhs); + Color operator*(const float &rhs) const; + Color operator/(const float &rhs) const; + Color operator+(const float &rhs) const; + Color operator-(const float &rhs) const; + + Color &operator=(const Color &rhs); static const Color Black; static const Color White;