Using Color Operators

[ LiB ]

Using Color Operators

C++ allows you to define custom operators using standard arithmetic operators (+, -, /, *). Let's add this extra functionality to the structure:

 // Accumulative addition of two colors inline color3& color3::operator += ( const  color3& in ) {      r += in.r;      g += in.g;      b += in.b;      return *this; } // Accumulative subtraction of two colors inline color3& color3::operator -= ( const color3& in ) {      r -=  in.r;      g -=  in.g;      b -=  in.b;      return *this; } // Accumulative multiplication of a color by a  scalar inline color3& color3::operator *= (   const  float&  in ) {      r *= in;      g *= in;      b *= in;      return *this;  } // Accumulative division of a color by a scalar inline color3& color3::operator /= ( const float& in ) {       float inv   1.f / n;       r *= inv;       g *= inv;       b *= inv;       return   *this; } // Adds two colors together: ret = a + b inline color3 operator+(color3 const &a, color3 const &b) {       return color3      (            a.r+b.r,            a.g+b.g,            a.b+b.b      ); }; // Subtracts two colors: ret = a - b inline color3 operator-(color3 const &a, color3 const &b) {       return color3       (            a.r-b.r,            a.g-b.g,            a.b-b.b       ); }; // Scales a color by a float: ret = a * b inline color3 operator*(color3 const &a, float const &b) {       return color3       (            a.r*b,            a.g*b,            a.b*b       ); }; // Scales a color by a float: ret = a * b inline color3 operator*(float const &a, color3 const &b)  {     return color3 (                   a*b.g,           a*b.b      ); }; // Divides a color by a float: ret = a / b inline color3 operator/(color3  const &a, float const &b) {      float inv = 1.f / b;      return color3      (           a.r*inv,           a.g*inv,           a.b*inv      ); }; // Color Equality, epsilon used due to numerical imprecision inline bool operator==(color3 const &a, color3 const &b) { if(FastFabs(a.r-b.r)<EPSILON)            if(FastFabs(a.g-b.g)<EPSILON)                 if(FastFabs(a.b-b.b)<EPSILON)                      return true;       return false; }; 

[ LiB ]


Focus On Photon Mapping
Focus On Photon Mapping (Premier Press Game Development)
ISBN: 1592000088
EAN: 2147483647
Year: 2005
Pages: 128
Authors: Marlon John

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net