Color.hpp
Go to the documentation of this file.
1 
2 
3 
4 #ifndef GOSU_COLOR_HPP
5 #define GOSU_COLOR_HPP
6 
7 #include <Gosu/Platform.hpp>
8 #include <Gosu/TR1.hpp>
9 
10 namespace Gosu
11 {
18  class Color
19  {
20  std::tr1::uint32_t rep;
21  #ifdef GOSU_IS_LITTLE_ENDIAN
22  enum { RED_OFFSET = 0, GREEN_OFFSET = 8, BLUE_OFFSET = 16, ALPHA_OFFSET = 24 };
23  #else
24  enum { RED_OFFSET = 24, GREEN_OFFSET = 16, BLUE_OFFSET = 8, ALPHA_OFFSET = 0 };
25  #endif
26 
27  public:
28  typedef std::tr1::uint8_t Channel;
29  static const unsigned GL_FORMAT = 0x1908; // GL_RGBA
30 
33  {
34  }
35 
37  Color(unsigned argb)
38  {
39  *this = Color((argb >> 24) & 0xff, (argb >> 16) & 0xff,
40  (argb >> 8) & 0xff, (argb >> 0) & 0xff);
41  }
42 
44  {
45  *this = Color(0xff, red, green, blue);
46  }
47 
49  {
50  rep = (alpha << ALPHA_OFFSET) | (red << RED_OFFSET) |
51  (green << GREEN_OFFSET) | (blue << BLUE_OFFSET);
52  }
53 
58  static Color fromHSV(double h, double s, double v);
59  static Color fromAHSV(Channel alpha, double h, double s, double v);
60 
61  Channel red() const
62  {
63  return static_cast<Channel>(rep >> RED_OFFSET);
64  }
65 
66  Channel green() const
67  {
68  return static_cast<Channel>(rep >> GREEN_OFFSET);
69  }
70 
71  Channel blue() const
72  {
73  return static_cast<Channel>(rep >> BLUE_OFFSET);
74  }
75 
76  Channel alpha() const
77  {
78  return static_cast<Channel>(rep >> ALPHA_OFFSET);
79  }
80 
82  {
83  rep &= ~(0xff << RED_OFFSET);
84  rep |= value << RED_OFFSET;
85  }
86 
88  {
89  rep &= ~(0xff << GREEN_OFFSET);
90  rep |= value << GREEN_OFFSET;
91  }
92 
94  {
95  rep &= ~(0xff << BLUE_OFFSET);
96  rep |= value << BLUE_OFFSET;
97  }
98 
100  {
101  rep &= ~(0xff << ALPHA_OFFSET);
102  rep |= value << ALPHA_OFFSET;
103  }
104 
106  double hue() const;
107 
109  void setHue(double h);
110 
112  double saturation() const;
113 
115  void setSaturation(double s);
116 
118  double value() const;
119 
121  void setValue(double v);
122 
124  std::tr1::uint32_t argb() const
125  {
126  return alpha() << 24 | red() << 16 | green() << 8 | blue();
127  }
128 
130  std::tr1::uint32_t bgr() const
131  {
132  return blue() << 16 | green() << 8 | red();
133  }
134 
136  std::tr1::uint32_t abgr() const
137  {
138  return alpha() << 24 | blue() << 16 | green() << 8 | red();
139  }
140 
142  std::tr1::uint32_t gl() const
143  {
144  return rep;
145  }
146 
147  static const Color NONE;
148  static const Color BLACK;
149  static const Color GRAY;
150  static const Color WHITE;
151 
152  static const Color AQUA;
153  static const Color RED;
154  static const Color GREEN;
155  static const Color BLUE;
156  static const Color YELLOW;
157  static const Color FUCHSIA;
158  static const Color CYAN;
159  };
160 
161  #ifndef SWIG
162  inline bool operator<(Color a, Color b)
163  {
164  return a.gl() < b.gl();
165  }
166 
167  inline bool operator==(Color a, Color b)
168  {
169  return a.gl() == b.gl();
170  }
171 
172  inline bool operator!=(Color a, Color b)
173  {
174  return a.gl() != b.gl();
175  }
176  #endif
177 
181  Color interpolate(Color a, Color b, double weight = 0.5);
182 
185  Color multiply(Color a, Color b);
186 
187  namespace Colors
188  {
189  const Color none = 0x00000000;
190  const Color black = 0xff000000;
191  const Color gray = 0xff808080;
192  const Color white = 0xffffffff;
193 
194  const Color aqua = 0xff00ffff;
195  const Color red = 0xffff0000;
196  const Color green = 0xff00ff00;
197  const Color blue = 0xff0000ff;
198  const Color yellow = 0xffffff00;
199  const Color fuchsia = 0xffff00ff;
200  const Color cyan = 0xff00ffff;
201  }
202 }
203 
204 #endif