SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Color.cpp
Go to the documentation of this file.
1#include <Math/Color.hpp>
2
3namespace Sleak {
4namespace Math {
5
6const Color Color::Red(255, 0, 0);
7const Color Color::Green(0, 255, 0);
8const Color Color::Blue(0, 0, 255);
9const Color Color::White(255, 255, 255);
10const Color Color::Black(0, 0, 0);
11const Color Color::Transparent(0, 0, 0, 0);
12
13Color Color::premultiplied() const noexcept {
14 return {
15 static_cast<uint8_t>(Clamp(static_cast<int>(r * static_cast<float>(a) / 255.0f), 0, 255)),
16 static_cast<uint8_t>(Clamp(static_cast<int>(g * static_cast<float>(a) / 255.0f), 0, 255)),
17 static_cast<uint8_t>(Clamp(static_cast<int>(b * static_cast<float>(a) / 255.0f), 0, 255)),
18 a
19 };
20}
21
22Color Color::linearToSrgb() const noexcept {
23 auto linearToSrgbComponent = [](float c) {
24 if (c <= 0.0031308f) {
25 return 12.92f * c;
26 } else {
27 return 1.055f * std::pow(c, 1.0f / 2.4f) - 0.055f;
28 }
29 };
30
31 return {
32 static_cast<uint8_t>(Clamp(static_cast<int>(linearToSrgbComponent(static_cast<float>(r) / 255.0f) * 255.0f), 0, 255)),
33 static_cast<uint8_t>(Clamp(static_cast<int>(linearToSrgbComponent(static_cast<float>(g) / 255.0f) * 255.0f), 0, 255)),
34 static_cast<uint8_t>(Clamp(static_cast<int>(linearToSrgbComponent(static_cast<float>(b) / 255.0f) * 255.0f), 0, 255)),
35 a
36 };
37}
38
39Color Color::srgbToLinear() const noexcept {
40 auto srgbToLinearComponent = [](float c) {
41 if (c <= 0.04045f) {
42 return c / 12.92f;
43 } else {
44 return std::pow((c + 0.055f) / 1.055f, 2.4f);
45 }
46 };
47
48 return {
49 static_cast<uint8_t>(Clamp(static_cast<int>(srgbToLinearComponent(static_cast<float>(r) / 255.0f) * 255.0f), 0, 255)),
50 static_cast<uint8_t>(Clamp(static_cast<int>(srgbToLinearComponent(static_cast<float>(g) / 255.0f) * 255.0f), 0, 255)),
51 static_cast<uint8_t>(Clamp(static_cast<int>(srgbToLinearComponent(static_cast<float>(b) / 255.0f) * 255.0f), 0, 255)),
52 a
53 };
54}
55
56Color Color::fromHSV(float h, float s, float v, uint8_t a) noexcept {
57 if (s == 0.0f) {
58 uint8_t value = static_cast<uint8_t>(Clamp(static_cast<int>(v * 255.0f), 0, 255));
59 return {value, value, value, a};
60 }
61
62 h *= 6.0f;
63 int i = static_cast<int>(std::floor(h));
64 float f = h - i;
65 float p = v * (1.0f - s);
66 float q = v * (1.0f - s * f);
67 float t = v * (1.0f - s * (1.0f - f));
68
69 uint8_t value_v = static_cast<uint8_t>(Clamp(static_cast<int>(v * 255.0f), 0, 255));
70 uint8_t value_p = static_cast<uint8_t>(Clamp(static_cast<int>(p * 255.0f), 0, 255));
71 uint8_t value_q = static_cast<uint8_t>(Clamp(static_cast<int>(q * 255.0f), 0, 255));
72 uint8_t value_t = static_cast<uint8_t>(Clamp(static_cast<int>(t * 255.0f), 0, 255));
73
74 switch (i) {
75 case 0: return {value_v, value_t, value_p, a};
76 case 1: return {value_q, value_v, value_p, a};
77 case 2: return {value_p, value_v, value_t, a};
78 case 3: return {value_p, value_q, value_v, a};
79 case 4: return {value_t, value_p, value_v, a};
80 default: return {value_v, value_p, value_q, a};
81 }
82}
83
84Color Color::fromHex(uint32_t hex) noexcept {
85 uint8_t r = static_cast<uint8_t>((hex >> 16) & 0xFF);
86 uint8_t g = static_cast<uint8_t>((hex >> 8) & 0xFF);
87 uint8_t b = static_cast<uint8_t>(hex & 0xFF);
88 uint8_t a = 255;
89 if((hex >> 24) & 0xFF)
90 a = static_cast<uint8_t>((hex >> 24) & 0xFF);
91 return {r, g, b, a};
92}
93
94} // namespace Math
95} // namespace Sleak
static const Color Transparent
Definition Color.hpp:23
Color srgbToLinear() const noexcept
Definition Color.cpp:39
Color linearToSrgb() const noexcept
Definition Color.cpp:22
static Color fromHSV(float h, float s, float v, uint8_t a=255) noexcept
Builds a color from hue/saturation/value in [0,1].
Definition Color.cpp:56
static const Color Red
Definition Color.hpp:18
constexpr Color()=default
Color premultiplied() const noexcept
Scales RGB by alpha, for premultiplied-alpha blending.
Definition Color.cpp:13
static const Color Black
Definition Color.hpp:22
static const Color Green
Definition Color.hpp:19
static const Color White
Definition Color.hpp:21
static Color fromHex(uint32_t hex) noexcept
Builds a color from a 0xRRGGBB or 0xAARRGGBB packed value.
Definition Color.cpp:84
static const Color Blue
Definition Color.hpp:20
Vectors, matrices, quaternions, colors, AABBs, and random helpers.
constexpr const T & Clamp(const T &value, T min, T max)
Clamps value into [min, max].
Definition Math.hpp:15
Root namespace for everything the engine exposes.
Definition Camera.hpp:10