Gosu
Color.hpp
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <Gosu/Platform.hpp>
7 #include <cstdint>
8 
9 namespace Gosu
10 {
16  class Color
17  {
18  std::uint32_t rep;
19  #ifdef GOSU_IS_LITTLE_ENDIAN
20  enum { RED_OFFSET = 0, GREEN_OFFSET = 8, BLUE_OFFSET = 16, ALPHA_OFFSET = 24 };
21  #else
22  enum { RED_OFFSET = 24, GREEN_OFFSET = 16, BLUE_OFFSET = 8, ALPHA_OFFSET = 0 };
23  #endif
24 
25  public:
26  typedef std::uint8_t Channel;
27  static const unsigned GL_FORMAT = 0x1908; // GL_RGBA
28 
31  {
32  }
33 
35  Color(unsigned argb)
36  : Color((argb >> 24) & 0xff, (argb >> 16) & 0xff, (argb >> 8) & 0xff, (argb >> 0) & 0xff)
37  {
38  }
39 
40  Color(Channel red, Channel green, Channel blue)
41  : Color(0xff, red, green, blue)
42  {
43  }
44 
45  Color(Channel alpha, Channel red, Channel green, Channel blue)
46  {
47  rep = (alpha << ALPHA_OFFSET) | (red << RED_OFFSET) |
48  (green << GREEN_OFFSET) | (blue << BLUE_OFFSET);
49  }
50 
53  static Color from_hsv(double h, double s, double v);
54 
57  static Color from_ahsv(Channel alpha, double h, double s, double v);
58 
59  Channel red() const
60  {
61  return static_cast<Channel>(rep >> RED_OFFSET);
62  }
63 
64  Channel green() const
65  {
66  return static_cast<Channel>(rep >> GREEN_OFFSET);
67  }
68 
69  Channel blue() const
70  {
71  return static_cast<Channel>(rep >> BLUE_OFFSET);
72  }
73 
74  Channel alpha() const
75  {
76  return static_cast<Channel>(rep >> ALPHA_OFFSET);
77  }
78 
79  void set_red(Channel value)
80  {
81  rep &= ~(0xff << RED_OFFSET);
82  rep |= value << RED_OFFSET;
83  }
84 
85  void set_green(Channel value)
86  {
87  rep &= ~(0xff << GREEN_OFFSET);
88  rep |= value << GREEN_OFFSET;
89  }
90 
91  void set_blue(Channel value)
92  {
93  rep &= ~(0xff << BLUE_OFFSET);
94  rep |= value << BLUE_OFFSET;
95  }
96 
97  void set_alpha(Channel value)
98  {
99  rep &= ~(0xff << ALPHA_OFFSET);
100  rep |= value << ALPHA_OFFSET;
101  }
102 
104  double hue() const;
105 
107  void set_hue(double h);
108 
110  double saturation() const;
111 
113  void set_saturation(double s);
114 
116  double value() const;
117 
119  void set_value(double v);
120 
122  std::uint32_t argb() const { return alpha() << 24 | red() << 16 | green() << 8 | blue(); }
123 
125  std::uint32_t bgr() const { return blue() << 16 | green() << 8 | red(); }
126 
128  std::uint32_t abgr() const { return alpha() << 24 | blue() << 16 | green() << 8 | red(); }
129 
131  std::uint32_t gl() const { return rep; }
132 
133  static const Color NONE;
134  static const Color BLACK;
135  static const Color GRAY;
136  static const Color WHITE;
137 
138  static const Color AQUA;
139  static const Color RED;
140  static const Color GREEN;
141  static const Color BLUE;
142  static const Color YELLOW;
143  static const Color FUCHSIA;
144  static const Color CYAN;
145  };
146 
147 #ifndef SWIG
148  inline bool operator<(Color a, Color b) { return a.gl() < b.gl(); }
149  inline bool operator==(Color a, Color b) { return a.gl() == b.gl(); }
150  inline bool operator!=(Color a, Color b) { return a.gl() != b.gl(); }
151 
155  Color interpolate(Color a, Color b, double weight = 0.5);
156 
159  Color multiply(Color a, Color b);
160 #endif
161 }
static const Color CYAN
Definition: Color.hpp:144
std::uint32_t bgr() const
Returns the color in 0x00bbggrr representation. Useful for Win32 programming.
Definition: Color.hpp:125
The library&#39;s main namespace.
Definition: Audio.hpp:12
std::uint8_t Channel
Definition: Color.hpp:26
void set_alpha(Channel value)
Definition: Color.hpp:97
Color()
The default constructor does not initialize the color to any value.
Definition: Color.hpp:30
void set_red(Channel value)
Definition: Color.hpp:79
Represents an RGBA color value with 8 bits for each channel.
Definition: Color.hpp:16
static const Color YELLOW
Definition: Color.hpp:142
void set_saturation(double s)
Changes the current color so saturation() will return s.
static const Color BLUE
Definition: Color.hpp:141
void set_blue(Channel value)
Definition: Color.hpp:91
Channel blue() const
Definition: Color.hpp:69
static Color from_hsv(double h, double s, double v)
Constructs a color from the given hue/saturation/value triple.
Channel alpha() const
Definition: Color.hpp:74
static const Color AQUA
Definition: Color.hpp:138
static const Color RED
Definition: Color.hpp:139
std::uint32_t gl() const
Returns the internal representation of the color (RGBA in memory).
Definition: Color.hpp:131
double saturation() const
Returns the saturation of the color, in the range of 0..1.
bool operator!=(Color a, Color b)
Definition: Color.hpp:150
static Color from_ahsv(Channel alpha, double h, double s, double v)
Constructs a color from the given hue/saturation/value triple.
Macros and utility functions to facilitate programming on all of Gosu&#39;s supported platforms...
Color interpolate(Color a, Color b, double weight=0.5)
Interpolates linearly between two colors, with a given weight towards the second color.
static const Color BLACK
Definition: Color.hpp:134
Color(unsigned argb)
Conversion constructor for literals of the form 0xaarrggbb.
Definition: Color.hpp:35
bool operator==(Color a, Color b)
Definition: Color.hpp:149
std::uint32_t argb() const
Returns the color in 0xaarrggbb representation.
Definition: Color.hpp:122
void set_hue(double h)
Changes the current color so hue() will return h.
Color(Channel alpha, Channel red, Channel green, Channel blue)
Definition: Color.hpp:45
static const unsigned GL_FORMAT
Definition: Color.hpp:27
double hue() const
Returns the hue of the color, in the usual range of 0..360.
Channel green() const
Definition: Color.hpp:64
double value() const
Returns the value (brightness) of the color, in the range of 0..1.
Color(Channel red, Channel green, Channel blue)
Definition: Color.hpp:40
void set_value(double v)
Changes the current color so value() will return v.
std::uint32_t abgr() const
Returns the color in 0xaabbggrr representation.
Definition: Color.hpp:128
Channel red() const
Definition: Color.hpp:59
static const Color NONE
Definition: Color.hpp:133
static const Color GRAY
Definition: Color.hpp:135
static const Color GREEN
Definition: Color.hpp:140
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:136
void set_green(Channel value)
Definition: Color.hpp:85
static const Color FUCHSIA
Definition: Color.hpp:143
bool operator<(Color a, Color b)
Definition: Color.hpp:148