src/Color.h

Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include "common.h"
00004 
00005 // a (r,g,b,a) color value
00006 class Color
00007 {
00008 public:
00009 
00010         Color(const unsigned char uR=0, const unsigned char uG=0, const unsigned char uB=0, const unsigned char uA=255)
00011         {
00012                 m_uData[0] = uR;
00013                 m_uData[1] = uG;
00014                 m_uData[2] = uB;
00015                 m_uData[3] = uA;
00016         };
00017 
00018         Color(const int iR, const int iG, const int iB, const int iA=255)
00019         {
00020                 m_uData[0] = (unsigned char) std::max(0,std::min(255,iR));
00021                 m_uData[1] = (unsigned char) std::max(0,std::min(255,iG));
00022                 m_uData[2] = (unsigned char) std::max(0,std::min(255,iB));
00023                 m_uData[3] = (unsigned char) std::max(0,std::min(255,iA));
00024         };
00025 
00026         Color(const float fR, const float fG, const float fB, const float fA=1.0f)
00027         {
00028                 m_uData[0] = (unsigned char) std::max(0.0f,std::min(255.0f,fR*255.0f));
00029                 m_uData[1] = (unsigned char) std::max(0.0f,std::min(255.0f,fG*255.0f));
00030                 m_uData[2] = (unsigned char) std::max(0.0f,std::min(255.0f,fB*255.0f));
00031                 m_uData[3] = (unsigned char) std::max(0.0f,std::min(255.0f,fA*255.0f));
00032         };
00033 
00034         Color(const unsigned char *pData)
00035         {
00036                 m_uData[0] = pData[0];
00037                 m_uData[1] = pData[1];
00038                 m_uData[2] = pData[2];
00039                 m_uData[3] = pData[3];
00040         };
00041 
00042         Color(const float *pData)
00043         {
00044                 m_uData[0] = (unsigned char) std::max(0.0f,std::min(255.0f,pData[0]*255.0f));
00045                 m_uData[1] = (unsigned char) std::max(0.0f,std::min(255.0f,pData[1]*255.0f));
00046                 m_uData[2] = (unsigned char) std::max(0.0f,std::min(255.0f,pData[2]*255.0f));
00047                 m_uData[3] = (unsigned char) std::max(0.0f,std::min(255.0f,pData[3]*255.0f));
00048         };
00049 
00050         ~Color()
00051         {
00052         };
00053 
00054         void Set(const unsigned char uR, const unsigned char uG, const unsigned char uB, const unsigned char uA = 255)
00055         {
00056                 m_uData[0] = uR;
00057                 m_uData[1] = uG;
00058                 m_uData[2] = uB;
00059                 m_uData[3] = uA;
00060         };
00061 
00062         const unsigned char * Get() const
00063         {
00064                 return m_uData;
00065         };
00066 
00067         void SetRed(const unsigned char uR)
00068         {
00069                 m_uData[0] = uR;
00070         };
00071 
00072         const unsigned char GetRed() const
00073         {
00074                 return m_uData[0];
00075         };
00076 
00077         void SetGreen(const unsigned char uG)
00078         {
00079                 m_uData[1] = uG;
00080         };
00081 
00082         const unsigned char GetGreen() const
00083         {
00084                 return m_uData[1];
00085         };
00086 
00087         void SetBlue(const unsigned char uB)
00088         {
00089                 m_uData[2] = uB;
00090         };
00091 
00092         const unsigned char GetBlue() const
00093         {
00094                 return m_uData[2];
00095         };
00096 
00097         void SetAlpha(const unsigned char uA)
00098         {
00099                 m_uData[3] = uA;
00100         };
00101 
00102         const unsigned char GetAlpha() const
00103         {
00104                 return m_uData[3];
00105         };
00106 
00107         void SetNormalized(const float fR, const float fG, const float fB, const float fA = 1.0f)
00108         {
00109                 m_uData[0] = (unsigned char) std::max(0.0f,std::min(255.0f,fR*255.0f));
00110                 m_uData[1] = (unsigned char) std::max(0.0f,std::min(255.0f,fG*255.0f));
00111                 m_uData[2] = (unsigned char) std::max(0.0f,std::min(255.0f,fB*255.0f));
00112                 m_uData[3] = (unsigned char) std::max(0.0f,std::min(255.0f,fA*255.0f));
00113         };
00114 
00115         void SetNormalizedRed(const float fR)
00116         {
00117                 m_uData[0] = (unsigned char) std::max(0.0f,std::min(255.0f,fR*255.0f));
00118         };
00119 
00120         void SetNormalizedGreen(const float fG)
00121         {
00122                 m_uData[1] = (unsigned char) std::max(0.0f,std::min(255.0f,fG*255.0f));
00123         };
00124 
00125         void SetNormalizedBlue(const float fB)
00126         {
00127                 m_uData[2] = (unsigned char) std::max(0.0f,std::min(255.0f,fB*255.0f));
00128         };
00129 
00130         void SetNormalizedAlpha(const float fA)
00131         {
00132                 m_uData[3] = (unsigned char) std::max(0.0f,std::min(255.0f,fA*255.0f));
00133         };
00134 
00135         const float GetNormalizedRed() const
00136         {
00137                 return float(m_uData[0]) / 255.0f;
00138         };
00139 
00140         const float GetNormalizedGreen() const
00141         {
00142                 return float(m_uData[1]) / 255.0f;
00143         };
00144 
00145         const float GetNormalizedBlue() const
00146         {
00147                 return float(m_uData[2]) / 255.0f;
00148         };
00149 
00150         const float GetNormalizedAlpha() const
00151         {
00152                 return float(m_uData[3]) / 255.0f;
00153         };
00154 
00155         void SetHSV(const float fHue, const float fSaturation, const float fValue)
00156         {
00157                 float fRed = fValue;
00158                 float fGreen = fValue;
00159                 float fBlue = fValue;
00160                 
00161                 if(fSaturation > 0.0f)
00162                 {
00163                         const float fH = fHue * 6.0f;
00164                         const unsigned int i = unsigned int(fH);
00165                         const float fF = fH - i;
00166 
00167                         const float fP = fValue * (1.0f - fSaturation);
00168                         const float fQ = fValue * (1.0f - (fSaturation * fF));
00169                         const float fT = fValue * (1.0f - (fSaturation * (1.0f - fF)));
00170 
00171                         switch(i)
00172                         {
00173                         case 0:                 
00174                                 fGreen = fT;
00175                                 fBlue = fP;
00176                                 break;
00177                         case 1:
00178                                 fRed = fQ;
00179                                 fBlue = fP;
00180                                 break;
00181                         case 2:
00182                                 fRed = fP;
00183                                 fBlue = fT;
00184                                 break;
00185                         case 3:
00186                                 fRed = fP;
00187                                 fGreen = fQ;
00188                                 break;
00189                         case 4:
00190                                 fRed = fT;
00191                                 fGreen = fP;
00192                                 break;
00193                         case 5:
00194                                 fGreen = fP;
00195                                 fBlue = fQ;
00196                                 break;
00197                         }
00198                 }
00199 
00200                 SetNormalized(fRed,fGreen,fBlue,GetNormalizedAlpha());
00201         };
00202 
00203         const Color & operator*=(const float fOther)
00204         {
00205                 m_uData[0] = (unsigned char) std::max(0.0f,std::min(255.0f,float(m_uData[0]) * fOther));
00206                 m_uData[1] = (unsigned char) std::max(0.0f,std::min(255.0f,float(m_uData[1]) * fOther));
00207                 m_uData[2] = (unsigned char) std::max(0.0f,std::min(255.0f,float(m_uData[2]) * fOther));
00208                 m_uData[3] = (unsigned char) std::max(0.0f,std::min(255.0f,float(m_uData[3]) * fOther));
00209                 return *this;
00210         };
00211 
00212         const Color & operator/=(const float fOther)
00213         {
00214                 m_uData[0] = (unsigned char) std::max(0.0f,std::min(255.0f,float(m_uData[0]) / fOther));
00215                 m_uData[1] = (unsigned char) std::max(0.0f,std::min(255.0f,float(m_uData[1]) / fOther));
00216                 m_uData[2] = (unsigned char) std::max(0.0f,std::min(255.0f,float(m_uData[2]) / fOther));
00217                 m_uData[3] = (unsigned char) std::max(0.0f,std::min(255.0f,float(m_uData[3]) / fOther));
00218                 return *this;
00219         };
00220 
00221         const Color & operator+=(const Color &colOther)
00222         {
00223                 m_uData[0] = (unsigned char) std::min(255,int(m_uData[0])+int(colOther.m_uData[0]));
00224                 m_uData[1] = (unsigned char) std::min(255,int(m_uData[1])+int(colOther.m_uData[1]));
00225                 m_uData[2] = (unsigned char) std::min(255,int(m_uData[2])+int(colOther.m_uData[2]));
00226                 m_uData[3] = (unsigned char) std::min(255,int(m_uData[3])+int(colOther.m_uData[3]));
00227                 return *this;
00228         };
00229 
00230         const Color & operator-=(const Color &colOther)
00231         {
00232                 m_uData[0] = (unsigned char) std::max(0,int(m_uData[0])-int(colOther.m_uData[0]));
00233                 m_uData[1] = (unsigned char) std::max(0,int(m_uData[1])-int(colOther.m_uData[1]));
00234                 m_uData[2] = (unsigned char) std::max(0,int(m_uData[2])-int(colOther.m_uData[2]));
00235                 m_uData[3] = (unsigned char) std::max(0,int(m_uData[2])-int(colOther.m_uData[3]));
00236                 return *this;
00237         };
00238 
00239         const Color operator*(const float fOther) const
00240         {
00241                 Color colNew = *this;
00242                 colNew *= fOther;
00243                 return colNew;
00244         };
00245 
00246         const Color operator/(const float fOther) const
00247         {
00248                 Color colNew = *this;
00249                 colNew *= fOther;
00250                 return colNew;
00251         };
00252 
00253         const Color operator+(const Color &colOther) const
00254         {
00255                 Color colNew = *this;
00256                 colNew += colOther;
00257                 return colNew;
00258         };
00259 
00260         const Color operator-(const Color &colOther) const
00261         {
00262                 Color colNew = *this;
00263                 colNew -= colOther;
00264                 return colNew;
00265         };
00266 
00267         const bool operator==(const Color & colOther) const
00268         {
00269                 return (memcmp((void*)m_uData,(void*)colOther.m_uData,sizeof(unsigned char)*4) == 0);
00270         };
00271 
00272         const bool operator!=(const Color & colOther) const
00273         {
00274                 return !(*this == colOther);
00275         };
00276 
00277         const bool choose()
00278         {
00279                 return chooseColor(*this);
00280         };
00281 
00282         static const bool chooseColor(Color & colColor)
00283         {
00284                 CHOOSECOLOR cc;
00285                 static COLORREF vcCustom[16];
00286                 
00287                 ZeroMemory(&cc, sizeof(cc));
00288                 cc.lStructSize = sizeof(cc);
00289                 cc.hwndOwner = GetForegroundWindow();
00290                 cc.lpCustColors = (LPDWORD) vcCustom;
00291                 cc.rgbResult = RGB(colColor.GetRed(),colColor.GetGreen(),colColor.GetBlue());
00292                 cc.Flags = CC_ANYCOLOR | CC_FULLOPEN | CC_RGBINIT;
00293 
00294                 const bool bResult = (ChooseColor(&cc) != 0);
00295 
00296                 if (bResult)
00297                 {
00298                         colColor.SetRed(GetRValue(cc.rgbResult));
00299                         colColor.SetGreen(GetGValue(cc.rgbResult));
00300                         colColor.SetBlue(GetBValue(cc.rgbResult));
00301                 }
00302 
00303                 return bResult;
00304         }
00305 
00306 private:
00307         unsigned char m_uData[4];
00308 };
00309 
00310 inline std::ostream & operator<< (std::ostream & os, const Color & colColor)
00311 {
00312         os << "(" << unsigned int(colColor.GetRed()) << ";" << unsigned int(colColor.GetGreen()) << ";" << unsigned int(colColor.GetBlue()) << ";" << unsigned short(colColor.GetAlpha()) << ")";
00313         return os;
00314 }
00315 
00316 inline std::istream & operator>> (std::istream & is, Color & colColor)
00317 {       
00318         unsigned int uR,uG,uB,uA;
00319 
00320         if (is >> eat("(") >> uR >> eat(";") >> uG >> eat(";") >> uB >> eat(";") >> uA >> eat(")"))
00321                 colColor.Set(unsigned char(uR),unsigned char(uG),unsigned char(uB),unsigned char(uA));
00322 
00323         return is;
00324 }

Generated on Mon Dec 10 18:18:11 2007 for VisLU by  doxygen 1.5.4