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