/////////////////////////////////////////////////////////////////////////
//                                                                     //
// This file contains the declaration and implementation of the        //
// CColor3 class.                                                      //
//                                                                     //
/////////////////////////////////////////////////////////////////////////
#ifndef __CCOLOR3_HPP__
#define __CCOLOR3_HPP__


/////////////////////////////////////////////////////////////////////////
//                                                                     //
// This class is a simple class wrapping a color.                      //
//                                                                     //
/////////////////////////////////////////////////////////////////////////
template <typename T>
class CColor3
{
public:
  CColor3()
    : m_red(0), m_green(0), m_blue(0) {}
  CColor3(T r, T g, T b)
    : m_red(r), m_green(g), m_blue(b) {}

  T     red  ()        const { return m_red; }
  void  red  (T r)           { m_red = r;}
  T     green()        const { return m_green; }
  void  green(T g)           { m_green = g;}
  T     blue ()        const { return m_blue; }
  void  blue (T b)           { m_blue = b;}
  
  inline void clamp()
  {
    if (m_red < 0)
      m_red = 0;
    else if (m_red > 1)
      m_red = 1;

    if (m_green < 0)
      m_green = 0;
    else if (m_green > 1)
      m_green = 1;

    if (m_blue < 0)
      m_blue = 0;
    else if (m_blue > 1)
      m_blue = 1;
  }
  inline CColor3 operator +(const CColor3 &c) const
  {
    return CColor3(m_red + c.m_red, m_green + c.m_green, m_blue + c.m_blue);
  }
  inline CColor3 operator -(const CColor3 &c) const
  {
    return CColor3(m_red - c.m_red, m_green - c.m_green, m_blue - c.m_blue);
  }
  inline CColor3 operator *(const CColor3 &c) const
  {
    return CColor3(m_red * c.m_red, m_green * c.m_green, m_blue * c.m_blue);
  }
  inline CColor3 operator *(T k) const
  {
    return CColor3(k*m_red, k*m_green, k*m_blue);
  }

  inline CColor3 operator += (const CColor3 &c)
  {
    m_red   += c.m_red;
    m_green += c.m_green;
    m_blue  += c.m_blue;
    return (*this);
  }
  inline CColor3 operator -= (const CColor3 &c)
  {
    m_red   -= c.m_red;
    m_green -= c.m_green;
    m_blue  -= c.m_blue;
    return (*this);
  }
  inline CColor3 operator *= (const CColor3 &c)
  {
    m_red   *= c.m_red;
    m_green *= c.m_green;
    m_blue  *= c.m_blue;
    return (*this);
  }
  inline CColor3 operator *= (T k)
  {
    m_red   *= k;
    m_green *= k;
    m_blue  *= k;
    return (*this);
  }


private:
  T m_red;
  T m_green;
  T m_blue;
};

template <typename T>
inline CColor3<T> operator *(T k, const CColor3<T> &c)
{
  return CColor3<T>(k*c.red(), k*c.green(), k*c.blue());
}


#endif  // __CCOLOR3_HPP__
