vee_color.h

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 
00003 /* COPYRIGHT
00004  *
00005  * This file is part of the Visual Effects Engine - VEE
00006  *
00007  * Read the "VEE-LICENSE" file for the license.
00008  *
00009  * Authors & Copyright:   
00010  *
00011  * Tommi Ilmonen, Tuukka Heikura, Marko Myllymaa and 
00012  * Janne Kontkanen 2001-2004
00013  *
00014  * Additional copyrights: Tekes 2003-2004
00015  *
00016  * firstname.lastname@hut.fi
00017  *
00018  */
00019 
00020 #ifndef VEE_COLOR_H
00021 #define VEE_COLOR_H
00022 
00062 class VEE_Rgba
00063 {
00064 public:
00065 
00067   VEE_Rgba() {}
00068 
00069   VEE_Rgba(float r, float g, float b, float a = 1.0, float t = 0.5)
00070     : m_r(r), m_g(g), m_b(b), m_a(a), m_t(t) {}
00071 
00072   ~VEE_Rgba() {}
00073   
00074   void set(float r, float g, float b, float a = 1.0, float t = 0.5)
00075   { m_r = r; m_g = g; m_b = b; m_a = a; m_t = t; }
00076 
00077   template <class S>
00078   void copy(const S * c, int n) 
00079   { for(int i=0; i < n; i++) data()[i] = c[i]; }
00080 
00081   float red()   const { return m_r; }
00082   float green() const { return m_g; }
00083   float blue()  const { return m_b; }
00084   float alpha() const { return m_a; }
00085   float theta() const { return m_t; }
00086 
00087   unsigned getARGB() const;
00088 
00089   void setRed(float r) { m_r = r; }
00090   void setGreen(float g) { m_g = g; }
00091   void setBlue(float b) { m_b = b; }
00092   void setAlpha(float a) { m_a = a; }
00093   void setTheta(float t) { m_t = t; }
00094 
00096   float *data() { return & m_r; } 
00097   const float *data() const { return & m_r; } 
00098 
00103   void clampNth(unsigned index, float minVal, float maxVal)
00104   {
00105     float &v = data()[index];
00106 
00107     if(v < minVal) v = minVal;
00108     else if(v > maxVal) v = maxVal;
00109   }
00110 
00111   void clampRgb(float minVal, float maxVal);
00112   void clampRgba(float minVal, float maxVal);
00113 
00116   VEE_Rgba farColor() const
00117   {
00118     VEE_Rgba c = *this;
00119     for(int i = 0; i < 3; i++)
00120       c[i] = (c[i] > 0.5f) ? 0.0f : 1.0f;
00121     return c;
00122   }
00123   
00125   void normalize()
00126   { clampRgba(0.0, 1.0); }
00127 
00130   VEE_Rgba mulRgba (float mul) const
00131   { return VEE_Rgba(m_r * mul, m_g * mul, m_b * mul, m_a * mul, m_t); }
00132 
00135   VEE_Rgba mulRgb (float mul) const
00136   { return VEE_Rgba(m_r * mul, m_g * mul, m_b * mul, m_a, m_t); }
00137 
00138   VEE_Rgba addRgba (const VEE_Rgba& rgb) const
00139   { return VEE_Rgba(m_r + rgb.m_r, m_g + rgb.m_g, m_b + rgb.m_b, 
00140         m_a + rgb.m_a); }
00141 
00144   VEE_Rgba operator * (float mul) const
00145   { return VEE_Rgba(m_r * mul, m_g * mul, m_b * mul, m_a * mul, m_t * mul); }
00146 
00149   VEE_Rgba operator + (const VEE_Rgba& rgb) const
00150   { return VEE_Rgba(m_r + rgb.m_r, m_g + rgb.m_g, m_b + rgb.m_b, 
00151         m_a + rgb.m_a, m_t + rgb.m_t); }
00152   
00155   VEE_Rgba operator - (const VEE_Rgba& rgb) const
00156   { return VEE_Rgba(m_r - rgb.m_r, m_g - rgb.m_g, m_b - rgb.m_b, 
00157         m_a - rgb.m_a, m_t - rgb.m_t); }
00158   
00159   float & operator [] (int n) { return (&m_r)[n]; }
00160   const float & operator [] (int n) const { return (&m_r)[n]; }
00161 
00162   static const VEE_Rgba White;
00163   static const VEE_Rgba Gray;
00164   static const VEE_Rgba Black;
00165   static const VEE_Rgba BlackTransparent;
00166 
00167   static const VEE_Rgba Red;
00168   static const VEE_Rgba Green;
00169   static const VEE_Rgba Blue;
00170 
00171   static const VEE_Rgba Cyan;
00172   static const VEE_Rgba Magenta;
00173   static const VEE_Rgba Yellow;
00174 
00175 protected:
00176 
00177   float m_r, m_g, m_b, m_a, m_t;
00178 };
00179 
00182 
00183 class VEE_Hsva
00184 {
00185 public:
00186   VEE_Hsva(float h = 0.0, float s = 0.0, float v = 0.0, 
00187      float a = 1.0, float t = 0.5)
00188     : m_h(h), m_s(s), m_v(v), m_a(a), m_t(t) {}
00189 
00190   ~VEE_Hsva() {}
00191 
00192   float hue() const { return m_h; }
00193   float saturation() const { return m_s; }
00194   float value() const { return m_v; }
00195   float alpha() const { return m_a; }
00196   float theta() const { return m_t; }
00197 
00198   void setHue(float h) { m_h = h; }
00199   void setSaturation(float s) { m_s = s; }
00200   void setValue(float v) { m_v = v; }
00201   void setAlpha(float a) { m_a = a; }
00202   void setTheta(float t) { m_t = t; }
00203 
00204   void set(float h, float s, float v, float a = 1.0, float t = 0.5)
00205   { m_h = h; m_s = s; m_v = v; m_a = a; m_t = t; }
00206 
00207   inline VEE_Rgba rgba() const;
00208 
00209   float * data() { return & m_h; }
00210   const float * data() const { return & m_h; }
00211 
00212   void normalize(int c) 
00213   { 
00214     float tmp = data()[c]; 
00215     if(tmp < 0.0) tmp = 0.0; 
00216     else if(tmp > 1.0) tmp = 1.0; 
00217     data()[c] = tmp;
00218   }
00219   void normalize() 
00220   { 
00221     for(int i = 0; i < 5; i++) 
00222       normalize(i); 
00223   }
00224 
00225   VEE_Rgba operator * (float mul) const
00226   { return VEE_Rgba(m_h * mul, m_s * mul, m_v * mul, m_a * mul, m_t * mul); }
00227 
00228   VEE_Hsva & operator = (const VEE_Rgba &);
00229 
00230   static const VEE_Hsva Gray;
00231   static const VEE_Hsva White;
00232   static const VEE_Hsva Black;
00233 
00234   static const VEE_Hsva Red;
00235   static const VEE_Hsva Green;
00236   static const VEE_Hsva Blue;
00237 
00238   static const VEE_Hsva Cyan;
00239   static const VEE_Hsva Magenta;
00240   static const VEE_Hsva Yellow;
00241 
00242 protected:
00243   float m_h, m_s, m_v, m_a, m_t;
00244 };
00245 
00246 inline VEE_Rgba VEE_Hsva::rgba() const
00247 {
00248   // void HSVtoRGB( float *r, float *g, float *b, float h, float s, float v )
00249   
00250   int i;
00251   float f, p, q, t;
00252 
00253   float h = m_h, s = m_s, v = m_v;
00254   float r, g, b;
00255   
00256   if( s == 0 ) {
00257     // achromatic (grey)
00258     return VEE_Rgba(v, v, v, m_a);
00259   }
00260 
00261   h *= 6.0f;                        // sector 0 to 5
00262   i = (int) h; // floor(h);
00263   f = h - i;                      // factorial part of h
00264   p = v * ( 1 - s );
00265   q = v * ( 1 - s * f );
00266   t = v * ( 1 - s * ( 1 - f ) );
00267   switch( i ) {
00268   case 0:
00269     r = v;
00270     g = t;
00271     b = p;
00272     break;
00273   case 1:
00274     r = q;
00275     g = v;
00276     b = p;
00277     break;
00278   case 2:
00279     r = p;
00280     g = v;
00281     b = t;
00282     break;
00283   case 3:
00284     r = p;
00285     g = q;
00286     b = v;
00287     break;
00288   case 4:
00289     r = t;
00290     g = p;
00291     b = v;
00292     break;
00293   case 5:
00294     r = v;
00295     g = p;
00296     b = q;
00297     break;
00298   default: // = case 0:
00299     r = v;
00300     g = t;
00301     b = p;
00302     break;    
00303   }
00304   
00305   return VEE_Rgba(r, g, b, m_a, m_t);
00306 }
00307 
00308 #endif

Generated on Mon Mar 12 21:09:00 2007 for VEE - The Visual Effects Engine by  doxygen 1.4.6