00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 #ifndef __SP_MATERIAL_COLOR_H__
00009 #define __SP_MATERIAL_COLOR_H__
00010 
00011 
00012 #include "Base/spStandard.hpp"
00013 #include "Base/spDimensionVector3D.hpp"
00014 #include "Base/spInputOutputString.hpp"
00015 
00016 
00017 namespace sp
00018 {
00019 namespace video
00020 {
00021 
00022 
00023 inline u32 getAlpha(u32 Color)
00024 {
00025     return Color >> 24;
00026 }
00027 inline u32 getRed(u32 Color)
00028 {
00029     return (Color >> 16) & 0xFF;
00030 }
00031 inline u32 getGreen(u32 Color)
00032 {
00033     return (Color >> 8) & 0xFF;
00034 }
00035 inline u32 getBlue(u32 Color)
00036 {
00037     return Color & 0xFF;
00038 }
00039 
00040 inline u32 getColor(u32 Red, u32 Green, u32 Blue, u32 Alpha)
00041 {
00042     return ((Alpha & 0xFF) << 24) | ((Red & 0xFF) << 16) | ((Green & 0xFF) << 8) | (Blue & 0xFF);
00043 }
00044 
00045 
00046 class color
00047 {
00048     
00049     public:
00050         
00051         color() :
00052             Red     (255),
00053             Green   (255),
00054             Blue    (255),
00055             Alpha   (255)
00056         {
00057         }
00058         color(u8 Brightness) :
00059             Red     (Brightness ),
00060             Green   (Brightness ),
00061             Blue    (Brightness ),
00062             Alpha   (255        )
00063         {
00064         }
00065         color(u8 R, u8 G, u8 B, u8 A = 255) :
00066             Red     (R),
00067             Green   (G),
00068             Blue    (B),
00069             Alpha   (A)
00070         {
00071         }
00072         color(const color &other) :
00073             Red     (other.Red  ),
00074             Green   (other.Green),
00075             Blue    (other.Blue ),
00076             Alpha   (other.Alpha)
00077         {
00078         }
00079         color(const color &other, u8 A) :
00080             Red     (other.Red  ),
00081             Green   (other.Green),
00082             Blue    (other.Blue ),
00083             Alpha   (A          )
00084         {
00085         }
00086         color(const dim::vector3df &Color, bool Adjust = true) :
00087             Red     (255),
00088             Green   (255),
00089             Blue    (255),
00090             Alpha   (255)
00091         {
00092             if (Adjust)
00093             {
00094                 Red     = static_cast<u8>(Color.X * 255);
00095                 Green   = static_cast<u8>(Color.Y * 255);
00096                 Blue    = static_cast<u8>(Color.Z * 255);
00097                 Alpha   = 255;
00098             }
00099             else
00100             {
00101                 Red     = static_cast<u8>(Color.X);
00102                 Green   = static_cast<u8>(Color.Y);
00103                 Blue    = static_cast<u8>(Color.Z);
00104                 Alpha   = 255;
00105             }
00106         }
00107         color(const dim::vector4df &Color, bool Adjust = true) :
00108             Red     (255),
00109             Green   (255),
00110             Blue    (255),
00111             Alpha   (255)
00112         {
00113             if (Adjust)
00114             {
00115                 Red     = static_cast<u8>(Color.X * 255);
00116                 Green   = static_cast<u8>(Color.Y * 255);
00117                 Blue    = static_cast<u8>(Color.Z * 255);
00118                 Alpha   = static_cast<u8>(Color.W * 255);
00119             }
00120             else
00121             {
00122                 Red     = static_cast<u8>(Color.X);
00123                 Green   = static_cast<u8>(Color.Y);
00124                 Blue    = static_cast<u8>(Color.Z);
00125                 Alpha   = static_cast<u8>(Color.W);
00126             }
00127         }
00128         ~color()
00129         {
00130         }
00131         
00132         
00133         
00134         inline bool operator == (const color &other) const
00135         {
00136             return Red == other.Red && Green == other.Green && Blue == other.Blue && Alpha == other.Alpha;
00137         }
00138         inline bool operator != (const color &other) const
00139         {
00140             return Red != other.Red || Green != other.Green || Blue != other.Blue || Alpha != other.Alpha;
00141         }
00142         
00143         
00144         
00145         inline color operator + (const color &other) const
00146         {
00147             return color(Red + other.Red, Green + other.Green, Blue + other.Blue, Alpha + other.Alpha);
00148         }
00149         inline color& operator += (const color &other)
00150         {
00151             Red += other.Red; Green += other.Green; Blue += other.Blue; Alpha += other.Alpha; return *this;
00152         }
00153         
00154         inline color operator - (const color &other) const
00155         {
00156             return color(Red - other.Red, Green - other.Green, Blue - other.Blue, Alpha - other.Alpha);
00157         }
00158         inline color& operator -= (const color &other)
00159         {
00160             Red -= other.Red; Green -= other.Green; Blue -= other.Blue; Alpha -= other.Alpha; return *this;
00161         }
00162         
00163         inline color operator / (const color &other) const
00164         {
00165             return color(Red/other.Red, Green/other.Green, Blue/other.Blue, Alpha/other.Alpha);
00166         }
00167         inline color& operator /= (const color &other)
00168         {
00169             Red /= other.Red; Green /= other.Green; Blue /= other.Blue; Alpha /= other.Alpha; return *this;
00170         }
00171         
00172         inline color operator / (u8 Factor) const
00173         {
00174             return color(Red/Factor, Green/Factor, Blue/Factor, Alpha);
00175         }
00176         inline color& operator /= (u8 Factor)
00177         {
00178             Red /= Factor; Green /= Factor; Blue /= Factor; return *this;
00179         }
00180         
00181         inline color operator * (const color &other) const
00182         {
00183             return color(Red*other.Red, Green*other.Green, Blue*other.Blue, Alpha*other.Alpha);
00184         }
00185         inline color& operator *= (const color &other)
00186         {
00187             Red *= other.Red; Green *= other.Green; Blue *= other.Blue; Alpha *= other.Alpha; return *this;
00188         }
00189         
00190         inline color operator * (u8 Factor) const
00191         {
00192             return color(Red*Factor, Green*Factor, Blue*Factor, Alpha);
00193         }
00194         inline color& operator *= (u8 Factor)
00195         {
00196             Red *= Factor; Green *= Factor; Blue *= Factor; return *this;
00197         }
00198         
00199         inline color operator * (f32 Factor) const
00200         {
00201             return color(u8(f32(Red)*Factor), u8(f32(Green)*Factor), u8(f32(Blue)*Factor), Alpha);
00202         }
00203         inline color& operator *= (f32 Factor)
00204         {
00205             Red = u8(f32(Red)*Factor); Green = u8(f32(Green)*Factor); Blue = u8(f32(Blue)*Factor);
00206             return *this;
00207         }
00208         
00209         
00210         
00211         inline const u8 operator [] (s32 i) const
00212         {
00213             switch (i)
00214             {
00215                 case 0: return Red;
00216                 case 1: return Green;
00217                 case 2: return Blue;
00218                 case 3: return Alpha;
00219             }
00220             return 0;
00221         }
00222         
00223         inline u8& operator [] (s32 i)
00224         {
00225             return *(&Red + i);
00226         }
00227         
00228         
00229         
00230         inline color getInverse() const
00231         {
00232             return color(255 - Red, 255 - Green, 255 - Blue, 255 - Alpha);
00233         }
00234         inline color& setInverse()
00235         {
00236             Red = 255 - Red, Green = 255 - Green, Blue = 255 - Blue, Alpha = 255 -Alpha;
00237             return *this;
00238         }
00239         
00240         inline color getInverse(
00241             bool isRedInverse, bool isGreenInverse, bool isBlueInverse, bool isAlphaInverse = false) const
00242         {
00243             return color(
00244                 isRedInverse    ? 255 - Red     : Red,
00245                 isGreenInverse  ? 255 - Green   : Green,
00246                 isBlueInverse   ? 255 - Blue    : Blue,
00247                 isAlphaInverse  ? 255 - Alpha   : Alpha
00248             );
00249         }
00250         inline color& setInverse(
00251             bool isRedInverse, bool isGreenInverse, bool isBlueInverse, bool isAlphaInverse = false)
00252         {
00253             if (isRedInverse    ) Red   = 255 - Red;
00254             if (isGreenInverse  ) Green = 255 - Green;
00255             if (isBlueInverse   ) Blue  = 255 - Blue;
00256             if (isAlphaInverse  ) Alpha = 255 - Alpha;
00257             return *this;
00258         }
00259         
00265         inline void setFloatArray(const f32* ColorArray)
00266         {
00267             if (ColorArray)
00268             {
00269                 Red     = static_cast<u8>(ColorArray[0] * 255);
00270                 Green   = static_cast<u8>(ColorArray[1] * 255);
00271                 Blue    = static_cast<u8>(ColorArray[2] * 255);
00272                 Alpha   = static_cast<u8>(ColorArray[3] * 255);
00273             }
00274         }
00285         inline void getFloatArray(f32* ColorArray) const
00286         {
00287             if (ColorArray)
00288             {
00289                 ColorArray[0] = static_cast<f32>(Red    ) / 255;
00290                 ColorArray[1] = static_cast<f32>(Green  ) / 255;
00291                 ColorArray[2] = static_cast<f32>(Blue   ) / 255;
00292                 ColorArray[3] = static_cast<f32>(Alpha  ) / 255;
00293             }
00294         }
00295         
00296         inline void setIntensity(const color &other)
00297         {
00298             Red     = u8( static_cast<s32>(Red     ) * static_cast<s32>(other.Red  ) / 255 );
00299             Green   = u8( static_cast<s32>(Green   ) * static_cast<s32>(other.Green) / 255 );
00300             Blue    = u8( static_cast<s32>(Blue    ) * static_cast<s32>(other.Blue ) / 255 );
00301             Alpha   = u8( static_cast<s32>(Alpha   ) * static_cast<s32>(other.Alpha) / 255 );
00302         }
00303         
00304         inline color getIntensity(const color &other) const
00305         {
00306             color clr(*this);
00307             clr.setIntensity(other);
00308             return clr;
00309         }
00310         
00311         inline void setIntensity(f32 Intensity)
00312         {
00313             Red     = static_cast<u8>(Intensity * Red   );
00314             Green   = static_cast<u8>(Intensity * Green );
00315             Blue    = static_cast<u8>(Intensity * Blue  );
00316             Alpha   = static_cast<u8>(Intensity * Alpha );
00317         }
00318         
00319         inline color getIntensity(f32 Intensity) const
00320         {
00321             color clr(*this);
00322             clr.setIntensity(Intensity);
00323             return clr;
00324         }
00325         
00326         inline void set(u8 R, u8 G, u8 B, u8 A)
00327         {
00328             Red = R; Green = G; Blue = B; Alpha = A;
00329         }
00330         
00331         inline u32 getSingle() const
00332         {
00333             return getColor(Red, Green, Blue, Alpha);
00334         }
00335         inline void setSingle(u32 Color)
00336         {
00337             Red     = getRed    (Color);
00338             Green   = getGreen  (Color);
00339             Blue    = getBlue   (Color);
00340             Alpha   = getAlpha  (Color);
00341         }
00342         
00343         inline bool equal(const color &other, bool isIncludingAlpha = true) const
00344         {
00345             return Red == other.Red && Green == other.Green && Blue == other.Blue && ( !isIncludingAlpha || Alpha == other.Alpha );
00346         }
00347         
00349         inline bool gray() const
00350         {
00351             return Red == Green && Green == Blue;
00352         }
00353         
00354         inline dim::vector3df getVector(bool Adjust = false) const
00355         {
00356             return (Adjust
00357                 ? dim::vector3df(static_cast<f32>(Red), static_cast<f32>(Green), static_cast<f32>(Blue)) / 255.0f
00358                 : dim::vector3df(static_cast<f32>(Red), static_cast<f32>(Green), static_cast<f32>(Blue))
00359             );
00360         }
00361         
00362         template <typename T> inline T getBrightness() const
00363         {
00364             return static_cast<T>( ( static_cast<s32>(Red) + static_cast<s32>(Green) + static_cast<s32>(Blue) ) / 3 );
00365         }
00366         
00367         
00368         
00369         static const color empty;   
00370         static const color red;     
00371         static const color green;   
00372         static const color blue;    
00373         static const color yellow;  
00374         static const color pink;    
00375         static const color black;   
00376         static const color white;   
00377         
00378         
00379         
00392         static color fromName(io::stringc Name)
00393         {
00394             if (!Name.size())
00395                 return empty;
00396             
00397             Name.makeLower();
00398             
00399             if (Name[0] == '#')
00400             {
00401                 video::color Color;
00402                 
00403                 if (Name.size() == 7 || Name.size() == 9)
00404                 {
00405                     Color.Red = io::getHexNumber<u8>(Name.mid(1, 2));
00406                     Color.Green = io::getHexNumber<u8>(Name.mid(3, 2));
00407                     Color.Blue = io::getHexNumber<u8>(Name.mid(5, 2));
00408                     
00409                     if (Name.size() == 9)
00410                         Color.Alpha = io::getHexNumber<u8>(Name.mid(7, 2));
00411                     
00412                     return Color;
00413                 }
00414                 
00415                 return empty;
00416             }
00417             
00418             if (Name == "red"   ) return red;
00419             if (Name == "green" ) return green;
00420             if (Name == "blue"  ) return blue;
00421             if (Name == "yellow") return yellow;
00422             if (Name == "black" ) return black;
00423             if (Name == "white" ) return white;
00424             
00425             return empty;
00426         }
00427         
00428         
00429         
00430         u8 Red, Green, Blue, Alpha;
00431         
00432 };
00433 
00434 
00435 } 
00436 
00437 } 
00438 
00439 
00440 #endif
00441 
00442 
00443 
00444