Main Page | File List

RGB.h

Go to the documentation of this file.
00001 /**
00002  *\file         RGB.h
00003  *
00004  *\brief        RGB class wrapping pixel color-triple: red, green, and blue.
00005  *
00006  *              Each rgb component is of a float value in [<b><var>0.0, 1.0</var></b>].
00007  *
00008  */
00009 
00010 
00011 #ifndef _RGB_H
00012 #define _RGB_H
00013 
00014 #include <iostream>
00015 #include <algorithm>
00016 
00017 using namespace std;
00018 
00019 class RGB
00020 {
00021     float rgb[3];
00022       
00023 public:
00024     RGB(float r=0., float g=0., float b=0.)  { rgb[0] = r; rgb[1] = g; rgb[2] = b; }
00025     RGB(float* ptr)                          { copy(ptr, ptr+3, rgb); }
00026       
00027 
00028     float& operator[](int idx)               { return *(rgb+idx); }   //non const method: return lvalue
00029     float operator[](int idx) const          { return *(rgb+idx); }   //const method: return rvalue
00030 
00031     RGB& operator*=(RGB const& rhs)          { for(int i=0; i<3; i++) rgb[i] *= rhs[i]; return *this; }
00032     RGB operator*(RGB const&rhs) const       { RGB ret(*this); return ret *= rhs; }
00033 
00034     RGB& operator*=(float scale)             { for(int i=0; i<3; i++) rgb[i] *= scale; return *this; }
00035     RGB operator*(float scale) const         { RGB ret(*this); return ret *= scale; }
00036 
00037     RGB& operator/=(float scale)             { for(int i=0; i<3; i++) rgb[i] /= scale; return *this; }
00038     RGB operator/(float scale) const         { RGB ret(*this); return ret /= scale; }
00039 
00040     RGB& operator+=(RGB const& rhs)          { for(int i=0; i<3; i++) rgb[i] += rhs[i]; return *this; }
00041     RGB operator+(RGB const& rhs) const      { RGB ret(*this); return ret+=rhs; }
00042 
00043     RGB& operator-=(RGB const& rhs)          { for(int i=0; i<3; i++) rgb[i] -= rhs[i]; return *this; }
00044     RGB operator-(RGB const& rhs) const      { RGB ret(*this); return ret-=rhs; }
00045 
00046     bool operator==(RGB const& rhs) const    { return equal(rgb, rgb+3, rhs.rgb); }
00047     bool operator!=(RGB const& rhs) const    { return ! ( (*this) == rhs ); }
00048 
00049     friend ostream& operator << (ostream& os, RGB const& rgb) { return os << "(" << rgb[0] << ", " << rgb[1] << ", " << rgb[2] << ")"; }
00050 };
00051 
00052   
00053 #endif

Generated on Mon Aug 2 16:51:15 2004 by doxygen 1.3.6