Move Color over to basics

This commit is contained in:
Eryn Wells 2014-08-09 19:30:47 -07:00
parent 47ae52ed05
commit 4b396cabe8
5 changed files with 270 additions and 0 deletions

View file

@ -6,6 +6,7 @@ Import('env')
files = [
'color.cc',
'matrix.cc',
'ray.cc',
'vector.cc',

View file

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

214
src/basics/color.cc Normal file
View file

@ -0,0 +1,214 @@
/* color.cc
* vim: set tw=80:
* Eryn Wells <eryn@erynwells.me>
*/
#include <ostream>
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: <r, g, b, a>
os << "<" << c.red << ", " << c.green << ", " << c.blue << ", " << c.alpha << ">";
return os;
}
} /* namespace basics */
} /* namespace charles */

54
src/basics/color.hh Normal file
View file

@ -0,0 +1,54 @@
/* color.hh
* vim: set tw=80:
* Eryn Wells <eryn@erynwells.me>
*/
#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 */