cpp-sdl2
C++ header-only SDL2 wrapper
color.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "exception.hpp"
4 #include <SDL_pixels.h>
5 #include <ostream>
6 
7 namespace sdl
8 {
10 class Color : public SDL_Color
11 {
12 public:
14  constexpr Color() : SDL_Color{0, 0, 0, 255} {}
15 
21  constexpr Color(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255)
22  : SDL_Color{r, g, b, a}
23  {
24  }
25 
27  constexpr Color(Color const&) = default;
28 
30  constexpr Color(Color&&) = default;
31 
33  Color(Uint32 raw, SDL_PixelFormat const& format)
34  {
35  SDL_GetRGBA(raw, &format, &r, &g, &b, &a);
36  }
37 
39  Color(Uint32 raw, Uint32 format)
40  {
41  auto f = SDL_AllocFormat(format);
42  if (!f) throw Exception{"SDL_AllocFormat"};
43  SDL_GetRGBA(raw, f, &r, &g, &b, &a);
44  SDL_FreeFormat(f);
45  }
46 
48  Color& operator=(Color const&) = default;
49 
51  Color& operator=(Color&&) = default;
52 
55  Uint32 as_uint(SDL_PixelFormat const& format) const
56  {
57  if (SDL_ISPIXELFORMAT_ALPHA(format.format))
58  {
59  return SDL_MapRGBA(&format, r, g, b, a);
60  }
61  else
62  {
63  return SDL_MapRGB(&format, r, g, b);
64  }
65  }
66 
68  Uint32 as_uint(Uint32 format) const
69  {
70  auto f = SDL_AllocFormat(format);
71  if (!f) throw Exception{"SDL_AllocFormat"};
72  auto raw = as_uint(*f);
73  SDL_FreeFormat(f);
74  return raw;
75  }
76 
78  bool operator==(Color const& c) const
79  {
80  return r == c.r && g == c.g && b == c.b && a == c.a;
81  }
82 
84  friend std::ostream& operator<<(std::ostream& stream, Color const& c)
85  {
86  return stream << "(r:" << c.r << ",g:" << c.g << ",b:" << c.b
87  << ",a:" << c.a << ")";
88  }
89 
90  static constexpr Color White() { return {255, 255, 255, 255}; }
91  static constexpr Color Black() { return {0, 0, 0, 255}; }
92  static constexpr Color Red() { return {255, 0, 0, 255}; }
93  static constexpr Color Green() { return {0, 255, 0, 255}; }
94  static constexpr Color Blue() { return {0, 0, 255, 255}; }
95  static constexpr Color Transparent() { return {0, 0, 0, 0}; }
96 };
97 
98 } // namespace sdl
constexpr Color()
Construct an sdl::Color object. Color will be black by default.
Definition: color.hpp:14
Color(Uint32 raw, Uint32 format)
Converting constructor that will initialize the color with a given 32 bit value, according to the pro...
Definition: color.hpp:39
static constexpr Color Black()
Definition: color.hpp:91
static constexpr Color Transparent()
Definition: color.hpp:95
Color(Uint32 raw, SDL_PixelFormat const &format)
Converting constructor that will initialize the color with a given 32 bit value, according to the pro...
Definition: color.hpp:33
static constexpr Color White()
Definition: color.hpp:90
Color & operator=(Color const &)=default
Default copy assing operator.
friend std::ostream & operator<<(std::ostream &stream, Color const &c)
Output stream overload that will print the value of the current color as a 4D vector of 8bit numbers...
Definition: color.hpp:84
C++ wrapping around the SDL_Color structure.
Definition: color.hpp:10
Define to deactivate exception support.
Definition: color.hpp:7
constexpr Color(Uint8 r, Uint8 g, Uint8 b, Uint8 a=255)
Construct an sdl Color::object with the given 8 bit color values.
Definition: color.hpp:21
Uint32 as_uint(Uint32 format) const
Return the color of the current object as a 32bit number (4 bytes)
Definition: color.hpp:68
allocator< U > a
Definition: simd.hpp:144
static constexpr Color Blue()
Definition: color.hpp:94
Define to deactivate exception support.
Definition: exception.hpp:21
static constexpr Color Red()
Definition: color.hpp:92
bool operator==(Color const &c) const
Return true if both colors are identical.
Definition: color.hpp:78
Uint32 as_uint(SDL_PixelFormat const &format) const
Return the color of the current object as a 32bit number (4 bytes)
Definition: color.hpp:55
static constexpr Color Green()
Definition: color.hpp:93