00001 #pragma once 00002 00003 #include "common.h" 00004 00005 00006 00007 using std::max; 00008 using std::min; 00009 00014 class Color 00015 { 00016 public: 00017 00025 Color(const unsigned char uR=0, const unsigned char uG=0, const unsigned char uB=0, const unsigned char uA=255) 00026 { 00027 m_uData[0] = uR; 00028 m_uData[1] = uG; 00029 m_uData[2] = uB; 00030 m_uData[3] = uA; 00031 }; 00032 00033 00041 Color(const int iR, const int iG, const int iB, const int iA=255) 00042 { 00043 m_uData[0] = (unsigned char) max(0,min(255,iR)); 00044 m_uData[1] = (unsigned char) max(0,min(255,iG)); 00045 m_uData[2] = (unsigned char) max(0,min(255,iB)); 00046 m_uData[3] = (unsigned char) max(0,min(255,iA)); 00047 }; 00048 00049 00057 Color(const float fR, const float fG, const float fB, const float fA=1.0f) 00058 { 00059 m_uData[0] = (unsigned char) max(0.0f,min(255.0f,fR*255.0f)); 00060 m_uData[1] = (unsigned char) max(0.0f,min(255.0f,fG*255.0f)); 00061 m_uData[2] = (unsigned char) max(0.0f,min(255.0f,fB*255.0f)); 00062 m_uData[3] = (unsigned char) max(0.0f,min(255.0f,fA*255.0f)); 00063 }; 00064 00069 Color(const unsigned char *pData) 00070 { 00071 m_uData[0] = pData[0]; 00072 m_uData[1] = pData[1]; 00073 m_uData[2] = pData[2]; 00074 m_uData[3] = pData[3]; 00075 }; 00076 00081 Color(const float *pData) 00082 { 00083 m_uData[0] = (unsigned char) max(0.0f,min(255.0f,pData[0]*255.0f)); 00084 m_uData[1] = (unsigned char) max(0.0f,min(255.0f,pData[1]*255.0f)); 00085 m_uData[2] = (unsigned char) max(0.0f,min(255.0f,pData[2]*255.0f)); 00086 m_uData[3] = (unsigned char) max(0.0f,min(255.0f,pData[3]*255.0f)); 00087 }; 00088 00089 00093 ~Color() 00094 { 00095 }; 00096 00097 00105 void Set(const unsigned char uR, const unsigned char uG, const unsigned char uB, const unsigned char uA = 255) 00106 { 00107 m_uData[0] = uR; 00108 m_uData[1] = uG; 00109 m_uData[2] = uB; 00110 m_uData[3] = uA; 00111 }; 00112 00117 const unsigned char * Get() const 00118 { 00119 return m_uData; 00120 }; 00121 00126 void SetRed(const unsigned char uR) 00127 { 00128 m_uData[0] = uR; 00129 }; 00130 00135 const unsigned char GetRed() const 00136 { 00137 return m_uData[0]; 00138 }; 00139 00144 void SetGreen(const unsigned char uG) 00145 { 00146 m_uData[1] = uG; 00147 }; 00148 00153 const unsigned char GetGreen() const 00154 { 00155 return m_uData[1]; 00156 }; 00157 00162 void SetBlue(const unsigned char uB) 00163 { 00164 m_uData[2] = uB; 00165 }; 00166 00171 const unsigned char GetBlue() const 00172 { 00173 return m_uData[2]; 00174 }; 00175 00180 void SetAlpha(const unsigned char uA) 00181 { 00182 m_uData[3] = uA; 00183 }; 00184 00189 const unsigned char GetAlpha() const 00190 { 00191 return m_uData[3]; 00192 }; 00193 00201 void SetNormalized(const float fR, const float fG, const float fB, const float fA = 1.0f) 00202 { 00203 m_uData[0] = (unsigned char) max(0.0f,min(255.0f,fR*255.0f)); 00204 m_uData[1] = (unsigned char) max(0.0f,min(255.0f,fG*255.0f)); 00205 m_uData[2] = (unsigned char) max(0.0f,min(255.0f,fB*255.0f)); 00206 m_uData[3] = (unsigned char) max(0.0f,min(255.0f,fA*255.0f)); 00207 }; 00208 00213 void SetNormalizedRed(const float fR) 00214 { 00215 m_uData[0] = (unsigned char) max(0.0f,min(255.0f,fR*255.0f)); 00216 }; 00217 00222 void SetNormalizedGreen(const float fG) 00223 { 00224 m_uData[1] = (unsigned char) max(0.0f,min(255.0f,fG*255.0f)); 00225 }; 00226 00231 void SetNormalizedBlue(const float fB) 00232 { 00233 m_uData[2] = (unsigned char) max(0.0f,min(255.0f,fB*255.0f)); 00234 }; 00235 00240 void SetNormalizedAlpha(const float fA) 00241 { 00242 m_uData[3] = (unsigned char) max(0.0f,min(255.0f,fA*255.0f)); 00243 }; 00244 00249 const float GetNormalizedRed() const 00250 { 00251 return float(m_uData[0]) / 255.0f; 00252 }; 00253 00258 const float GetNormalizedGreen() const 00259 { 00260 return float(m_uData[1]) / 255.0f; 00261 }; 00262 00267 const float GetNormalizedBlue() const 00268 { 00269 return float(m_uData[2]) / 255.0f; 00270 }; 00271 00276 const float GetNormalizedAlpha() const 00277 { 00278 return float(m_uData[3]) / 255.0f; 00279 }; 00280 00287 void SetHSV(const float fHue, const float fSaturation, const float fValue) 00288 { 00289 float fRed = fValue; 00290 float fGreen = fValue; 00291 float fBlue = fValue; 00292 00293 if(fSaturation > 0.0f) 00294 { 00295 const float fH = fHue * 6.0f; 00296 const unsigned int i = unsigned int(fH); 00297 const float fF = fH - i; 00298 00299 const float fP = fValue * (1.0f - fSaturation); 00300 const float fQ = fValue * (1.0f - (fSaturation * fF)); 00301 const float fT = fValue * (1.0f - (fSaturation * (1.0f - fF))); 00302 00303 switch(i) 00304 { 00305 case 0: 00306 fGreen = fT; 00307 fBlue = fP; 00308 break; 00309 case 1: 00310 fRed = fQ; 00311 fBlue = fP; 00312 break; 00313 case 2: 00314 fRed = fP; 00315 fBlue = fT; 00316 break; 00317 case 3: 00318 fRed = fP; 00319 fGreen = fQ; 00320 break; 00321 case 4: 00322 fRed = fT; 00323 fGreen = fP; 00324 break; 00325 case 5: 00326 fGreen = fP; 00327 fBlue = fQ; 00328 break; 00329 } 00330 } 00331 00332 SetNormalized(fRed,fGreen,fBlue,GetNormalizedAlpha()); 00333 }; 00334 00335 const Color & operator*=(const float fOther) 00336 { 00337 m_uData[0] = (unsigned char) max(0.0f,min(255.0f,float(m_uData[0]) * fOther)); 00338 m_uData[1] = (unsigned char) max(0.0f,min(255.0f,float(m_uData[1]) * fOther)); 00339 m_uData[2] = (unsigned char) max(0.0f,min(255.0f,float(m_uData[2]) * fOther)); 00340 m_uData[3] = (unsigned char) max(0.0f,min(255.0f,float(m_uData[3]) * fOther)); 00341 return *this; 00342 }; 00343 00344 const Color & operator/=(const float fOther) 00345 { 00346 m_uData[0] = (unsigned char) max(0.0f,min(255.0f,float(m_uData[0]) / fOther)); 00347 m_uData[1] = (unsigned char) max(0.0f,min(255.0f,float(m_uData[1]) / fOther)); 00348 m_uData[2] = (unsigned char) max(0.0f,min(255.0f,float(m_uData[2]) / fOther)); 00349 m_uData[3] = (unsigned char) max(0.0f,min(255.0f,float(m_uData[3]) / fOther)); 00350 return *this; 00351 }; 00352 00353 const Color & operator+=(const Color &colOther) 00354 { 00355 m_uData[0] = (unsigned char) min(255,int(m_uData[0])+int(colOther.m_uData[0])); 00356 m_uData[1] = (unsigned char) min(255,int(m_uData[1])+int(colOther.m_uData[1])); 00357 m_uData[2] = (unsigned char) min(255,int(m_uData[2])+int(colOther.m_uData[2])); 00358 m_uData[3] = (unsigned char) min(255,int(m_uData[3])+int(colOther.m_uData[3])); 00359 return *this; 00360 }; 00361 00362 const Color & operator-=(const Color &colOther) 00363 { 00364 m_uData[0] = (unsigned char) max(0,int(m_uData[0])-int(colOther.m_uData[0])); 00365 m_uData[1] = (unsigned char) max(0,int(m_uData[1])-int(colOther.m_uData[1])); 00366 m_uData[2] = (unsigned char) max(0,int(m_uData[2])-int(colOther.m_uData[2])); 00367 m_uData[3] = (unsigned char) max(0,int(m_uData[2])-int(colOther.m_uData[3])); 00368 return *this; 00369 }; 00370 00371 const Color operator*(const float fOther) const 00372 { 00373 Color colNew = *this; 00374 colNew *= fOther; 00375 return colNew; 00376 }; 00377 00378 const Color operator/(const float fOther) const 00379 { 00380 Color colNew = *this; 00381 colNew *= fOther; 00382 return colNew; 00383 }; 00384 00385 const Color operator+(const Color &colOther) const 00386 { 00387 Color colNew = *this; 00388 colNew += colOther; 00389 return colNew; 00390 }; 00391 00392 const Color operator-(const Color &colOther) const 00393 { 00394 Color colNew = *this; 00395 colNew -= colOther; 00396 return colNew; 00397 }; 00398 00399 const bool operator==(const Color & colOther) const 00400 { 00401 return (memcmp((void*)m_uData,(void*)colOther.m_uData,sizeof(unsigned char)*4) == 0); 00402 }; 00403 00404 const bool operator!=(const Color & colOther) const 00405 { 00406 return !(*this == colOther); 00407 }; 00408 00409 const bool choose() 00410 { 00411 return chooseColor(*this); 00412 }; 00413 00414 static const bool chooseColor(Color & colColor) 00415 { 00416 CHOOSECOLOR cc; 00417 static COLORREF vcCustom[16]; 00418 00419 ZeroMemory(&cc, sizeof(cc)); 00420 cc.lStructSize = sizeof(cc); 00421 cc.hwndOwner = GetForegroundWindow(); 00422 cc.lpCustColors = (LPDWORD) vcCustom; 00423 cc.rgbResult = RGB(colColor.GetRed(),colColor.GetGreen(),colColor.GetBlue()); 00424 cc.Flags = CC_ANYCOLOR | CC_FULLOPEN | CC_RGBINIT; 00425 00426 const bool bResult = (ChooseColor(&cc) != 0); 00427 00428 if (bResult) 00429 { 00430 colColor.SetRed(GetRValue(cc.rgbResult)); 00431 colColor.SetGreen(GetGValue(cc.rgbResult)); 00432 colColor.SetBlue(GetBValue(cc.rgbResult)); 00433 } 00434 00435 return bResult; 00436 } 00437 00438 private: 00439 unsigned char m_uData[4]; 00440 }; 00441 00442 inline std::ostream & operator<< (std::ostream & os, const Color & colColor) 00443 { 00444 os << "(" << unsigned int(colColor.GetRed()) << ";" << unsigned int(colColor.GetGreen()) << ";" << unsigned int(colColor.GetBlue()) << ";" << unsigned short(colColor.GetAlpha()) << ")"; 00445 return os; 00446 } 00447 00448 inline std::istream & operator>> (std::istream & is, Color & colColor) 00449 { 00450 unsigned int uR,uG,uB,uA; 00451 00452 if (is >> eat("(") >> uR >> eat(";") >> uG >> eat(";") >> uB >> eat(";") >> uA >> eat(")")) 00453 colColor.Set(unsigned char(uR),unsigned char(uG),unsigned char(uB),unsigned char(uA)); 00454 00455 return is; 00456 }