/******************************************************************/
/* This file is part of the homework assignments for CSCI-427/527 */
/* at The College of William & Mary and authored by Pieter Peers. */
/* No part of this file, whether altered or in original form, can */
/* be distributed or used outside the context of CSCI-427/527 */
/* without consent of either the College of William & Mary or */
/* Pieter Peers. */
/******************************************************************/
#include <cmath>
#include <cassert>
#include <algorithm>
#include "color.h"
//////////////////
// Constructors //
//////////////////
color::color(color::const_reference value)
{
r = g = b = value;
}
color::color(color::const_reference r, color::const_reference g, color::const_reference b)
{
this->r = r;
this->g = g;
this->b = b;
}
color::color(const color& col)
{
r = col.r;
g = col.g;
b = col.b;
}
////////////////
// Inspectors //
////////////////
color::const_reference color::operator[](size_t index) const
{
assert(index < size());
return data[index];
}
color::reference color::operator[](size_t index)
{
assert(index < size());
return data[index];
}
color::iterator color::begin(void)
{
return data;
}
color::const_iterator color::begin(void) const
{
return data;
}
color::iterator color::end(void)
{
return begin() + size();
}
color::const_iterator color::end(void) const
{
return begin() + size();
}
///////////////
// Operators //
///////////////
color& color::operator=(const color& col)
{
_assign(col);
return *this;
}
bool color::operator==(const color& col) const
{
return (r == col.r) && (g == col.g) && (b == col.b);
}
bool color::operator!=(const color& col) const
{
return (r != col.r) || (g != col.g) || (b != col.b);
}
color color::operator+(const color& col) const
{
return color(r + col.r, g + col.g, b + col.b);
}
color color::operator-(const color& col) const
{
return color(r - col.r, g - col.g, b - col.b);
}
color color::operator*(const color& col) const
{
return color(r * col.r, g * col.g, b * col.b);
}
color color::operator*(color::const_reference scale) const
{
return color(r * scale, g * scale, b * scale);
}
color color::operator/(const color& col) const
{
return color(r / col.r, g / col.g, b / col.b);
}
color color::operator/(color::const_reference scale) const
{
return color(r / scale, g / scale, b / scale);
}
color& color::operator+=(const color& col)
{
r += col.r;
g += col.g;
b += col.b;
return *this;
}
color& color::operator-=(const color& col)
{
r -= col.r;
g -= col.g;
b -= col.b;
return *this;
}
color& color::operator*=(const color& col)
{
r *= col.r;
g *= col.g;
b *= col.b;
return *this;
}
color& color::operator*=(color::const_reference scale)
{
r *= scale;
g *= scale;
b *= scale;
return *this;
}
color& color::operator/=(const color& col)
{
r /= col.r;
g /= col.g;
b /= col.b;
return *this;
}
color& color::operator/=(color::const_reference scale)
{
r /= scale;
g /= scale;
b /= scale;
return *this;
}
///////////////
// Modifiers //
///////////////
color& color::abs(void)
{
std::for_each(begin(), end(), [](reference val)
{
if(val<0) val = -val;
});
return *this;
}
color& color::clamp(const_reference lowerBound, const_reference upperBound)
{
std::for_each(begin(), end(), [&](reference val)
{
if(val < lowerBound) val = lowerBound;
else if(val > upperBound) val = upperBound;
});
return *this;
}
/////////////////////
// Private Methods //
/////////////////////
void color::_assign(const color& col)
{
r = col.r;
g = col.g;
b = col.b;
}
void color::_swap(color& col)
{
std::swap(r, col.r);
std::swap(g, col.g);
std::swap(b, col.b);
}