-
Notifications
You must be signed in to change notification settings - Fork 2
/
Color.h
119 lines (95 loc) · 3.03 KB
/
Color.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#pragma once
#include <cstdint> // std::uint32_t, std::uint8_t, etc
#include <ostream> // std::ostream
#include <type_traits> // std::enable_if_t
#include "math/Math.h"
#include "renderer/PixelFormat.h"
struct SDL_Color;
namespace ptgn {
class Color {
public:
// Generates a color with random RGB values and alpha of 255.
static Color RandomSolid();
// Generates a color with random RGBA values.
static Color Random();
/*
* Converts color to a 32-bit pixel format.
* @return The color converted into a 32-bit integer.
*/
static constexpr std::uint32_t AsUint32(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a) {
return r + (g << 8) + (b << 16) + (a << 24);
}
// Default construct color to black.
Color() = default;
~Color() = default;
// Construct color from individual RGBA8888 pixel format values.
constexpr Color(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a) :
r{ r }, g{ g }, b{ b }, a{ a } {}
/*
* Construct color from a pixel format integer.
* @param pixel Integer from which to retrieve red, green, blue, and alpha values.
* @param format The format which the pixel follows.
*/
Color(std::uint32_t pixel, PixelFormat format);
Color(const Color& copy) = default;
Color(Color&& move) = default;
Color& operator=(const Color& copy) = default;
Color& operator=(Color&& move) = default;
// Implicit conversion to SDL_Color, for internal use.
operator SDL_Color() const;
bool operator==(const Color& rhs) const {
return
r == rhs.r &&
g == rhs.g &&
b == rhs.b &&
a == rhs.a;
}
bool operator!=(const Color& rhs) const {
return !operator==(rhs);
}
/*
* Converts color to a given pixel format integer
* @param format The format according to which pixels are converted to an integer.
* @return The color converted to the given pixel format, 0 if invalid pixel format.
*/
std::uint32_t ToUint32(PixelFormat format) const;
/*
* Converts color to a 32-bit pixel format
* @return The color converted into a 32-bit integer.
*/
constexpr std::uint32_t ToUint32() const {
return AsUint32(r, g, b, a);
}
/*
* @return True if alpha value of color is 0, false otherwise.
*/
bool IsTransparent() const { return a == 0; }
// Default color: black.
std::uint8_t r{ 0 };
std::uint8_t g{ 0 };
std::uint8_t b{ 0 };
std::uint8_t a{ 255 };
private:
// Construction from SDL_Color, for internal use.
Color(const SDL_Color& color);
};
namespace math {
template <typename T,
std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
inline Color Lerp(const Color& a, const Color& b, T t) {
return {
Lerp<std::uint8_t>(a.r, b.r, t),
Lerp<std::uint8_t>(a.g, b.g, t),
Lerp<std::uint8_t>(a.b, b.b, t),
Lerp<std::uint8_t>(a.a, b.a, t)
};
}
} // namespace math
} // namespace ptgn
inline std::ostream& operator<<(std::ostream& os, const ptgn::Color& color) {
os << '[' << static_cast<std::uint32_t>(color.r);
os << ',' << static_cast<std::uint32_t>(color.g);
os << ',' << static_cast<std::uint32_t>(color.b);
os << ',' << static_cast<std::uint32_t>(color.a) << ']';
return os;
}