• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

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 <boost/cstdint.hpp>
00008 #include <Gosu/Platform.hpp>
00009 
00010 namespace Gosu
00011 {
00018     class Color
00019     {
00020         boost::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 boost::uint8_t Channel;
00029         static const unsigned GL_FORMAT = 0x1908; // GL_RGBA
00030         
00032         Color()
00033         {
00034         }
00035         
00037         Color(boost::uint32_t 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         boost::uint32_t argb() const
00125         {
00126             return alpha() << 24 | red() << 16 | green() << 8 | blue();
00127         }
00128 
00130         boost::uint32_t bgr() const
00131         {
00132             return blue() << 16 | green() << 8 | red();
00133         }
00134 
00136         boost::uint32_t abgr() const
00137         {
00138             return alpha() << 24 | blue() << 16 | green() << 8 | red();
00139         }
00140         
00141         static const Color NONE;
00142         static const Color BLACK;
00143         static const Color GRAY;
00144         static const Color WHITE;
00145         
00146         static const Color AQUA;
00147         static const Color RED;
00148         static const Color GREEN;
00149         static const Color BLUE;
00150         static const Color YELLOW;
00151         static const Color FUCHSIA;
00152         static const Color CYAN;
00153     };
00154     
00155     // Causes weird errors when included in the SWIG wrapping process.
00156     // If, with a future version of SWIG, this can be included and
00157     // require 'gosu'; include Gosu
00158     // works from within Ruby, the #ifndef guard can be removed.
00159     #ifndef SWIG
00160     inline bool operator<(Color a, Color b)
00161     {
00162         return a.argb() < b.argb();
00163     }
00164     
00165     inline bool operator==(Color a, Color b)
00166     {
00167         return a.argb() == b.argb();
00168     }
00169 
00170     inline bool operator!=(Color a, Color b)
00171     {
00172         return a.argb() != b.argb();
00173     }
00174     #endif
00175 
00179     Color interpolate(Color a, Color b, double weight = 0.5);
00180     
00183     Color multiply(Color a, Color b);
00184 
00185     namespace Colors
00186     {
00187         const Color none    = 0x00000000;
00188         const Color black   = 0xff000000;
00189         const Color gray    = 0xff808080;
00190         const Color white   = 0xffffffff;
00191         
00192         const Color aqua    = 0xff00ffff;
00193         const Color red     = 0xffff0000;
00194         const Color green   = 0xff00ff00;
00195         const Color blue    = 0xff0000ff;
00196         const Color yellow  = 0xffffff00;
00197         const Color fuchsia = 0xffff00ff;
00198         const Color cyan    = 0xff00ffff;
00199     }
00200 }
00201 
00202 #endif

Documentation not clear enough? Please go to one of the places listed on http://www.libgosu.org/ and leave feedback. Thanks!