ge211
ge211_color.h
1 #pragma once
2 
3 #include "ge211_forward.h"
4 
5 #include <SDL_pixels.h>
6 #include <cstdint>
7 
8 namespace ge211 {
9 
22 class Color
23 {
24 public:
27 
29  constexpr Color() noexcept
30  : Color{0, 0, 0, 0}
31  { }
32 
37  constexpr Color(uint8_t red,
38  uint8_t green,
39  uint8_t blue,
40  uint8_t alpha = 255) noexcept
41  : red_{red}, green_{green}, blue_{blue}, alpha_{alpha}
42  { }
43 
48  static Color from_rgba(double red, double green, double blue,
49  double alpha = 1.0) noexcept;
50 
52 
55 
57  static constexpr Color white() noexcept
58  {
59  return {255, 255, 255};
60  }
61 
63  static constexpr Color black() noexcept
64  {
65  return {0, 0, 0};
66  }
67 
69  static Color medium_red() noexcept
70  { return from_hsla(0, .5, .5); }
72  static Color medium_green() noexcept
73  { return from_hsla(120, .5, .5); }
75  static Color medium_blue() noexcept
76  { return from_hsla(240, .5, .5); }
78  static Color medium_cyan() noexcept
79  { return from_hsla(180, .5, .5); }
81  static Color medium_magenta() noexcept
82  { return from_hsla(300, .5, .5); }
84  static Color medium_yellow() noexcept
85  { return from_hsla(60, .5, .5); }
86 
88 
91 
93  uint8_t red() const noexcept { return red_; };
95  uint8_t green() const noexcept { return green_; };
97  uint8_t blue() const noexcept { return blue_; };
99  uint8_t alpha() const noexcept { return alpha_; };
100 
101 
102 
105 
107  Color invert() const noexcept;
108 
111  Color rotate_hue(double degrees) const noexcept;
112 
116  Color lighten(double unit_amount) const noexcept;
117 
121  Color darken(double unit_amount) const noexcept;
122 
126  Color saturate(double unit_amount) const noexcept;
127 
131  Color desaturate(double unit_amount) const noexcept;
132 
136  Color fade_in(double unit_amount) const noexcept;
137 
141  Color fade_out(double unit_amount) const noexcept;
142 
144 
147 
151  struct HSLA {
154  double hue;
157  double saturation;
159  double lightness;
162  double alpha;
163 
165  Color to_rgba() const noexcept;
166 
169 
171 
172  HSLA rotate_hue(double degrees) const noexcept;
176  HSLA saturate(double unit_amount) const noexcept;
180  HSLA desaturate(double unit_amount) const noexcept;
184  HSLA lighten(double unit_amount) const noexcept;
188  HSLA darken(double unit_amount) const noexcept;
192  HSLA fade_in(double unit_amount) const noexcept;
196  HSLA fade_out(double unit_amount) const noexcept;
197 
199  };
200 
208  static Color from_hsla(double hue, double saturation, double lightness,
209  double alpha = 1) noexcept;
210 
212  HSLA to_hsla() const noexcept;
216  struct HSVA {
219  double hue;
222  double saturation;
225  double value;
228  double alpha;
229 
231  Color to_rgba() const noexcept;
232 
235 
238  HSVA rotate_hue(double degrees) const noexcept;
242  HSVA saturate(double unit_amount) const noexcept;
246  HSVA desaturate(double unit_amount) const noexcept;
250  HSVA revalue(double unit_amount) const noexcept;
254  HSVA devalue(double unit_amount) const noexcept;
258  HSVA fade_in(double unit_amount) const noexcept;
262  HSVA fade_out(double unit_amount) const noexcept;
263 
265  };
266 
274  static Color from_hsva(double hue, double saturation, double value,
275  double alpha = 1) noexcept;
276 
278  HSVA to_hsva() const noexcept;
279 
281 
282 private:
283  uint8_t red_;
284  uint8_t green_;
285  uint8_t blue_;
286  uint8_t alpha_;
287 
288  friend Text_sprite;
289  friend detail::Render_sprite;
290 
291  SDL_Color to_sdl_() const noexcept;
292  uint32_t to_sdl_(const SDL_PixelFormat*) const noexcept;
293 };
294 
295 }
296 
static Color from_hsva(double hue, double saturation, double value, double alpha=1) noexcept
Constructs a color given the hue, saturation, value, and alpha.
Definition: ge211_color.cpp:79
Color invert() const noexcept
Returns the inverse of a color.
Definition: ge211_color.cpp:87
double saturation
The fullness of the color, from 0,0 (grey) to 1.0 (fully saturated).
Definition: ge211_color.h:222
double hue
The hue in degrees from 0 to 360.
Definition: ge211_color.h:219
uint8_t alpha() const noexcept
Gets the alpha (opacity) component of a color.
Definition: ge211_color.h:99
static Color medium_magenta() noexcept
Solid magenta.
Definition: ge211_color.h:81
double value
The brightness of the color, from 0.0 (black) to 1.0 (fully colored).
Definition: ge211_color.h:225
Representation for the hue-saturation-value-alpha color model.
Definition: ge211_color.h:216
HSVA to_hsva() const noexcept
Converts a color to the hue-saturation-value (HSV) color model.
Representation for the hue-saturation-lightness-alpha color model.
Definition: ge211_color.h:151
Color rotate_hue(double degrees) const noexcept
Returns a color by rotating the hue, leaving the other properties constant.
Definition: ge211_color.cpp:92
The game engine namespace.
Definition: ge211.h:17
Color fade_in(double unit_amount) const noexcept
Increases opacity of the color.
Color saturate(double unit_amount) const noexcept
Produces a fuller tone by saturating the color.
double lightness
The lightness of the color, from 0.0 (black) to 1.0 (white).
Definition: ge211_color.h:159
constexpr Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha=255) noexcept
Constructs the color with the given components.
Definition: ge211_color.h:37
HSLA to_hsla() const noexcept
Converts a color to the hue-saturation-lightness (HSL) color model.
Color darken(double unit_amount) const noexcept
Produces a shade by darkening the color.
static Color medium_red() noexcept
Solid red.
Definition: ge211_color.h:69
uint8_t blue() const noexcept
Gets the blue component of a color.
Definition: ge211_color.h:97
uint8_t red() const noexcept
Gets the red component of a color.
Definition: ge211_color.h:93
uint8_t green() const noexcept
Gets the green component of a color.
Definition: ge211_color.h:95
Color fade_out(double unit_amount) const noexcept
Decreases opacity of the color.
Color desaturate(double unit_amount) const noexcept
Produces a weaker tone by desaturating the color.
static Color from_rgba(double red, double green, double blue, double alpha=1.0) noexcept
Constructs a color with the given components.
Definition: ge211_color.cpp:26
static Color medium_cyan() noexcept
Solid cyan.
Definition: ge211_color.h:78
double saturation
The fullness of the color, from 0.0 (grey) to 1.0 (fully saturated).
Definition: ge211_color.h:157
A Sprite that displays text.
static constexpr Color white() noexcept
Solid white.
Definition: ge211_color.h:57
static Color medium_yellow() noexcept
Solid yellow.
Definition: ge211_color.h:84
static Color medium_blue() noexcept
Solid blue.
Definition: ge211_color.h:75
static constexpr Color black() noexcept
Solid black.
Definition: ge211_color.h:63
static Color medium_green() noexcept
Solid green.
Definition: ge211_color.h:72
For representing colors.
Definition: ge211_color.h:22
double hue
The hue in degrees from 0 to 360.
Definition: ge211_color.h:154
double alpha
The opacity of the color, from 0.0 (fully transparent) to 1.0 (fully opaque).
Definition: ge211_color.h:228
static Color from_hsla(double hue, double saturation, double lightness, double alpha=1) noexcept
Constructs a color given the hue, saturation, lightness, and alpha.
Definition: ge211_color.cpp:71
constexpr Color() noexcept
Constructs the transparent color.
Definition: ge211_color.h:29
Color lighten(double unit_amount) const noexcept
Produces a tint by lightening the color.
Definition: ge211_color.cpp:97
double alpha
The opacity of the color, from 0.0 (fully transparent) to 1.0 (fully opaque).
Definition: ge211_color.h:162