color.h

Go to the documentation of this file.
00001 #ifndef __UTIL__COLOUR_H__
00002 #define __UTIL__COLOUR_H__
00003 
00009 #include <config.h>
00010 #include <util/vector.h>
00011 #include <util/clamp.h>
00012 #include <QtGui/QColor>
00013 
00014 namespace util {
00019   template<class T> class Color : public Vector<4, T> {
00020   public:
00021 
00025     template <typename T1>
00026     explicit Color(const Vector<4,T1>& color, const T& scale = 1)
00027       : Vector<4,T>(color[0]*scale, color[1]*scale, color[2]*scale, color[3]*scale)
00028     { }
00029 
00030     template <typename T1>
00031     explicit Color(const Vector<4,T1>& color, const T1& scale)
00032       : Vector<4,T>(color[0]*scale, color[1]*scale, color[2]*scale, color[3]*scale)
00033     { }
00034 
00035     Color(const Vector<4,T>& copy)
00036       : Vector<4,T>(copy)
00037     { }
00038 
00039     Color(const QColor& c)
00040     {
00041       convertFromQColor(*this, c);
00042     }
00043 
00044 #ifdef USE_CXX0X
00045     Color(Color&& copy) = default;
00046 #endif
00047 
00055     Color(const T& r = T(), const T& g = T(), const T& b = T(), const T& a = T())
00056       : Vector<4,T>(r,g,b,a) { }
00057 
00061     T& r() {return this->x();}
00062 
00066     T& g() {return this->y();}
00067 
00071     T& b() {return this->z();}
00072 
00076     T& a() {return this->t();}
00077 
00078 
00082     const T& r() const {return this->x();}
00083 
00087     const T& g() const {return this->y();}
00088 
00092     const T& b() const {return this->z();}
00093 
00097     const T& a() const {return this->t();}
00098 
00099 
00103     void r(const T& val) {this->x(val);}
00104 
00108     void g(const T& val) {this->y(val);}
00109 
00113     void b(const T& val) {this->z(val);}
00114 
00118     void a(const T& val) {this->t(val);}
00119 
00120     Color& operator=(const Color& c);
00121     Color& operator=(const Vector<4,T>& c);
00122     Color& operator=(const T& val);
00123 
00124 
00125 #ifdef USE_CXX0X
00126     Color& operator=(Color&& copy) = default;
00127     Color& operator=(Vector<4,T>&& c);
00128 #endif
00129 
00130 
00131     operator QColor() const
00132     {
00133       return convertToQColor(*this);
00134     }
00135   };
00136 
00137   QColor convertToQColor(const Color<float>& c);
00138   QColor convertToQColor(const Color<double>& c);
00139   QColor convertToQColor(const Color<long double>& c);
00140   QColor convertToQColor(const Color<unsigned char>& c);
00141   QColor convertToQColor(const Color<unsigned short>& c);
00142   QColor convertToQColor(const Color<unsigned int>& c);
00143   QColor convertToQColor(const Color<unsigned long>& c);
00144   QColor convertToQColor(const Color<unsigned long long>& c);
00145   QColor convertToQColor(const Color<char>& c);
00146   QColor convertToQColor(const Color<short>& c);
00147   QColor convertToQColor(const Color<int>& c);
00148   QColor convertToQColor(const Color<long>& c);
00149   QColor convertToQColor(const Color<long long>& c);
00150 
00151   void convertFromQColor(Color<float>& c, const QColor& col);
00152   void convertFromQColor(Color<double>& c, const QColor& col);
00153   void convertFromQColor(Color<long double>& c, const QColor& col);
00154   void convertFromQColor(Color<unsigned char>& c, const QColor& col);
00155   void convertFromQColor(Color<unsigned short>& c, const QColor& col);
00156   void convertFromQColor(Color<unsigned int>& c, const QColor& col);
00157   void convertFromQColor(Color<unsigned long>& c, const QColor& col);
00158   void convertFromQColor(Color<unsigned long long>& c, const QColor& col);
00159   void convertFromQColor(Color<char>& c, const QColor& col);
00160   void convertFromQColor(Color<short>& c, const QColor& col);
00161   void convertFromQColor(Color<int>& c, const QColor& col);
00162   void convertFromQColor(Color<long>& c, const QColor& col);
00163   void convertFromQColor(Color<long long>& c, const QColor& col);
00164 
00166   template <class T>
00167     Color<T>& Color<T>::operator=(const Color<T>& c) {
00168       this->Vector<4,T>::operator=(c);
00169       return *this;
00170     }
00171 
00172   template <class T>
00173     Color<T>& Color<T>::operator=(const T& val) {
00174       this->Vector<4,T>::operator=(val);
00175       return *this;
00176     }
00177 
00178   template <class T>
00179     Color<T>& Color<T>::operator=(const Vector<4,T>& c) {
00180       this->Vector<4,T>::operator=(c);
00181       return *this;
00182     }
00183 
00184 #ifdef USE_CXX0X
00185   template <class T>
00186     Color<T>& Color<T>::operator=(Vector<4,T>&& c) {
00187       this->Vector<4,T>::operator=(std::move(c));
00188       return *this;
00189     }
00190 #endif
00191 
00193   template <class T>
00194   Color<T> convertHSVtoRGB(T h, T s, T v) {
00195     // based on Jo's code in medit
00196 
00197     Color<T> rgb;
00198     rgb.a(1.0);
00199 
00200     while (h > 360.0) h -= 360.0;
00201     while (h < 0.0) h += 360.0;
00202 
00203     h /= 60.0;
00204 
00205     int i = int(h);
00206 
00207     double f = h - i;
00208     double p = v * (1 - s);
00209     double q = v * (1 - (s * f));
00210     double t = v * (1 - (s * (1 - f)));
00211 
00212     switch (i) {
00213     case 0: rgb.r(v); rgb.g(t); rgb.b(p); break;
00214     case 1: rgb.r(q); rgb.g(v); rgb.b(p); break;
00215     case 2: rgb.r(p); rgb.g(v); rgb.b(t); break;
00216     case 3: rgb.r(p); rgb.g(q); rgb.b(v); break;
00217     case 4: rgb.r(t); rgb.g(p); rgb.b(v); break;
00218     case 5: rgb.r(v); rgb.g(p); rgb.b(q); break;
00219     }
00220 
00221     return rgb;
00222   }
00223 
00224   typedef Color<float> Colorf;
00225   typedef Color<double> Colord;
00226 
00227 }
00228 
00229 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Fri May 31 15:37:53 2013 for VVE by  doxygen 1.6.3