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)) {}
36 [[nodiscard]]
static constexpr Color fromRgb(std::uint32_t rgb, std::uint8_t alpha = 255) noexcept {
37 return Color((rgb << 8) | alpha);
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));
48 [[nodiscard]]
static constexpr std::optional<Color>
fromHexString(std::string_view text)
noexcept {
49 if (!text.empty() && text.front() ==
'#') {
50 text.remove_prefix(1);
52 std::uint32_t value = 0;
53 for (
const char c : text) {
54 const int digit = hexDigit(c);
58 value = (value << 4) | static_cast<std::uint32_t>(digit);
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));
65 case 8:
return Color(value);
66 default:
return std::nullopt;
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;
76 [[nodiscard]]
constexpr std::uint32_t
toRgb() const noexcept {
80 [[nodiscard]]
constexpr std::array<float, 4>
toFloat() const noexcept {
81 return {
r / 255.f,
g / 255.f,
b / 255.f,
a / 255.f};
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);
98 const auto luma =
static_cast<std::uint8_t
>((54 *
r + 183 *
g + 19 *
b + 128) >> 8);
99 return Color(luma, luma, luma,
a);
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);
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;
118 static_assert(
sizeof(Color) == 4,
"Color must stay 4 packed bytes");
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);
146 return Color(mix(a.r, b.r), mix(a.g, b.g), mix(a.b, b.b), mix(a.a, b.a));
151 if (src.a == 255 || dst.a == 0) {
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);
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));
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