v4color.h

Go to the documentation of this file.
00001 
00032 #ifndef LL_V4COLOR_H
00033 #define LL_V4COLOR_H
00034 
00035 #include "llerror.h"
00036 //#include "vmath.h"
00037 #include "llmath.h"
00038 #include "llsd.h"
00039 
00040 class LLColor3;
00041 class LLColor4U;
00042 class LLVector4;
00043 
00044 //  LLColor4 = |x y z w|
00045 
00046 static const U32 LENGTHOFCOLOR4 = 4;
00047 
00048 static const U32 MAX_LENGTH_OF_COLOR_NAME = 15; //Give plenty of room for additional colors...
00049 
00050 class LLColor4
00051 {
00052         public:
00053                 F32 mV[LENGTHOFCOLOR4];
00054                 LLColor4();                                             // Initializes LLColor4 to (0, 0, 0, 1)
00055                 LLColor4(F32 r, F32 g, F32 b);          // Initializes LLColor4 to (r, g, b, 1)
00056                 LLColor4(F32 r, F32 g, F32 b, F32 a);           // Initializes LLColor4 to (r. g, b, a)
00057                 LLColor4(U32 clr);                                                      // Initializes LLColor4 to (r=clr>>24, etc))
00058                 LLColor4(const F32 *vec);                       // Initializes LLColor4 to (vec[0]. vec[1], vec[2], 1)
00059                 LLColor4(const LLColor3 &vec, F32 a = 1.f);     // Initializes LLColor4 to (vec, a)
00060                 LLColor4(const LLSD& sd);
00061                 explicit LLColor4(const LLColor4U& color4u);  // "explicit" to avoid automatic conversion
00062                 explicit LLColor4(const LLVector4& vector4);  // "explicit" to avoid automatic conversion
00063 
00064                 LLSD getValue() const
00065                 {
00066                         LLSD ret;
00067                         ret[0] = mV[0];
00068                         ret[1] = mV[1];
00069                         ret[2] = mV[2];
00070                         ret[3] = mV[3];
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                         mV[3] = (F32) sd[3].asReal();
00080                 }
00081 
00082                 void calcHSL(F32* hue, F32* saturation, F32* luminance) const;
00083 
00084                 const LLColor4& setToBlack();                                           // zero LLColor4 to (0, 0, 0, 1)
00085                 const LLColor4& setToWhite();                                           // zero LLColor4 to (0, 0, 0, 1)
00086 
00087                 const LLColor4& setVec(F32 r, F32 g, F32 b, F32 a);     // Sets LLColor4 to (r, g, b, a)
00088                 const LLColor4& setVec(F32 r, F32 g, F32 b);    // Sets LLColor4 to (r, g, b) (no change in a)
00089                 const LLColor4& setVec(const LLColor4 &vec);    // Sets LLColor4 to vec
00090                 const LLColor4& setVec(const LLColor3 &vec);    // Sets LLColor4 to LLColor3 vec (no change in alpha)
00091                 const LLColor4& setVec(const LLColor3 &vec, F32 a);     // Sets LLColor4 to LLColor3 vec, with alpha specified
00092                 const LLColor4& setVec(const F32 *vec);                 // Sets LLColor4 to vec
00093                 const LLColor4& setVec(const LLColor4U& color4u); // Sets LLColor4 to color4u, rescaled.
00094 
00095 
00096                 const LLColor4&    setAlpha(F32 a);
00097 
00098                 F32                     magVec() const;                         // Returns magnitude of LLColor4
00099                 F32                     magVecSquared() const;          // Returns magnitude squared of LLColor4
00100                 F32                     normVec();                                      // Normalizes and returns the magnitude of LLColor4
00101                 BOOL            isOpaque() { return mV[VALPHA] == 1.f; }
00102 
00103                 F32 operator[](int idx) const { return mV[idx]; }
00104                 F32 &operator[](int idx) { return mV[idx]; }
00105         
00106             const LLColor4& operator=(const LLColor3 &a);       // Assigns vec3 to vec4 and returns vec4
00107                 const LLColor4& operator=(const LLSD& sd);
00108                 
00109                 friend std::ostream&     operator<<(std::ostream& s, const LLColor4 &a);                // Print a
00110                 friend LLColor4 operator+(const LLColor4 &a, const LLColor4 &b);        // Return vector a + b
00111                 friend LLColor4 operator-(const LLColor4 &a, const LLColor4 &b);        // Return vector a minus b
00112                 friend LLColor4 operator*(const LLColor4 &a, const LLColor4 &b);        // Return component wise a * b
00113                 friend LLColor4 operator*(const LLColor4 &a, F32 k);                            // Return rgb times scaler k (no alpha change)
00114                 friend LLColor4 operator*(F32 k, const LLColor4 &a);                            // Return rgb times scaler k (no alpha change)
00115                 friend LLColor4 operator%(const LLColor4 &a, F32 k);                            // Return alpha times scaler k (no rgb change)
00116                 friend LLColor4 operator%(F32 k, const LLColor4 &a);                            // Return alpha times scaler k (no rgb change)
00117                 friend bool operator==(const LLColor4 &a, const LLColor4 &b);           // Return a == b
00118                 friend bool operator!=(const LLColor4 &a, const LLColor4 &b);           // Return a != b
00119                 
00120                 friend bool operator==(const LLColor4 &a, const LLColor3 &b);           // Return a == b
00121                 friend bool operator!=(const LLColor4 &a, const LLColor3 &b);           // Return a != b
00122 
00123                 friend const LLColor4& operator+=(LLColor4 &a, const LLColor4 &b);      // Return vector a + b
00124                 friend const LLColor4& operator-=(LLColor4 &a, const LLColor4 &b);      // Return vector a minus b
00125                 friend const LLColor4& operator*=(LLColor4 &a, F32 k);                          // Return rgb times scaler k (no alpha change)
00126                 friend const LLColor4& operator%=(LLColor4 &a, F32 k);                          // Return alpha times scaler k (no rgb change)
00127 
00128                 friend const LLColor4& operator*=(LLColor4 &a, const LLColor4 &b); // Doesn't multiply alpha! (for lighting)
00129 
00130                 // conversion
00131                 operator const LLColor4U() const;
00132 
00133                 // Basic color values.
00134                 static LLColor4 red;
00135                 static LLColor4 green;
00136                 static LLColor4 blue;
00137                 static LLColor4 black;
00138                 static LLColor4 white;
00139                 static LLColor4 yellow;
00140                 static LLColor4 magenta;
00141                 static LLColor4 cyan;
00142                 static LLColor4 smoke;
00143                 static LLColor4 grey;
00144                 static LLColor4 orange;
00145                 static LLColor4 purple;
00146                 static LLColor4 pink;
00147                 static LLColor4 transparent;
00148 
00149                 // Extra color values.
00150                 static LLColor4 grey1;
00151                 static LLColor4 grey2;
00152                 static LLColor4 grey3;
00153                 static LLColor4 grey4;
00154 
00155                 static LLColor4 red1;
00156                 static LLColor4 red2;
00157                 static LLColor4 red3;
00158                 static LLColor4 red4;
00159                 static LLColor4 red5;
00160 
00161                 static LLColor4 green1;
00162                 static LLColor4 green2;
00163                 static LLColor4 green3;
00164                 static LLColor4 green4;
00165                 static LLColor4 green5;
00166                 static LLColor4 green6;
00167 
00168                 static LLColor4 blue1;
00169                 static LLColor4 blue2;
00170                 static LLColor4 blue3;
00171                 static LLColor4 blue4;
00172                 static LLColor4 blue5;
00173                 static LLColor4 blue6;
00174 
00175                 static LLColor4 yellow1;
00176                 static LLColor4 yellow2;
00177                 static LLColor4 yellow3;
00178                 static LLColor4 yellow4;
00179                 static LLColor4 yellow5;
00180                 static LLColor4 yellow6;
00181                 static LLColor4 yellow7;
00182                 static LLColor4 yellow8;
00183                 static LLColor4 yellow9;
00184 
00185                 static LLColor4 orange1;
00186                 static LLColor4 orange2;
00187                 static LLColor4 orange3;
00188                 static LLColor4 orange4;
00189                 static LLColor4 orange5;
00190                 static LLColor4 orange6;
00191 
00192                 static LLColor4 magenta1;
00193                 static LLColor4 magenta2;
00194                 static LLColor4 magenta3;
00195                 static LLColor4 magenta4;
00196 
00197                 static LLColor4 purple1;
00198                 static LLColor4 purple2;
00199                 static LLColor4 purple3;
00200                 static LLColor4 purple4;
00201                 static LLColor4 purple5;
00202                 static LLColor4 purple6;
00203 
00204                 static LLColor4 pink1;
00205                 static LLColor4 pink2;
00206 
00207                 static LLColor4 cyan1;
00208                 static LLColor4 cyan2;
00209                 static LLColor4 cyan3;
00210                 static LLColor4 cyan4;
00211                 static LLColor4 cyan5;
00212                 static LLColor4 cyan6;
00213         
00214                 static BOOL parseColor(const char* buf, LLColor4* color);
00215                 static BOOL parseColor4(const char* buf, LLColor4* color);
00216 
00217                 inline void clamp();
00218 };
00219 
00220 
00221 // Non-member functions 
00222 F32             distVec(const LLColor4 &a, const LLColor4 &b);                  // Returns distance between a and b
00223 F32             distVec_squared(const LLColor4 &a, const LLColor4 &b);  // Returns distance squared between a and b
00224 LLColor3        vec4to3(const LLColor4 &vec);
00225 LLColor4        vec3to4(const LLColor3 &vec);
00226 LLColor4 lerp(const LLColor4 &a, const LLColor4 &b, F32 u);
00227 
00228 inline LLColor4::LLColor4(void)
00229 {
00230         mV[VX] = 0.f;
00231         mV[VY] = 0.f;
00232         mV[VZ] = 0.f;
00233         mV[VW] = 1.f;
00234 }
00235 
00236 inline LLColor4::LLColor4(const LLSD& sd)
00237 {
00238         *this = sd;
00239 }
00240 
00241 inline LLColor4::LLColor4(F32 r, F32 g, F32 b)
00242 {
00243         mV[VX] = r;
00244         mV[VY] = g;
00245         mV[VZ] = b;
00246         mV[VW] = 1.f;
00247 }
00248 
00249 inline LLColor4::LLColor4(F32 r, F32 g, F32 b, F32 a)
00250 {
00251         mV[VX] = r;
00252         mV[VY] = g;
00253         mV[VZ] = b;
00254         mV[VW] = a;
00255 }
00256 
00257 inline LLColor4::LLColor4(U32 clr)
00258 {
00259         mV[VX] = (clr&0xff) * (1.0f/255.0f);
00260         mV[VY] = ((clr>>8)&0xff) * (1.0f/255.0f);
00261         mV[VZ] = ((clr>>16)&0xff) * (1.0f/255.0f);
00262         mV[VW] = (clr>>24) * (1.0f/255.0f);
00263 }
00264 
00265 
00266 inline LLColor4::LLColor4(const F32 *vec)
00267 {
00268         mV[VX] = vec[VX];
00269         mV[VY] = vec[VY];
00270         mV[VZ] = vec[VZ];
00271         mV[VW] = vec[VW];
00272 }
00273 
00274 inline const LLColor4&  LLColor4::setToBlack(void)
00275 {
00276         mV[VX] = 0.f;
00277         mV[VY] = 0.f;
00278         mV[VZ] = 0.f;
00279         mV[VW] = 1.f;
00280         return (*this);
00281 }
00282 
00283 inline const LLColor4&  LLColor4::setToWhite(void)
00284 {
00285         mV[VX] = 1.f;
00286         mV[VY] = 1.f;
00287         mV[VZ] = 1.f;
00288         mV[VW] = 1.f;
00289         return (*this);
00290 }
00291 
00292 inline const LLColor4&  LLColor4::setVec(F32 x, F32 y, F32 z)
00293 {
00294         mV[VX] = x;
00295         mV[VY] = y;
00296         mV[VZ] = z;
00297 
00298 //  no change to alpha!
00299 //      mV[VW] = 1.f;  
00300 
00301         return (*this);
00302 }
00303 
00304 inline const LLColor4&  LLColor4::setVec(F32 x, F32 y, F32 z, F32 a)
00305 {
00306         mV[VX] = x;
00307         mV[VY] = y;
00308         mV[VZ] = z;
00309         mV[VW] = a;  
00310         return (*this);
00311 }
00312 
00313 inline const LLColor4&  LLColor4::setVec(const LLColor4 &vec)
00314 {
00315         mV[VX] = vec.mV[VX];
00316         mV[VY] = vec.mV[VY];
00317         mV[VZ] = vec.mV[VZ];
00318         mV[VW] = vec.mV[VW];
00319         return (*this);
00320 }
00321 
00322 
00323 inline const LLColor4&  LLColor4::setVec(const F32 *vec)
00324 {
00325         mV[VX] = vec[VX];
00326         mV[VY] = vec[VY];
00327         mV[VZ] = vec[VZ];
00328         mV[VW] = vec[VW];
00329         return (*this);
00330 }
00331 
00332 inline const LLColor4&  LLColor4::setAlpha(F32 a)
00333 {
00334         mV[VW] = a;
00335         return (*this);
00336 }
00337 
00338 // LLColor4 Magnitude and Normalization Functions
00339 
00340 inline F32              LLColor4::magVec(void) const
00341 {
00342         return fsqrtf(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]);
00343 }
00344 
00345 inline F32              LLColor4::magVecSquared(void) const
00346 {
00347         return mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ];
00348 }
00349 
00350 inline F32              LLColor4::normVec(void)
00351 {
00352         F32 mag = fsqrtf(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]);
00353         F32 oomag;
00354 
00355         if (mag)
00356         {
00357                 oomag = 1.f/mag;
00358                 mV[VX] *= oomag;
00359                 mV[VY] *= oomag;
00360                 mV[VZ] *= oomag;
00361         }
00362         return (mag);
00363 }
00364 
00365 // LLColor4 Operators
00366 
00367 
00368 inline LLColor4 operator+(const LLColor4 &a, const LLColor4 &b)
00369 {
00370         return LLColor4(
00371                 a.mV[VX] + b.mV[VX],
00372                 a.mV[VY] + b.mV[VY],
00373                 a.mV[VZ] + b.mV[VZ],
00374                 a.mV[VW] + b.mV[VW]);
00375 }
00376 
00377 inline LLColor4 operator-(const LLColor4 &a, const LLColor4 &b)
00378 {
00379         return LLColor4(
00380                 a.mV[VX] - b.mV[VX],
00381                 a.mV[VY] - b.mV[VY],
00382                 a.mV[VZ] - b.mV[VZ],
00383                 a.mV[VW] - b.mV[VW]);
00384 }
00385 
00386 inline LLColor4  operator*(const LLColor4 &a, const LLColor4 &b)
00387 {
00388         return LLColor4(
00389                 a.mV[VX] * b.mV[VX],
00390                 a.mV[VY] * b.mV[VY],
00391                 a.mV[VZ] * b.mV[VZ],
00392                 a.mV[VW] * b.mV[VW]);
00393 }
00394 
00395 inline LLColor4 operator*(const LLColor4 &a, F32 k)
00396 {       
00397         // only affects rgb (not a!)
00398         return LLColor4(
00399                 a.mV[VX] * k,
00400                 a.mV[VY] * k,
00401                 a.mV[VZ] * k,
00402                 a.mV[VW]);
00403 }
00404 
00405 inline LLColor4 operator*(F32 k, const LLColor4 &a)
00406 {
00407         // only affects rgb (not a!)
00408         return LLColor4(
00409                 a.mV[VX] * k,
00410                 a.mV[VY] * k,
00411                 a.mV[VZ] * k,
00412                 a.mV[VW]);
00413 }
00414 
00415 inline LLColor4 operator%(F32 k, const LLColor4 &a)
00416 {
00417         // only affects alpha (not rgb!)
00418         return LLColor4(
00419                 a.mV[VX],
00420                 a.mV[VY],
00421                 a.mV[VZ],
00422                 a.mV[VW] * k);
00423 }
00424 
00425 inline LLColor4 operator%(const LLColor4 &a, F32 k)
00426 {
00427         // only affects alpha (not rgb!)
00428         return LLColor4(
00429                 a.mV[VX],
00430                 a.mV[VY],
00431                 a.mV[VZ],
00432                 a.mV[VW] * k);
00433 }
00434 
00435 inline bool operator==(const LLColor4 &a, const LLColor4 &b)
00436 {
00437         return (  (a.mV[VX] == b.mV[VX])
00438                         &&(a.mV[VY] == b.mV[VY])
00439                         &&(a.mV[VZ] == b.mV[VZ])
00440                         &&(a.mV[VW] == b.mV[VW]));
00441 }
00442 
00443 inline bool operator!=(const LLColor4 &a, const LLColor4 &b)
00444 {
00445         return (  (a.mV[VX] != b.mV[VX])
00446                         ||(a.mV[VY] != b.mV[VY])
00447                         ||(a.mV[VZ] != b.mV[VZ])
00448                         ||(a.mV[VW] != b.mV[VW]));
00449 }
00450 
00451 inline const LLColor4& operator+=(LLColor4 &a, const LLColor4 &b)
00452 {
00453         a.mV[VX] += b.mV[VX];
00454         a.mV[VY] += b.mV[VY];
00455         a.mV[VZ] += b.mV[VZ];
00456         a.mV[VW] += b.mV[VW];
00457         return a;
00458 }
00459 
00460 inline const LLColor4& operator-=(LLColor4 &a, const LLColor4 &b)
00461 {
00462         a.mV[VX] -= b.mV[VX];
00463         a.mV[VY] -= b.mV[VY];
00464         a.mV[VZ] -= b.mV[VZ];
00465         a.mV[VW] -= b.mV[VW];
00466         return a;
00467 }
00468 
00469 inline const LLColor4& operator*=(LLColor4 &a, F32 k)
00470 {
00471         // only affects rgb (not a!)
00472         a.mV[VX] *= k;
00473         a.mV[VY] *= k;
00474         a.mV[VZ] *= k;
00475         return a;
00476 }
00477 
00478 inline const LLColor4& operator *=(LLColor4 &a, const LLColor4 &b)
00479 {
00480         a.mV[VX] *= b.mV[VX];
00481         a.mV[VY] *= b.mV[VY];
00482         a.mV[VZ] *= b.mV[VZ];
00483 //      a.mV[VW] *= b.mV[VW];
00484         return a;
00485 }
00486 
00487 inline const LLColor4& operator%=(LLColor4 &a, F32 k)
00488 {
00489         // only affects alpha (not rgb!)
00490         a.mV[VW] *= k;
00491         return a;
00492 }
00493 
00494 
00495 // Non-member functions
00496 
00497 inline F32              distVec(const LLColor4 &a, const LLColor4 &b)
00498 {
00499         LLColor4 vec = a - b;
00500         return (vec.magVec());
00501 }
00502 
00503 inline F32              distVec_squared(const LLColor4 &a, const LLColor4 &b)
00504 {
00505         LLColor4 vec = a - b;
00506         return (vec.magVecSquared());
00507 }
00508 
00509 inline LLColor4 lerp(const LLColor4 &a, const LLColor4 &b, F32 u)
00510 {
00511         return LLColor4(
00512                 a.mV[VX] + (b.mV[VX] - a.mV[VX]) * u,
00513                 a.mV[VY] + (b.mV[VY] - a.mV[VY]) * u,
00514                 a.mV[VZ] + (b.mV[VZ] - a.mV[VZ]) * u,
00515                 a.mV[VW] + (b.mV[VW] - a.mV[VW]) * u);
00516 }
00517 
00518 
00519 void LLColor4::clamp()
00520 {
00521         // Clamp the color...
00522         if (mV[0] < 0.f)
00523         {
00524                 mV[0] = 0.f;
00525         }
00526         else if (mV[0] > 1.f)
00527         {
00528                 mV[0] = 1.f;
00529         }
00530         if (mV[1] < 0.f)
00531         {
00532                 mV[1] = 0.f;
00533         }
00534         else if (mV[1] > 1.f)
00535         {
00536                 mV[1] = 1.f;
00537         }
00538         if (mV[2] < 0.f)
00539         {
00540                 mV[2] = 0.f;
00541         }
00542         else if (mV[2] > 1.f)
00543         {
00544                 mV[2] = 1.f;
00545         }
00546         if (mV[3] < 0.f)
00547         {
00548                 mV[3] = 0.f;
00549         }
00550         else if (mV[3] > 1.f)
00551         {
00552                 mV[3] = 1.f;
00553         }
00554 }
00555 
00556 inline const LLColor4& LLColor4::operator=(const LLSD& sd)
00557 {
00558         mV[0] = (F32) sd[0].asReal();
00559         mV[1] = (F32) sd[1].asReal();
00560         mV[2] = (F32) sd[2].asReal();
00561         mV[3] = (F32) sd[3].asReal();
00562 
00563         return *this;
00564 }
00565 
00566 #endif
00567 

Generated on Fri May 16 08:32:20 2008 for SecondLife by  doxygen 1.5.5