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