ge211  2021.5.1
A student game engine
ge211_color.hxx
1 #pragma once
2 
3 #include "ge211_forward.hxx"
4 #include "ge211_noexcept.hxx"
5 
6 #include <SDL_pixels.h>
7 #include <cstdint>
8 
9 namespace ge211 {
10 
23 class Color
24 {
25 public:
28 
30  constexpr Color() NOEXCEPT
31  : Color{0, 0, 0, 0}
32  { }
33 
38  constexpr Color(uint8_t red,
39  uint8_t green,
40  uint8_t blue,
41  uint8_t alpha = 255) NOEXCEPT
42  : red_{red}, green_{green}, blue_{blue}, alpha_{alpha}
43  { }
44 
49  static Color from_rgba(double red, double green, double blue,
50  double alpha = 1.0) NOEXCEPT;
51 
53 
56 
58  static constexpr Color white() NOEXCEPT
59  {
60  return {255, 255, 255};
61  }
62 
64  static constexpr Color black() NOEXCEPT
65  {
66  return {0, 0, 0};
67  }
68 
70  static Color medium_red() NOEXCEPT
71  { return from_hsla(0, .5, .5); }
73  static Color medium_green() NOEXCEPT
74  { return from_hsla(120, .5, .5); }
76  static Color medium_blue() NOEXCEPT
77  { return from_hsla(240, .5, .5); }
79  static Color medium_cyan() NOEXCEPT
80  { return from_hsla(180, .5, .5); }
82  static Color medium_magenta() NOEXCEPT
83  { return from_hsla(300, .5, .5); }
85  static Color medium_yellow() NOEXCEPT
86  { return from_hsla(60, .5, .5); }
87 
89 
92 
94  uint8_t red() const NOEXCEPT { return red_; };
96  uint8_t green() const NOEXCEPT { return green_; };
98  uint8_t blue() const NOEXCEPT { return blue_; };
100  uint8_t alpha() const NOEXCEPT { return alpha_; };
101 
102 
105 
108  Color blend(double weight, Color that) const NOEXCEPT;
109 
111  Color invert() const NOEXCEPT;
112 
115  Color rotate_hue(double degrees) const NOEXCEPT;
116 
120  Color lighten(double unit_amount) const NOEXCEPT;
121 
125  Color darken(double unit_amount) const NOEXCEPT;
126 
130  Color saturate(double unit_amount) const NOEXCEPT;
131 
135  Color desaturate(double unit_amount) const NOEXCEPT;
136 
140  Color fade_in(double unit_amount) const NOEXCEPT;
141 
145  Color fade_out(double unit_amount) const NOEXCEPT;
146 
148 
151 
155  struct HSLA
156  {
159  double hue;
160 
163  double saturation;
164 
166  double lightness;
167 
170  double alpha;
171 
174  HSLA(double hue, double saturation,
175  double lightness, double alpha = 1.0) NOEXCEPT;
176 
178  Color to_rgba() const NOEXCEPT;
179 
182 
184 
185  HSLA rotate_hue(double degrees) const NOEXCEPT;
189  HSLA saturate(double unit_amount) const NOEXCEPT;
193  HSLA desaturate(double unit_amount) const NOEXCEPT;
197  HSLA lighten(double unit_amount) const NOEXCEPT;
201  HSLA darken(double unit_amount) const NOEXCEPT;
205  HSLA fade_in(double unit_amount) const NOEXCEPT;
209  HSLA fade_out(double unit_amount) const NOEXCEPT;
210 
212  };
213 
221  static Color from_hsla(double hue, double saturation, double lightness,
222  double alpha = 1) NOEXCEPT;
223 
225  HSLA to_hsla() const NOEXCEPT;
229  struct HSVA
230  {
233  double hue;
234 
237  double saturation;
238 
241  double value;
242 
245  double alpha;
246 
249  HSVA(double hue, double saturation,
250  double value, double alpha = 1.0) NOEXCEPT;
251 
253  Color to_rgba() const NOEXCEPT;
254 
257 
260  HSVA rotate_hue(double degrees) const NOEXCEPT;
264  HSVA saturate(double unit_amount) const NOEXCEPT;
268  HSVA desaturate(double unit_amount) const NOEXCEPT;
272  HSVA revalue(double unit_amount) const NOEXCEPT;
276  HSVA devalue(double unit_amount) const NOEXCEPT;
280  HSVA fade_in(double unit_amount) const NOEXCEPT;
284  HSVA fade_out(double unit_amount) const NOEXCEPT;
285 
287  };
288 
296  static Color from_hsva(double hue, double saturation, double value,
297  double alpha = 1) NOEXCEPT;
298 
300  HSVA to_hsva() const NOEXCEPT;
301 
303 
304 private:
305  uint8_t red_;
306  uint8_t green_;
307  uint8_t blue_;
308  uint8_t alpha_;
309 
310  friend Text_sprite;
311  friend ::ge211::internal::Render_sprite;
312 
313  SDL_Color to_sdl_() const NOEXCEPT;
314  uint32_t to_sdl_(const SDL_PixelFormat*) const NOEXCEPT;
315 };
316 
317 }
318 
ge211::Color::medium_magenta
static Color medium_magenta()
Solid magenta.
Definition: ge211_color.hxx:82
ge211::Color::to_hsla
HSLA to_hsla() const
Converts a color to the hue-saturation-lightness (HSL) color model.
Definition: ge211_color.cxx:149
ge211::Color::HSLA::saturation
double saturation
The fullness of the color, from 0.0 (grey) to 1.0 (fully saturated).
Definition: ge211_color.hxx:163
ge211::Color::from_rgba
static Color from_rgba(double red, double green, double blue, double alpha=1.0)
Constructs a color with the given components.
Definition: ge211_color.cxx:28
ge211::Color::Color
constexpr Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha=255)
Constructs the color with the given components.
Definition: ge211_color.hxx:38
ge211::Color::green
uint8_t green() const
Gets the green component of a color.
Definition: ge211_color.hxx:96
ge211::Color::HSVA::alpha
double alpha
The opacity of the color, from 0.0 (fully transparent) to 1.0 (fully opaque).
Definition: ge211_color.hxx:245
ge211::Color::lighten
Color lighten(double unit_amount) const
Produces a tint by lightening the color.
Definition: ge211_color.cxx:109
ge211::Color::fade_out
Color fade_out(double unit_amount) const
Decreases opacity of the color.
Definition: ge211_color.cxx:191
ge211::Color::blue
uint8_t blue() const
Gets the blue component of a color.
Definition: ge211_color.hxx:98
ge211::Color::white
static constexpr Color white()
Solid white.
Definition: ge211_color.hxx:58
ge211
The game engine namespace.
Definition: ge211.hxx:4
ge211::Color::HSVA
Representation for the hue-saturation-value-alpha color model.
Definition: ge211_color.hxx:230
ge211::Color::HSLA
Representation for the hue-saturation-lightness-alpha color model.
Definition: ge211_color.hxx:156
ge211::Color::HSLA::hue
double hue
The hue in degrees from 0 to 360.
Definition: ge211_color.hxx:159
ge211::Color::medium_red
static Color medium_red()
Solid red.
Definition: ge211_color.hxx:70
ge211::Color::alpha
uint8_t alpha() const
Gets the alpha (opacity) component of a color.
Definition: ge211_color.hxx:100
ge211::Color::HSLA::alpha
double alpha
The opacity of the color, from 0.0 (fully transparent) to 1.0 (fully opaque).
Definition: ge211_color.hxx:170
ge211::Color::blend
Color blend(double weight, Color that) const
Produces a blend of this color and that, with higher weight (between 0 and 1) increasing the level of...
Definition: ge211_color.cxx:89
ge211::sprites::Text_sprite
A Sprite that displays text.
Definition: ge211_sprites.hxx:232
ge211::Color::medium_cyan
static Color medium_cyan()
Solid cyan.
Definition: ge211_color.hxx:79
ge211::Color::red
uint8_t red() const
Gets the red component of a color.
Definition: ge211_color.hxx:94
ge211::Color::HSVA::saturation
double saturation
The fullness of the color, from 0,0 (grey) to 1.0 (fully saturated).
Definition: ge211_color.hxx:237
ge211::Color::medium_yellow
static Color medium_yellow()
Solid yellow.
Definition: ge211_color.hxx:85
ge211::Color::black
static constexpr Color black()
Solid black.
Definition: ge211_color.hxx:64
ge211::internal::Render_sprite
A Render_sprite works by allowing its derived classes to render themselves pixel-by-pixel onto an SDL...
Definition: ge211_sprites.hxx:105
ge211::Color::medium_green
static Color medium_green()
Solid green.
Definition: ge211_color.hxx:73
ge211::Color::darken
Color darken(double unit_amount) const
Produces a shade by darkening the color.
Definition: ge211_color.cxx:114
ge211::Color::rotate_hue
Color rotate_hue(double degrees) const
Returns a color by rotating the hue, leaving the other properties constant.
Definition: ge211_color.cxx:104
ge211::Color
For representing colors.
Definition: ge211_color.hxx:24
ge211::Color::from_hsla
static Color from_hsla(double hue, double saturation, double lightness, double alpha=1)
Constructs a color given the hue, saturation, lightness, and alpha.
Definition: ge211_color.cxx:73
ge211::Color::from_hsva
static Color from_hsva(double hue, double saturation, double value, double alpha=1)
Constructs a color given the hue, saturation, value, and alpha.
Definition: ge211_color.cxx:81
ge211::Color::Color
constexpr Color()
Constructs the transparent color.
Definition: ge211_color.hxx:30
ge211::Color::desaturate
Color desaturate(double unit_amount) const
Produces a weaker tone by desaturating the color.
Definition: ge211_color.cxx:124
ge211::Color::HSLA::lightness
double lightness
The lightness of the color, from 0.0 (black) to 1.0 (white).
Definition: ge211_color.hxx:166
ge211::Color::medium_blue
static Color medium_blue()
Solid blue.
Definition: ge211_color.hxx:76
ge211::Color::HSVA::value
double value
The brightness of the color, from 0.0 (black) to 1.0 (fully colored).
Definition: ge211_color.hxx:241
ge211::Color::invert
Color invert() const
Returns the inverse of a color.
Definition: ge211_color.cxx:99
ge211::Color::to_hsva
HSVA to_hsva() const
Converts a color to the hue-saturation-value (HSV) color model.
Definition: ge211_color.cxx:160
ge211::Color::saturate
Color saturate(double unit_amount) const
Produces a fuller tone by saturating the color.
Definition: ge211_color.cxx:119
ge211::Color::HSVA::hue
double hue
The hue in degrees from 0 to 360.
Definition: ge211_color.hxx:233
ge211::Color::fade_in
Color fade_in(double unit_amount) const
Increases opacity of the color.
Definition: ge211_color.cxx:186