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

Generated on Thu Jul 1 06:10:00 2010 for Second Life Viewer by  doxygen 1.4.7