libSDL2pp
C++ bindings/wrapper for SDL2
Loading...
Searching...
No Matches
Color.hh
1 /*
2 libSDL2pp - C++ bindings/wrapper for SDL2
3 Copyright (C) 2017 Vraiment <jemc44@gmail.com>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20 */
21
22#ifndef SDL2PP_COLOR_HH
23#define SDL2PP_COLOR_HH
24
25#include <ostream>
26
27#include <SDL_pixels.h>
28
29#include <SDL2pp/Export.hh>
30
31namespace SDL2pp {
32
48class SDL2PP_EXPORT Color : public SDL_Color {
49public:
56 constexpr Color() : SDL_Color{0, 0, 0, 0} {
57 }
58
65 constexpr Color(const SDL_Color& color) : SDL_Color{color.r, color.g, color.b, color.a} {
66 }
67
76 constexpr Color(Uint8 r, Uint8 g, Uint8 b) : SDL_Color{r, g, b, SDL_ALPHA_OPAQUE} {
77 }
78
88 constexpr Color(Uint8 r, Uint8 g, Uint8 b, Uint8 a) : SDL_Color{r, g, b, a} {
89 }
90
95 Color(const Color&) noexcept = default;
96
101 Color(Color&&) noexcept = default;
102
109 Color& operator=(const Color&) noexcept = default;
110
117 Color& operator=(Color&&) noexcept = default;
118
125 constexpr Uint8 GetRed() const {
126 return r;
127 }
128
137 Color& SetRed(int nr) {
138 r = nr;
139 return *this;
140 }
141
148 constexpr Uint8 GetGreen() const {
149 return g;
150 }
151
160 Color& SetGreen(int ng) {
161 g = ng;
162 return *this;
163 }
164
171 constexpr Uint8 GetBlue() const {
172 return b;
173 }
174
183 Color& SetBlue(int nb) {
184 b = nb;
185 return *this;
186 }
187
194 constexpr Uint8 GetAlpha() const {
195 return a;
196 }
197
206 Color& SetAlpha(int na) {
207 a = na;
208 return *this;
209 }
210};
211
212}
213
223constexpr bool operator==(const SDL2pp::Color& a, const SDL2pp::Color& b) {
224 return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
225}
226
236constexpr bool operator!=(const SDL2pp::Color& a, const SDL2pp::Color& b) {
237 return !(a == b);
238}
239
249SDL2PP_EXPORT std::ostream& operator<<(std::ostream& stream, const SDL2pp::Color& color);
250
251namespace std {
252
257template<>
258struct hash<SDL2pp::Color> {
267 size_t operator()(const SDL2pp::Color& c) const {
268 size_t seed = std::hash<int>()(c.r);
269 seed ^= std::hash<int>()(c.g) + 0x9e3779b9 + (seed<<6) + (seed>>2);
270 seed ^= std::hash<int>()(c.b) + 0x9e3779b9 + (seed<<6) + (seed>>2);
271 seed ^= std::hash<int>()(c.a) + 0x9e3779b9 + (seed<<6) + (seed>>2);
272 return seed;
273 }
274};
275
276}
277
278#endif
RGB color with Alpha.
Definition: Color.hh:48
Color & SetGreen(int ng)
Set the green component from the color.
Definition: Color.hh:160
constexpr Color()
Default constructor.
Definition: Color.hh:56
Color(const Color &) noexcept=default
Copy constructor.
constexpr Uint8 GetAlpha() const
Get the alpha component from the color.
Definition: Color.hh:194
Color & SetAlpha(int na)
Set the alpha component from the color.
Definition: Color.hh:206
Color & SetRed(int nr)
Set the red component from the color.
Definition: Color.hh:137
Color & SetBlue(int nb)
Set the blue component from the color.
Definition: Color.hh:183
constexpr Color(const SDL_Color &color)
Construct a color from existing SDL_Color.
Definition: Color.hh:65
constexpr Uint8 GetBlue() const
Get the blue component from the color.
Definition: Color.hh:171
constexpr Uint8 GetGreen() const
Get the green component from the color.
Definition: Color.hh:148
constexpr Color(Uint8 r, Uint8 g, Uint8 b)
Construct the color from given RGB, alpha is opaque.
Definition: Color.hh:76
constexpr Color(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Construct the color from given RGB and alpha values.
Definition: Color.hh:88
Color(Color &&) noexcept=default
Move constructor.
size_t operator()(const SDL2pp::Color &c) const
Hash function for SDL2pp::Color.
Definition: Color.hh:267