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