Color.hpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 #ifndef GOSU_COLOR_HPP
00005 #define GOSU_COLOR_HPP
00006 
00007 #include <Gosu/Platform.hpp>
00008 #include <Gosu/TR1.hpp>
00009 
00010 namespace Gosu
00011 {
00018     class Color
00019     {
00020         std::tr1::uint32_t rep;
00021         #ifdef GOSU_IS_LITTLE_ENDIAN
00022         enum { RED_OFFSET = 0, GREEN_OFFSET = 8, BLUE_OFFSET = 16, ALPHA_OFFSET = 24 };
00023         #else
00024         enum { RED_OFFSET = 24, GREEN_OFFSET = 16, BLUE_OFFSET = 8, ALPHA_OFFSET = 0 };
00025         #endif
00026         
00027     public:
00028         typedef std::tr1::uint8_t Channel;
00029         static const unsigned GL_FORMAT = 0x1908; // GL_RGBA
00030         
00032         Color()
00033         {
00034         }
00035         
00037         Color(unsigned argb)
00038         {
00039             *this = Color((argb >> 24) & 0xff, (argb >> 16) & 0xff,
00040                           (argb >>  8) & 0xff, (argb >>  0) & 0xff);
00041         }
00042         
00043         Color(Channel red, Channel green, Channel blue)
00044         {
00045             *this = Color(0xff, red, green, blue);
00046         }
00047         
00048         Color(Channel alpha, Channel red, Channel green, Channel blue)
00049         {
00050             rep = (alpha << ALPHA_OFFSET) | (red << RED_OFFSET) |
00051                   (green << GREEN_OFFSET) | (blue << BLUE_OFFSET);
00052         }
00053         
00058         static Color fromHSV(double h, double s, double v);
00059         static Color fromAHSV(Channel alpha, double h, double s, double v);
00060 
00061         Channel red() const
00062         {
00063             return static_cast<Channel>(rep >> RED_OFFSET);
00064         }
00065 
00066         Channel green() const
00067         {
00068             return static_cast<Channel>(rep >> GREEN_OFFSET);
00069         }
00070 
00071         Channel blue() const
00072         {
00073             return static_cast<Channel>(rep >> BLUE_OFFSET);
00074         }
00075 
00076         Channel alpha() const
00077         {
00078             return static_cast<Channel>(rep >> ALPHA_OFFSET);
00079         }
00080 
00081         void setRed(Channel value)
00082         {
00083             rep &= ~(0xff << RED_OFFSET);
00084             rep |= value << RED_OFFSET;
00085         }
00086 
00087         void setGreen(Channel value)
00088         {
00089             rep &= ~(0xff << GREEN_OFFSET);
00090             rep |= value << GREEN_OFFSET;
00091         }
00092 
00093         void setBlue(Channel value)
00094         {
00095             rep &= ~(0xff << BLUE_OFFSET);
00096             rep |= value << BLUE_OFFSET;
00097         }
00098 
00099         void setAlpha(Channel value)
00100         {
00101             rep &= ~(0xff << ALPHA_OFFSET);
00102             rep |= value << ALPHA_OFFSET;
00103         }
00104 
00106         double hue() const;
00107         
00109         void setHue(double h);
00110         
00112         double saturation() const;
00113         
00115         void setSaturation(double s);
00116         
00118         double value() const;
00119         
00121         void setValue(double v);
00122 
00124         std::tr1::uint32_t argb() const
00125         {
00126             return alpha() << 24 | red() << 16 | green() << 8 | blue();
00127         }
00128 
00130         std::tr1::uint32_t bgr() const
00131         {
00132             return blue() << 16 | green() << 8 | red();
00133         }
00134 
00136         std::tr1::uint32_t abgr() const
00137         {
00138             return alpha() << 24 | blue() << 16 | green() << 8 | red();
00139         }
00140         
00142         std::tr1::uint32_t gl() const
00143         {
00144             return rep;
00145         }
00146         
00147         static const Color NONE;
00148         static const Color BLACK;
00149         static const Color GRAY;
00150         static const Color WHITE;
00151         
00152         static const Color AQUA;
00153         static const Color RED;
00154         static const Color GREEN;
00155         static const Color BLUE;
00156         static const Color YELLOW;
00157         static const Color FUCHSIA;
00158         static const Color CYAN;
00159     };
00160     
00161     #ifndef SWIG
00162     inline bool operator<(Color a, Color b)
00163     {
00164         return a.gl() < b.gl();
00165     }
00166     
00167     inline bool operator==(Color a, Color b)
00168     {
00169         return a.gl() == b.gl();
00170     }
00171 
00172     inline bool operator!=(Color a, Color b)
00173     {
00174         return a.gl() != b.gl();
00175     }
00176     #endif
00177 
00181     Color interpolate(Color a, Color b, double weight = 0.5);
00182     
00185     Color multiply(Color a, Color b);
00186 
00187     namespace Colors
00188     {
00189         const Color none    = 0x00000000;
00190         const Color black   = 0xff000000;
00191         const Color gray    = 0xff808080;
00192         const Color white   = 0xffffffff;
00193         
00194         const Color aqua    = 0xff00ffff;
00195         const Color red     = 0xffff0000;
00196         const Color green   = 0xff00ff00;
00197         const Color blue    = 0xff0000ff;
00198         const Color yellow  = 0xffffff00;
00199         const Color fuchsia = 0xffff00ff;
00200         const Color cyan    = 0xff00ffff;
00201     }
00202 }
00203 
00204 #endif