CColor.h

Go to the documentation of this file.
00001 
00012 #ifndef CCOLOR_H
00013 #define CCOLOR_H
00014 
00015 
00021 class CColor
00022 {
00023  public:
00027   CColor(float r = 0, float g = 0, float b = 0, float a = 0)
00028     {
00029       red   = r;
00030       green = g;
00031       blue  = b;
00032       alpha = a;
00033     }
00034 
00039   float& operator[] (int i)
00040     {
00041       switch (i)
00042         {
00043         case 0: return red;
00044         case 1: return green;
00045         case 2: return blue;
00046         case 3: return alpha;
00047         default: POP_ILLEGAL("Invalid color index."); return red;
00048         }
00049     }
00050 
00055   float operator[] (int i) const
00056     {
00057       switch (i)
00058         {
00059         case 0: return red;
00060         case 1: return green;
00061         case 2: return blue;
00062         case 3: return alpha;
00063         default: POP_ILLEGAL("Invalid color index."); return red;
00064         }
00065     }
00066 
00071   void Clamp(void)
00072     {
00073       if (red < 0)
00074         red = 0;
00075       else if (red > 1)
00076         red = 1;
00077 
00078       if (green < 0)
00079         green = 0;
00080       else if (green > 1)
00081         green = 1;
00082 
00083       if (blue < 0)
00084         blue = 0;
00085       else if (blue > 1)
00086         blue = 1;
00087 
00088       if (alpha < 0)
00089         alpha = 0;
00090       else if (alpha > 1)
00091         alpha = 1;
00092     }
00093 
00099   inline CColor operator +(const CColor &c) const
00100     {
00101       return CColor(red + c.red, green + c.green,
00102                     blue + c.blue, alpha + c.alpha);
00103     }
00104 
00110   inline CColor operator *(const CColor &c) const
00111     {
00112       return CColor(red * c.red, green * c.green,
00113                     blue * c.blue, alpha * c.alpha);
00114     }
00115 
00121   inline CColor operator *(float k) const
00122     {
00123       return CColor(k*red, k*green, k*blue, k*alpha);
00124     }
00125 
00126   // Same operator, but with the operands switched
00127   friend inline CColor operator *(float k, const CColor &c);
00128 
00129 
00133   inline CColor operator += (const CColor &c)
00134     {
00135       red   += c.red;
00136       green += c.green;
00137       blue  += c.blue;
00138       alpha += c.alpha;
00139       return (*this);
00140     }
00141 
00145   inline CColor operator -= (const CColor &c)
00146     {
00147       red   -= c.red;
00148       green -= c.green;
00149       blue  -= c.blue;
00150       alpha -= c.alpha;
00151       return (*this);
00152     }
00153 
00157   inline CColor operator *= (const CColor &c)
00158     {
00159       red   *= c.red;
00160       green *= c.green;
00161       blue  *= c.blue;
00162       alpha *= c.alpha;
00163       return (*this);
00164     }
00165 
00169   inline CColor operator *= (float k)
00170     {
00171       red   *= k;
00172       green *= k;
00173       blue  *= k;
00174       alpha *= k;
00175       return (*this);
00176     }
00177 
00178   friend inline std::ostream& operator <<(std::ostream& o,
00179                                           const CColor &c);
00180   
00181  private:
00182   float red;   
00183   float green; 
00184   float blue;  
00185   float alpha; 
00186 };
00187 
00193 inline CColor operator *(float k, const CColor &c)
00194 {
00195   return CColor(k*c.red, k*c.green, k*c.blue, k*c.alpha);
00196 }
00197 
00201 inline std::ostream& operator << (std::ostream& o, const CColor &c)
00202 {
00203   o << "( " << c.red << " , " << c.green 
00204     << " , " << c.blue << " , " << c.alpha << " )";
00205   return o;
00206 }
00207 
00208 
00209 #endif  // CCOLOR_H
00210 

Generated on Fri Dec 5 03:20:33 2008 for Mathematical Ray-tracer by  doxygen 1.5.4