Color.hpp
Go to the documentation of this file.
1 
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  GOSU_DEPRECATED const Color none (0x00000000);
190  GOSU_DEPRECATED const Color black (0xff000000);
191  GOSU_DEPRECATED const Color gray (0xff808080);
192  GOSU_DEPRECATED const Color white (0xffffffff);
193 
194  GOSU_DEPRECATED const Color aqua (0xff00ffff);
195  GOSU_DEPRECATED const Color red (0xffff0000);
196  GOSU_DEPRECATED const Color green (0xff00ff00);
197  GOSU_DEPRECATED const Color blue (0xff0000ff);
198  GOSU_DEPRECATED const Color yellow (0xffffff00);
199  GOSU_DEPRECATED const Color fuchsia (0xffff00ff);
200  GOSU_DEPRECATED const Color cyan (0xff00ffff);
201  }
202 }
203 
204 #endif
Includes all parts of C++03 (TR1) that are relevant for Gosu.
GOSU_DEPRECATED const Color aqua(0xff00ffff)
static const Color CYAN
Definition: Color.hpp:158
GOSU_DEPRECATED const Color white(0xffffffff)
GOSU_DEPRECATED const Color green(0xff00ff00)
#define GOSU_DEPRECATED
Definition: Platform.hpp:85
GOSU_DEPRECATED const Color none(0x00000000)
GOSU_DEPRECATED const Color blue(0xff0000ff)
Color()
The default constructor does not initialize the color to any value.
Definition: Color.hpp:32
Represents an RGBA color value with 8 bits for each channel.
Definition: Color.hpp:18
static const Color YELLOW
Definition: Color.hpp:156
std::tr1::uint32_t abgr() const
Returns the color in 0xaabbggrr representation.
Definition: Color.hpp:136
static const Color BLUE
Definition: Color.hpp:155
static Color fromHSV(double h, double s, double v)
Constructs a color from the given hue/saturation/value triple.
void setBlue(Channel value)
Definition: Color.hpp:93
static const Color AQUA
Definition: Color.hpp:152
double saturation() const
Returns the saturation of the color, in the range of 0..1.
void setGreen(Channel value)
Definition: Color.hpp:87
double value() const
Returns the value (brightness) of the color, in the range of 0..1.
std::tr1::uint32_t bgr() const
Returns the color in 0x00bbggrr representation. Useful for Win32 programming.
Definition: Color.hpp:130
void setSaturation(double s)
Changes the current color so saturation() will return s.
static const Color RED
Definition: Color.hpp:153
Channel red() const
Definition: Color.hpp:61
std::tr1::uint8_t Channel
Definition: Color.hpp:28
GOSU_DEPRECATED const Color cyan(0xff00ffff)
GOSU_DEPRECATED const Color yellow(0xffffff00)
void setAlpha(Channel value)
Definition: Color.hpp:99
bool operator!=(Color a, Color b)
Definition: Color.hpp:172
GOSU_DEPRECATED const Color fuchsia(0xffff00ff)
Macros and utility functions to facilitate programming on all of Gosu&#39;s supported platforms...
std::tr1::uint32_t gl() const
Returns the internal representation of the color (RGBA in memory).
Definition: Color.hpp:142
Color interpolate(Color a, Color b, double weight=0.5)
Interpolates linearly between two colors, with a given weight towards the second color.
Channel alpha() const
Definition: Color.hpp:76
GOSU_DEPRECATED const Color gray(0xff808080)
static const Color BLACK
Definition: Color.hpp:148
Color(unsigned argb)
Conversion constructor for literals of the form 0xaarrggbb.
Definition: Color.hpp:37
double hue() const
Returns the hue of the color, in the usual range of 0..360.
bool operator==(Color a, Color b)
Definition: Color.hpp:167
Channel blue() const
Definition: Color.hpp:71
Channel green() const
Definition: Color.hpp:66
static Color fromAHSV(Channel alpha, double h, double s, double v)
Color(Channel alpha, Channel red, Channel green, Channel blue)
Definition: Color.hpp:48
static const unsigned GL_FORMAT
Definition: Color.hpp:29
std::tr1::uint32_t argb() const
Returns the color in 0xaarrggbb representation.
Definition: Color.hpp:124
void setHue(double h)
Changes the current color so hue() will return h.
Color(Channel red, Channel green, Channel blue)
Definition: Color.hpp:43
static const Color NONE
Definition: Color.hpp:147
static const Color GRAY
Definition: Color.hpp:149
GOSU_DEPRECATED const Color red(0xffff0000)
static const Color GREEN
Definition: Color.hpp:154
GOSU_DEPRECATED const Color black(0xff000000)
Color multiply(Color a, Color b)
Combines two colors as if their channels were mapped to the 0..1 range and then multiplied with each ...
static const Color WHITE
Definition: Color.hpp:150
void setValue(double v)
Changes the current color so value() will return v.
void setRed(Channel value)
Definition: Color.hpp:81
static const Color FUCHSIA
Definition: Color.hpp:157
bool operator<(Color a, Color b)
Definition: Color.hpp:162