RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
Color.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstdint>
5#include <iosfwd>
6#include <optional>
7#include <string>
8#include <string_view>
9
11namespace RST::Color {
12
14 struct Hsv {
15 float h = 0.f;
16 float s = 0.f;
17 float v = 0.f;
18 float a = 1.f;
19 };
20
22 struct Color {
23 std::uint8_t r;
24 std::uint8_t g;
25 std::uint8_t b;
26 std::uint8_t a;
27
29 constexpr Color() noexcept : r(0), g(0), b(0), a(255) {}
30 constexpr Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha = 255) noexcept : r(red), g(green), b(blue), a(alpha) {}
32 constexpr Color(std::uint32_t hex) noexcept : r(static_cast<std::uint8_t>(hex >> 24)), g(static_cast<std::uint8_t>(hex >> 16)), b(static_cast<std::uint8_t>(hex >> 8)), a(static_cast<std::uint8_t>(hex)) {}
33
34 // ── Construction ─────────────────────────────────────────────────────
36 [[nodiscard]] static constexpr Color fromRgb(std::uint32_t rgb, std::uint8_t alpha = 255) noexcept {
37 return Color((rgb << 8) | alpha);
38 }
39
41 [[nodiscard]] static constexpr Color fromFloat(float red, float green, float blue, float alpha = 1.f) noexcept {
42 return Color(toByte(red), toByte(green), toByte(blue), toByte(alpha));
43 }
44
45 [[nodiscard]] static Color fromHsv(const Hsv& hsv) noexcept;
46
48 [[nodiscard]] static constexpr std::optional<Color> fromHexString(std::string_view text) noexcept {
49 if (!text.empty() && text.front() == '#') {
50 text.remove_prefix(1);
51 }
52 std::uint32_t value = 0;
53 for (const char c : text) {
54 const int digit = hexDigit(c);
55 if (digit < 0) {
56 return std::nullopt;
57 }
58 value = (value << 4) | static_cast<std::uint32_t>(digit);
59 }
60 const auto nibble = [value](int index) { return static_cast<std::uint8_t>(((value >> (4 * index)) & 0xF) * 0x11); };
61 switch (text.size()) {
62 case 3: return Color(nibble(2), nibble(1), nibble(0));
63 case 4: return Color(nibble(3), nibble(2), nibble(1), nibble(0));
64 case 6: return fromRgb(value);
65 case 8: return Color(value);
66 default: return std::nullopt;
67 }
68 }
69
70 // ── Conversion ───────────────────────────────────────────────────────
72 [[nodiscard]] constexpr std::uint32_t toHex() const noexcept {
73 return (static_cast<std::uint32_t>(r) << 24) | (static_cast<std::uint32_t>(g) << 16) | (static_cast<std::uint32_t>(b) << 8) | a;
74 }
76 [[nodiscard]] constexpr std::uint32_t toRgb() const noexcept {
77 return toHex() >> 8;
78 }
80 [[nodiscard]] constexpr std::array<float, 4> toFloat() const noexcept {
81 return {r / 255.f, g / 255.f, b / 255.f, a / 255.f};
82 }
83
84 [[nodiscard]] Hsv toHsv() const noexcept;
86 [[nodiscard]] std::string toHexString(bool withAlpha = true) const;
87
88 // ── Variations ───────────────────────────────────────────────────────
89 [[nodiscard]] constexpr Color withAlpha(std::uint8_t alpha) const noexcept { return Color(r, g, b, alpha); }
90
92 [[nodiscard]] constexpr Color inverted() const noexcept {
93 return Color(static_cast<std::uint8_t>(255 - r), static_cast<std::uint8_t>(255 - g), static_cast<std::uint8_t>(255 - b), a);
94 }
95
97 [[nodiscard]] constexpr Color grayscale() const noexcept {
98 const auto luma = static_cast<std::uint8_t>((54 * r + 183 * g + 19 * b + 128) >> 8);
99 return Color(luma, luma, luma, a);
100 }
101
102 [[nodiscard]] constexpr bool operator==(const Color&) const noexcept = default;
103
104 private:
105 [[nodiscard]] static constexpr std::uint8_t toByte(float value) noexcept {
106 const float clamped = value > 0.f ? (value < 1.f ? value : 1.f) : 0.f;
107 return static_cast<std::uint8_t>(clamped * 255.f + 0.5f);
108 }
109
110 [[nodiscard]] static constexpr int hexDigit(char c) noexcept {
111 if (c >= '0' && c <= '9') return c - '0';
112 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
113 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
114 return -1;
115 }
116 };
117
118 static_assert(sizeof(Color) == 4, "Color must stay 4 packed bytes");
119
121 std::ostream& operator<<(std::ostream& out, const Color& color);
122
125 constexpr Color Black {0, 0, 0, 255};
126 constexpr Color White {255, 255, 255, 255};
127 constexpr Color Gray {128, 128, 128, 255};
128 constexpr Color Red {255, 0, 0, 255};
129 constexpr Color Green {0, 255, 0, 255};
130 constexpr Color Blue {0, 0, 255, 255};
131 constexpr Color Yellow {255, 255, 0, 255};
132 constexpr Color Magenta {255, 0, 255, 255};
133 constexpr Color Cyan {0, 255, 255, 255};
134 constexpr Color Orange {255, 165, 0, 255};
135 constexpr Color Purple {128, 0, 128, 255};
136 constexpr Color Transparent {0, 0, 0, 0};
138
140 [[nodiscard]] constexpr Color lerp(const Color& a, const Color& b, float t) noexcept {
141 t = t > 0.f ? (t < 1.f ? t : 1.f) : 0.f;
142 const auto mix = [t](std::uint8_t from, std::uint8_t to) {
143 const float start = static_cast<float>(from);
144 return static_cast<std::uint8_t>(start + (static_cast<float>(to) - start) * t + 0.5f);
145 };
146 return Color(mix(a.r, b.r), mix(a.g, b.g), mix(a.b, b.b), mix(a.a, b.a));
147 }
148
150 [[nodiscard]] constexpr Color blend(const Color& src, const Color& dst) noexcept {
151 if (src.a == 255 || dst.a == 0) {
152 return src;
153 }
154 if (src.a == 0) {
155 return dst;
156 }
157 const float srcAlpha = static_cast<float>(src.a) / 255.f;
158 const float dstAlpha = static_cast<float>(dst.a) / 255.f * (1.f - srcAlpha);
159 const float outAlpha = srcAlpha + dstAlpha;
160 const auto mix = [=](std::uint8_t s, std::uint8_t d) {
161 return static_cast<std::uint8_t>((static_cast<float>(s) * srcAlpha + static_cast<float>(d) * dstAlpha) / outAlpha + 0.5f);
162 };
163 return Color(mix(src.r, dst.r), mix(src.g, dst.g), mix(src.b, dst.b), static_cast<std::uint8_t>(outAlpha * 255.f + 0.5f));
164 }
165}
RGBA colors: hex and HSV conversions, blending, interpolation and named colors.
Definition Color.hpp:11
constexpr Color Gray
Definition Color.hpp:127
constexpr Color Black
Definition Color.hpp:125
constexpr Color Purple
Definition Color.hpp:135
std::ostream & operator<<(std::ostream &out, const Color &color)
Prints the color as #RRGGBBAA.
constexpr Color Green
Definition Color.hpp:129
constexpr Color Transparent
Definition Color.hpp:136
constexpr Color Red
Definition Color.hpp:128
constexpr Color blend(const Color &src, const Color &dst) noexcept
Draws src over dst, taking the alpha of both into account.
Definition Color.hpp:150
constexpr Color Magenta
Definition Color.hpp:132
constexpr Color Yellow
Definition Color.hpp:131
constexpr Color Blue
Definition Color.hpp:130
constexpr Color lerp(const Color &a, const Color &b, float t) noexcept
Interpolates every component, alpha included, with t clamped to [0, 1].
Definition Color.hpp:140
constexpr Color Orange
Definition Color.hpp:134
constexpr Color White
Definition Color.hpp:126
constexpr Color Cyan
Definition Color.hpp:133
8-bit RGBA color, packed in 4 bytes.
Definition Color.hpp:22
constexpr Color withAlpha(std::uint8_t alpha) const noexcept
Definition Color.hpp:89
static constexpr Color fromFloat(float red, float green, float blue, float alpha=1.f) noexcept
From components in [0, 1], clamped.
Definition Color.hpp:41
constexpr std::uint32_t toHex() const noexcept
Packed as 0xRRGGBBAA.
Definition Color.hpp:72
constexpr std::uint32_t toRgb() const noexcept
Packed as 0xRRGGBB, without alpha.
Definition Color.hpp:76
Hsv toHsv() const noexcept
constexpr Color() noexcept
Opaque black.
Definition Color.hpp:29
constexpr Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha=255) noexcept
Definition Color.hpp:30
constexpr Color grayscale() const noexcept
Gray of the same luminance, keeping alpha.
Definition Color.hpp:97
std::string toHexString(bool withAlpha=true) const
Formats as #RRGGBBAA, or #RRGGBB without alpha.
std::uint8_t a
Definition Color.hpp:26
constexpr Color(std::uint32_t hex) noexcept
From a 0xRRGGBBAA value.
Definition Color.hpp:32
constexpr std::array< float, 4 > toFloat() const noexcept
Components in [0, 1].
Definition Color.hpp:80
constexpr Color inverted() const noexcept
Inverts red, green and blue, keeping alpha.
Definition Color.hpp:92
static Color fromHsv(const Hsv &hsv) noexcept
static constexpr Color fromRgb(std::uint32_t rgb, std::uint8_t alpha=255) noexcept
From a 0xRRGGBB value and a separate alpha.
Definition Color.hpp:36
std::uint8_t b
Definition Color.hpp:25
std::uint8_t g
Definition Color.hpp:24
std::uint8_t r
Definition Color.hpp:23
constexpr bool operator==(const Color &) const noexcept=default
static constexpr std::optional< Color > fromHexString(std::string_view text) noexcept
Parses #RGB, #RGBA, #RRGGBB or #RRGGBBAA, with or without the #.
Definition Color.hpp:48
HSV color: hue in degrees [0, 360), saturation, value and alpha in [0, 1].
Definition Color.hpp:14
float s
Definition Color.hpp:16
float v
Definition Color.hpp:17
float h
Definition Color.hpp:15
float a
Definition Color.hpp:18