diff --git a/src/basics/SConscript b/src/basics/SConscript index 2014565..0f47e1c 100644 --- a/src/basics/SConscript +++ b/src/basics/SConscript @@ -6,6 +6,7 @@ Import('env') files = [ + 'color.cc', 'matrix.cc', 'ray.cc', 'vector.cc', diff --git a/src/basics/basics.hh b/src/basics/basics.hh index fb956cd..ba66078 100644 --- a/src/basics/basics.hh +++ b/src/basics/basics.hh @@ -9,6 +9,7 @@ #ifndef __BASICS_BASICS_HH__ #define __BASICS_BASICS_HH__ +#include "basics/color.hh" #include "basics/matrix.hh" #include "basics/ray.hh" #include "basics/types.hh" diff --git a/src/basics/color.cc b/src/basics/color.cc new file mode 100644 index 0000000..a1dcb2f --- /dev/null +++ b/src/basics/color.cc @@ -0,0 +1,214 @@ +/* color.cc + * vim: set tw=80: + * Eryn Wells + */ + +#include + + +namespace charles { +namespace basics { + +/* + * Color::Color -- + */ +Color::Color() + : Color(0.0, 0.0, 0.0, 0.0) +{ } + + +/* + * Color::Color -- + */ +Color::Color(const Double& r, + const Double& g, + const Double& b, + const Double& a) + : red(r), + green(g), + blue(b), + alpha(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 Double &rhs) +{ + red *= rhs; + green *= rhs; + blue *= rhs; + return *this; +} + +Color & +Color::operator/=(const Double &rhs) +{ + return *this *= (1.0 / rhs); +} + +Color & +Color::operator+=(const Double &rhs) +{ + red += rhs; + green += rhs; + blue += rhs; + alpha += rhs; + return *this; +} + +Color & +Color::operator-=(const Double &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 Double &rhs) + const +{ + return Color(*this) *= rhs; +} + +Color +Color::operator/(const Double &rhs) + const +{ + return Color(*this) /= rhs; +} + +Color +Color::operator+(const Double &rhs) + const +{ + return Color(*this) += rhs; +} + +Color +Color::operator-(const Double &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; +} + + +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 Double& lhs, + const Color& rhs) +{ + return rhs * lhs; +} + + +std::ostream & +operator<<(std::ostream& os, + const Color& c) +{ + // Stream colors like this: + os << "<" << c.red << ", " << c.green << ", " << c.blue << ", " << c.alpha << ">"; + return os; +} + +} /* namespace basics */ +} /* namespace charles */ diff --git a/src/basics/color.hh b/src/basics/color.hh new file mode 100644 index 0000000..ed4d0c2 --- /dev/null +++ b/src/basics/color.hh @@ -0,0 +1,54 @@ +/* color.hh + * vim: set tw=80: + * Eryn Wells + */ + +#include "basics/types.hh" + + +namespace charles { +namespace basics { + +struct Color +{ + Color(); + Color(const Double& r, const Double& g, const Double& b, const Double& a = 1.0); + + Color &operator*=(const Double &rhs); + Color &operator/=(const Double &rhs); + Color &operator+=(const Double &rhs); + Color &operator-=(const Double &rhs); + Color operator*(const Double &rhs) const; + Color operator/(const Double &rhs) const; + Color operator+(const Double &rhs) const; + Color operator-(const Double &rhs) const; + + 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; + static const Color Green; + static const Color Blue; + + Double red, green, blue, alpha; +}; + + +const Color operator*(const Double &lhs, const Color &rhs); + +std::ostream &operator<<(std::ostream &os, const Color &c); + +} /* namespace basics */ +} /* namespace charles */ + diff --git a/src/light.h b/src/light.hh similarity index 100% rename from src/light.h rename to src/light.hh