KeosColor.h

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 
00022 #ifndef KEOS_COLOR_H
00023 #define KEOS_COLOR_H
00024 
00025 #include "KeosPrerequisites.h"
00026 
00027 namespace Keos
00028 {
00029   //-----------------------------------------------------------------------
00032   class CColor
00033   {
00034   public :
00035 
00039     CColor(ulong nColor = 0xFF000000)
00040     {
00041       m_nColor = nColor;
00042     }
00043 
00050     CColor(uchar r, uchar g, uchar b, uchar a = 0xFF)
00051     {
00052       Set(r, g, b, a);
00053     }
00054 
00058     ulong ToARGB() const
00059     {
00060       return (GetAlpha() << 24) | (GetRed() << 16) | (GetGreen() << 8) | (GetBlue() << 0);
00061     }
00062 
00066     ulong ToABGR() const
00067     {
00068       return (GetAlpha() << 24) | (GetBlue() << 16) | (GetGreen() << 8) | (GetRed() << 0);
00069     }
00070 
00074     ulong ToRGBA() const
00075     {
00076       //return (GetRed() << 24) | (GetGreen() << 16) | (GetBlue() << 8) | (GetAlpha() << 0);
00077       return m_nColor; 
00078     }
00079 
00083     uchar GetAlpha() const
00084     {
00085       return static_cast<uchar>((m_nColor & 0xFF000000) >> 24);
00086     }
00087 
00091     uchar GetRed() const
00092     {
00093       return static_cast<uchar>((m_nColor & 0x00FF0000) >> 16);
00094     }
00095 
00099     uchar GetGreen() const
00100     {
00101       return static_cast<uchar>((m_nColor & 0x0000FF00) >> 8);
00102     }
00103 
00107     uchar GetBlue() const
00108     {
00109       return static_cast<uchar>((m_nColor & 0x000000FF) >> 0);
00110     }
00111 
00118     void SetFloats(float r, float g, float b, float a = 1.0f)
00119     {
00120       int R = static_cast<int>(r * 255.0f);
00121       int G = static_cast<int>(g * 255.0f);
00122       int B = static_cast<int>(b * 255.0f);
00123       int A = static_cast<int>(a * 255.0f);
00124 
00125       SetInt(R, G, B, A);
00126     }
00127 
00134     void Set(uchar r, uchar g, uchar b, uchar a = 0xFF)
00135     {
00136       m_nColor = (a << 24) | (r << 16) | (g << 8) | (b << 0);
00137     }
00138 
00142     void ToFloat(float Dest[]) const
00143     {
00144       Dest[0] = GetRed()   / 255.0f;
00145       Dest[1] = GetGreen() / 255.0f;
00146       Dest[2] = GetBlue()  / 255.0f;
00147       Dest[3] = GetAlpha() / 255.0f;
00148     }
00149 
00152     bool operator ==(const CColor& c) const
00153     {
00154       return m_nColor == c.m_nColor;
00155     }
00156 
00159     bool operator !=(const CColor& c) const
00160     {
00161       return !(*this == c);
00162     }
00163 
00166     const CColor& operator +=(const CColor& c)
00167     {
00168       int R = GetRed()   + c.GetRed();
00169       int G = GetGreen() + c.GetGreen();
00170       int B = GetBlue()  + c.GetBlue();
00171       int A = GetAlpha() + c.GetAlpha();
00172 
00173       SetInt(R, G, B, A);
00174 
00175       return *this;
00176     }
00177 
00180     const CColor& operator -=(const CColor& c)
00181     {
00182       int R = GetRed()   - c.GetRed();
00183       int G = GetGreen() - c.GetGreen();
00184       int B = GetBlue()  - c.GetBlue();
00185       int A = GetAlpha() - c.GetAlpha();
00186 
00187       SetInt(R, G, B, A);
00188 
00189       return *this;
00190     }
00191 
00194     CColor operator +(const CColor& c) const
00195     {
00196       int R = GetRed()   + c.GetRed();
00197       int G = GetGreen() + c.GetGreen();
00198       int B = GetBlue()  + c.GetBlue();
00199       int A = GetAlpha() + c.GetAlpha();
00200 
00201       CColor Ret;
00202       Ret.SetInt(R, G, B, A);
00203 
00204       return Ret;
00205     }
00206 
00209     CColor operator -(const CColor& c) const
00210     {
00211       int R = GetRed()   - c.GetRed();
00212       int G = GetGreen() - c.GetGreen();
00213       int B = GetBlue()  - c.GetBlue();
00214       int A = GetAlpha() - c.GetAlpha();
00215 
00216       CColor Ret;
00217       Ret.SetInt(R, G, B, A);
00218 
00219       return Ret;
00220     }
00221 
00224     CColor operator *(float v) const
00225     {
00226       int R = static_cast<int>(GetRed()   * v);
00227       int G = static_cast<int>(GetGreen() * v);
00228       int B = static_cast<int>(GetBlue()  * v);
00229       int A = static_cast<int>(GetAlpha() * v);
00230 
00231       CColor Ret;
00232       Ret.SetInt(R, G, B, A);
00233 
00234       return Ret;
00235     }
00236 
00239     const CColor& operator *=(float v)
00240     {
00241       int R = static_cast<int>(GetRed()   * v);
00242       int G = static_cast<int>(GetGreen() * v);
00243       int B = static_cast<int>(GetBlue()  * v);
00244       int A = static_cast<int>(GetAlpha() * v);
00245 
00246       SetInt(R, G, B, A);
00247 
00248       return *this;
00249     }
00250 
00253     CColor operator /(float v) const
00254     {
00255       return *this * (1.0f / v);
00256     }
00257 
00260     const CColor& operator /=(float v)
00261     {
00262       return *this *= (1.0f / v);
00263     }
00264 
00268     CColor Add(const CColor& c) const
00269     {
00270       return *this + c;
00271     }
00272 
00276     CColor Modulate(const CColor& c) const
00277     {
00278       uchar R = static_cast<uchar>(GetRed()   * c.GetRed()   / 255);
00279       uchar G = static_cast<uchar>(GetGreen() * c.GetGreen() / 255);
00280       uchar B = static_cast<uchar>(GetBlue()  * c.GetBlue()  / 255);
00281       uchar A = static_cast<uchar>(GetAlpha() * c.GetAlpha() / 255);
00282 
00283       return CColor(R, G, B, A);
00284     }
00285 
00287     static const ulong White = 0xFFFFFFFF;
00289     static const ulong Black = 0xFF000000;
00291     static const ulong Red   = 0xFFFF0000;
00293     static const ulong Green = 0xFF00FF00;
00295     static const ulong Blue  = 0xFF0000FF;
00296 
00300     friend std::istream& operator >>(std::istream& Stream, CColor& Color)
00301     {
00302       int R, G, B, A;
00303       Stream >> R >> G >> B >> A;
00304       Color.SetInt(R, G, B, A);
00305 
00306       return Stream;
00307     }
00308 
00312     friend std::ostream& operator <<(std::ostream& Stream, const CColor& Color)
00313     {
00314       return Stream << (int)Color.GetRed() << " " << (int)Color.GetGreen() << " " << (int)Color.GetBlue() << " " << (int)Color.GetAlpha();
00315     }
00316 
00317   private :
00318 
00325     void SetInt(int r, int g, int b, int a = 0xFF)
00326     {
00327       uchar R = (r >= 0) ? (r <= 255 ? r : 255) : 0;
00328       uchar G = (g >= 0) ? (g <= 255 ? g : 255) : 0;
00329       uchar B = (b >= 0) ? (b <= 255 ? b : 255) : 0;
00330       uchar A = (a >= 0) ? (a <= 255 ? a : 255) : 0;
00331 
00332       Set(R, G, B);
00333     }
00334 
00336     ulong m_nColor;
00337   };
00338 
00339 } // namespace Keos
00340 
00341 
00342 #endif // KEOS_COLOR_H

Generated on Fri Mar 9 14:29:02 2007 for Keos by  doxygen 1.5.1-p1