SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Color.hpp
Go to the documentation of this file.
1#ifndef _COLOR_HPP_
2#define _COLOR_HPP_
3
4#include <algorithm>
5#include <cmath>
6#include <cstdint>
7#include <sstream>
8#include <string>
9#include <Math/Math.hpp>
10#include <Math/Vector.hpp>
11
12namespace Sleak {
13namespace Math {
14/// 8-bit RGBA color. Value type, cheap to pass and store on vertices/UI.
15/// @ingroup math
16class Color {
17 public:
18 static const Color Red;
19 static const Color Green;
20 static const Color Blue;
21 static const Color White;
22 static const Color Black;
23 static const Color Transparent;
24
25 constexpr Color() = default;
26 constexpr Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) noexcept
27 : r(r), g(g), b(b), a(a) {}
28
29 /// Clamps every channel into [0, 255].
30 [[nodiscard]] constexpr Color clamped() const noexcept {
31 return {static_cast<uint8_t>(Clamp<float>(r, 0, 255)),
32 static_cast<uint8_t>(Clamp<float>(g, 0, 255)),
33 static_cast<uint8_t>(Clamp<float>(b, 0, 255)),
34 static_cast<uint8_t>(Clamp<float>(a, 0, 255))};
35 }
36
37 /// Returns a copy with alpha replaced.
38 [[nodiscard]] constexpr Color withAlpha(uint8_t newAlpha) const noexcept {
39 return {r, g, b, newAlpha};
40 }
41
42 /// Scales RGB by alpha, for premultiplied-alpha blending.
43 [[nodiscard]] Color premultiplied() const noexcept;
44
45 [[nodiscard]] Color linearToSrgb() const noexcept;
46 [[nodiscard]] Color srgbToLinear() const noexcept;
47
48 /// Builds a color from hue/saturation/value in [0,1].
49 [[nodiscard]] static Color fromHSV(float h, float s, float v,
50 uint8_t a = 255) noexcept;
51 /// Builds a color from a 0xRRGGBB or 0xAARRGGBB packed value.
52 [[nodiscard]] static Color fromHex(uint32_t hex) noexcept;
53
54 [[nodiscard]] std::string toString() const {
55 std::ostringstream ss;
56 ss << "Color(" << static_cast<int>(r) << ", " << static_cast<int>(g)
57 << ", " << static_cast<int>(b) << ", " << static_cast<int>(a) << ")";
58 return ss.str();
59 }
60
61 constexpr bool operator==(const Color& other) const noexcept {
62 return r == other.r && g == other.g && b == other.b && a == other.a;
63 }
64
65 constexpr bool operator!=(const Color& other) const noexcept {
66 return !(*this == other);
67 }
68
69 constexpr Color operator+(const Color& other) const noexcept {
70 return Color{static_cast<uint8_t>(Clamp(r + other.r, 0, 255)),
71 static_cast<uint8_t>(Clamp(g + other.g, 0, 255)),
72 static_cast<uint8_t>(Clamp(b + other.b, 0, 255)),
73 static_cast<uint8_t>(Clamp(a + other.a, 0, 255))};
74 }
75
76 constexpr Color operator*(float scalar) const noexcept {
77 return Color{static_cast<uint8_t>(
78 Clamp(static_cast<int>(r * scalar), 0, 255)),
79 static_cast<uint8_t>(
80 Clamp(static_cast<int>(g * scalar), 0, 255)),
81 static_cast<uint8_t>(
82 Clamp(static_cast<int>(b * scalar), 0, 255)),
83 static_cast<uint8_t>(
84 Clamp(static_cast<int>(a * scalar), 0, 255))};
85 }
86
87 uint8_t GetR() const { return r; }
88 uint8_t GetG() const { return g; }
89 uint8_t GetB() const { return b; }
90 uint8_t GetA() const { return a; }
91
92 /// Converts to a Vector4D with each channel in [0,1].
93 [[nodiscard]] Vector4D normalize() const noexcept {
94 return {static_cast<float>(r) / 255.0f, static_cast<float>(g) / 255.0f,
95 static_cast<float>(b) / 255.0f, static_cast<float>(a) / 255.0f};
96 }
97
98 private:
99 uint8_t r{0};
100 uint8_t g{0};
101 uint8_t b{0};
102 uint8_t a{255};
103};
104} // namespace Math
105} // namespace Sleak
106
107#endif
static const Color Transparent
Definition Color.hpp:23
constexpr Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255) noexcept
Definition Color.hpp:26
Color srgbToLinear() const noexcept
Definition Color.cpp:39
Color linearToSrgb() const noexcept
Definition Color.cpp:22
uint8_t GetR() const
Definition Color.hpp:87
constexpr Color operator*(float scalar) const noexcept
Definition Color.hpp:76
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
uint8_t GetB() const
Definition Color.hpp:89
static const Color Red
Definition Color.hpp:18
constexpr Color operator+(const Color &other) const noexcept
Definition Color.hpp:69
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
std::string toString() const
Definition Color.hpp:54
constexpr Color withAlpha(uint8_t newAlpha) const noexcept
Returns a copy with alpha replaced.
Definition Color.hpp:38
Vector4D normalize() const noexcept
Converts to a Vector4D with each channel in [0,1].
Definition Color.hpp:93
constexpr bool operator!=(const Color &other) const noexcept
Definition Color.hpp:65
uint8_t GetG() const
Definition Color.hpp:88
constexpr Color clamped() const noexcept
Clamps every channel into [0, 255].
Definition Color.hpp:30
static const Color Green
Definition Color.hpp:19
static const Color White
Definition Color.hpp:21
uint8_t GetA() const
Definition Color.hpp:90
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
constexpr bool operator==(const Color &other) const noexcept
Definition Color.hpp:61
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