v3color.h

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

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