00001
00032 #ifndef LL_V3COLOR_H
00033 #define LL_V3COLOR_H
00034
00035 class LLColor4;
00036 class LLVector4;
00037
00038 #include "llerror.h"
00039 #include "llmath.h"
00040 #include "llsd.h"
00041
00042
00043
00044 static const U32 LENGTHOFCOLOR3 = 3;
00045
00046 class LLColor3
00047 {
00048 public:
00049 F32 mV[LENGTHOFCOLOR3];
00050
00051 static LLColor3 white;
00052 static LLColor3 black;
00053 static LLColor3 grey;
00054
00055 public:
00056 LLColor3();
00057 LLColor3(F32 r, F32 g, F32 b);
00058 LLColor3(const F32 *vec);
00059 LLColor3(char *color_string);
00060 explicit LLColor3(const LLColor4& color4);
00061 explicit LLColor3(const LLVector4& vector4);
00062 LLColor3(const LLSD& sd);
00063
00064
00065 LLSD getValue() const
00066 {
00067 LLSD ret;
00068 ret[0] = mV[0];
00069 ret[1] = mV[1];
00070 ret[2] = mV[2];
00071 return ret;
00072 }
00073
00074 void setValue(const LLSD& sd)
00075 {
00076 mV[0] = (F32) sd[0].asReal();;
00077 mV[1] = (F32) sd[1].asReal();;
00078 mV[2] = (F32) sd[2].asReal();;
00079 }
00080
00081 void calcHSL(F32* hue, F32* saturation, F32* luminance) const;
00082
00083 const LLColor3& setToBlack();
00084 const LLColor3& setToWhite();
00085 const LLColor3& setVec(F32 x, F32 y, F32 z);
00086 const LLColor3& setVec(const LLColor3 &vec);
00087 const LLColor3& setVec(const F32 *vec);
00088
00089 F32 magVec() const;
00090 F32 magVecSquared() const;
00091 F32 normVec();
00092 F32 brightness() const;
00093
00094 const LLColor3& operator=(const LLColor4 &a);
00095
00096 friend std::ostream& operator<<(std::ostream& s, const LLColor3 &a);
00097 friend LLColor3 operator+(const LLColor3 &a, const LLColor3 &b);
00098 friend LLColor3 operator-(const LLColor3 &a, const LLColor3 &b);
00099
00100 friend const LLColor3& operator+=(LLColor3 &a, const LLColor3 &b);
00101 friend const LLColor3& operator-=(LLColor3 &a, const LLColor3 &b);
00102 friend const LLColor3& operator*=(LLColor3 &a, const LLColor3 &b);
00103
00104 friend LLColor3 operator*(const LLColor3 &a, const LLColor3 &b);
00105 friend LLColor3 operator*(const LLColor3 &a, F32 k);
00106 friend LLColor3 operator*(F32 k, const LLColor3 &a);
00107
00108 friend bool operator==(const LLColor3 &a, const LLColor3 &b);
00109 friend bool operator!=(const LLColor3 &a, const LLColor3 &b);
00110
00111 friend const LLColor3& operator*=(LLColor3 &a, F32 k);
00112
00113 friend LLColor3 operator-(const LLColor3 &a);
00114
00115 inline void clamp();
00116 inline void exp();
00117 };
00118
00119 LLColor3 lerp(const LLColor3 &a, const LLColor3 &b, F32 u);
00120
00121
00122 void LLColor3::clamp()
00123 {
00124
00125 if (mV[0] < 0.f)
00126 {
00127 mV[0] = 0.f;
00128 }
00129 else if (mV[0] > 1.f)
00130 {
00131 mV[0] = 1.f;
00132 }
00133 if (mV[1] < 0.f)
00134 {
00135 mV[1] = 0.f;
00136 }
00137 else if (mV[1] > 1.f)
00138 {
00139 mV[1] = 1.f;
00140 }
00141 if (mV[2] < 0.f)
00142 {
00143 mV[2] = 0.f;
00144 }
00145 else if (mV[2] > 1.f)
00146 {
00147 mV[2] = 1.f;
00148 }
00149 }
00150
00151
00152 F32 distVec(const LLColor3 &a, const LLColor3 &b);
00153 F32 distVec_squared(const LLColor3 &a, const LLColor3 &b);
00154
00155 inline LLColor3::LLColor3(void)
00156 {
00157 mV[0] = 0.f;
00158 mV[1] = 0.f;
00159 mV[2] = 0.f;
00160 }
00161
00162 inline LLColor3::LLColor3(F32 r, F32 g, F32 b)
00163 {
00164 mV[VX] = r;
00165 mV[VY] = g;
00166 mV[VZ] = b;
00167 }
00168
00169
00170 inline LLColor3::LLColor3(const F32 *vec)
00171 {
00172 mV[VX] = vec[VX];
00173 mV[VY] = vec[VY];
00174 mV[VZ] = vec[VZ];
00175 }
00176
00177 inline LLColor3::LLColor3(char* color_string)
00178 {
00179 if (strlen(color_string) < 6)
00180 {
00181 mV[0] = 0.f;
00182 mV[1] = 0.f;
00183 mV[2] = 0.f;
00184 return;
00185 }
00186
00187 static char tempstr[7];
00188 strncpy(tempstr,color_string,6);
00189 tempstr[6] = '\0';
00190 mV[VZ] = (F32)strtol(&tempstr[4],NULL,16)/255.f;
00191 tempstr[4] = '\0';
00192 mV[VY] = (F32)strtol(&tempstr[2],NULL,16)/255.f;
00193 tempstr[2] = '\0';
00194 mV[VX] = (F32)strtol(&tempstr[0],NULL,16)/255.f;
00195 }
00196
00197 inline const LLColor3& LLColor3::setToBlack(void)
00198 {
00199 mV[0] = 0.f;
00200 mV[1] = 0.f;
00201 mV[2] = 0.f;
00202 return (*this);
00203 }
00204
00205 inline const LLColor3& LLColor3::setToWhite(void)
00206 {
00207 mV[0] = 1.f;
00208 mV[1] = 1.f;
00209 mV[2] = 1.f;
00210 return (*this);
00211 }
00212
00213 inline const LLColor3& LLColor3::setVec(F32 r, F32 g, F32 b)
00214 {
00215 mV[0] = r;
00216 mV[1] = g;
00217 mV[2] = b;
00218 return (*this);
00219 }
00220
00221 inline const LLColor3& LLColor3::setVec(const LLColor3 &vec)
00222 {
00223 mV[0] = vec.mV[0];
00224 mV[1] = vec.mV[1];
00225 mV[2] = vec.mV[2];
00226 return (*this);
00227 }
00228
00229 inline const LLColor3& LLColor3::setVec(const F32 *vec)
00230 {
00231 mV[0] = vec[0];
00232 mV[1] = vec[1];
00233 mV[2] = vec[2];
00234 return (*this);
00235 }
00236
00237 inline F32 LLColor3::brightness(void) const
00238 {
00239 return (mV[0] + mV[1] + mV[2]) / 3.0f;
00240 }
00241
00242 inline F32 LLColor3::magVec(void) const
00243 {
00244 return fsqrtf(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]);
00245 }
00246
00247 inline F32 LLColor3::magVecSquared(void) const
00248 {
00249 return mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2];
00250 }
00251
00252 inline F32 LLColor3::normVec(void)
00253 {
00254 F32 mag = fsqrtf(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]);
00255 F32 oomag;
00256
00257 if (mag)
00258 {
00259 oomag = 1.f/mag;
00260 mV[0] *= oomag;
00261 mV[1] *= oomag;
00262 mV[2] *= oomag;
00263 }
00264 return (mag);
00265 }
00266
00267 inline void LLColor3::exp()
00268 {
00269 #if 0
00270 mV[0] = ::exp(mV[0]);
00271 mV[1] = ::exp(mV[1]);
00272 mV[2] = ::exp(mV[2]);
00273 #else
00274 mV[0] = (F32)LL_FAST_EXP(mV[0]);
00275 mV[1] = (F32)LL_FAST_EXP(mV[1]);
00276 mV[2] = (F32)LL_FAST_EXP(mV[2]);
00277 #endif
00278 }
00279
00280
00281 inline LLColor3 operator+(const LLColor3 &a, const LLColor3 &b)
00282 {
00283 return LLColor3(
00284 a.mV[0] + b.mV[0],
00285 a.mV[1] + b.mV[1],
00286 a.mV[2] + b.mV[2]);
00287 }
00288
00289 inline LLColor3 operator-(const LLColor3 &a, const LLColor3 &b)
00290 {
00291 return LLColor3(
00292 a.mV[0] - b.mV[0],
00293 a.mV[1] - b.mV[1],
00294 a.mV[2] - b.mV[2]);
00295 }
00296
00297 inline LLColor3 operator*(const LLColor3 &a, const LLColor3 &b)
00298 {
00299 return LLColor3(
00300 a.mV[0] * b.mV[0],
00301 a.mV[1] * b.mV[1],
00302 a.mV[2] * b.mV[2]);
00303 }
00304
00305 inline LLColor3 operator*(const LLColor3 &a, F32 k)
00306 {
00307 return LLColor3( a.mV[0] * k, a.mV[1] * k, a.mV[2] * k );
00308 }
00309
00310 inline LLColor3 operator*(F32 k, const LLColor3 &a)
00311 {
00312 return LLColor3( a.mV[0] * k, a.mV[1] * k, a.mV[2] * k );
00313 }
00314
00315 inline bool operator==(const LLColor3 &a, const LLColor3 &b)
00316 {
00317 return ( (a.mV[0] == b.mV[0])
00318 &&(a.mV[1] == b.mV[1])
00319 &&(a.mV[2] == b.mV[2]));
00320 }
00321
00322 inline bool operator!=(const LLColor3 &a, const LLColor3 &b)
00323 {
00324 return ( (a.mV[0] != b.mV[0])
00325 ||(a.mV[1] != b.mV[1])
00326 ||(a.mV[2] != b.mV[2]));
00327 }
00328
00329 inline const LLColor3 &operator*=(LLColor3 &a, const LLColor3 &b)
00330 {
00331 a.mV[0] *= b.mV[0];
00332 a.mV[1] *= b.mV[1];
00333 a.mV[2] *= b.mV[2];
00334 return a;
00335 }
00336
00337 inline const LLColor3& operator+=(LLColor3 &a, const LLColor3 &b)
00338 {
00339 a.mV[0] += b.mV[0];
00340 a.mV[1] += b.mV[1];
00341 a.mV[2] += b.mV[2];
00342 return a;
00343 }
00344
00345 inline const LLColor3& operator-=(LLColor3 &a, const LLColor3 &b)
00346 {
00347 a.mV[0] -= b.mV[0];
00348 a.mV[1] -= b.mV[1];
00349 a.mV[2] -= b.mV[2];
00350 return a;
00351 }
00352
00353 inline const LLColor3& operator*=(LLColor3 &a, F32 k)
00354 {
00355 a.mV[0] *= k;
00356 a.mV[1] *= k;
00357 a.mV[2] *= k;
00358 return a;
00359 }
00360
00361 inline LLColor3 operator-(const LLColor3 &a)
00362 {
00363 return LLColor3(
00364 1.f - a.mV[0],
00365 1.f - a.mV[1],
00366 1.f - a.mV[2] );
00367 }
00368
00369
00370
00371 inline F32 distVec(const LLColor3 &a, const LLColor3 &b)
00372 {
00373 F32 x = a.mV[0] - b.mV[0];
00374 F32 y = a.mV[1] - b.mV[1];
00375 F32 z = a.mV[2] - b.mV[2];
00376 return fsqrtf( x*x + y*y + z*z );
00377 }
00378
00379 inline F32 distVec_squared(const LLColor3 &a, const LLColor3 &b)
00380 {
00381 F32 x = a.mV[0] - b.mV[0];
00382 F32 y = a.mV[1] - b.mV[1];
00383 F32 z = a.mV[2] - b.mV[2];
00384 return x*x + y*y + z*z;
00385 }
00386
00387 inline LLColor3 lerp(const LLColor3 &a, const LLColor3 &b, F32 u)
00388 {
00389 return LLColor3(
00390 a.mV[VX] + (b.mV[VX] - a.mV[VX]) * u,
00391 a.mV[VY] + (b.mV[VY] - a.mV[VY]) * u,
00392 a.mV[VZ] + (b.mV[VZ] - a.mV[VZ]) * u);
00393 }
00394
00395
00396 #endif