27 return static_cast<uint8_t
>(std::lround(std::clamp(value, 0.0F, 255.0F)));
51 Color(uint8_t
r, uint8_t
g, uint8_t
b, uint8_t
a = 255)
79 return {
static_cast<float>(
r) / 255.0F,
static_cast<float>(
g) / 255.0F,
80 static_cast<float>(
b) / 255.0F,
static_cast<float>(
a) / 255.0F };
90 if (hex.empty() || hex[0] !=
'#' || (hex.length() != 7 && hex.length() != 9)) {
95 const bool digits_ok = std::all_of(hex.begin() + 1, hex.end(),
96 [](
char c) { return std::isxdigit(static_cast<unsigned char>(c)) != 0; });
101 const auto r =
static_cast<uint8_t
>(std::stoi(hex.substr(1, 2),
nullptr, 16));
102 const auto g =
static_cast<uint8_t
>(std::stoi(hex.substr(3, 2),
nullptr, 16));
103 const auto b =
static_cast<uint8_t
>(std::stoi(hex.substr(5, 2),
nullptr, 16));
106 if (hex.length() == 9) {
107 a =
static_cast<uint8_t
>(std::stoi(hex.substr(7, 2),
nullptr, 16));
110 return {
r,
g,
b,
a };
119 const float p = std::clamp(percentage, 0.0F, 1.0F);
130 const float keep = 1.0F - std::clamp(percentage, 0.0F, 1.0F);
140 float alpha =
a / 255.0F;
141 return {
static_cast<uint8_t
>(
r * alpha + color.
r * (1 - alpha)),
142 static_cast<uint8_t
>(
g * alpha + color.
g * (1 - alpha)),
143 static_cast<uint8_t
>(
b * alpha + color.
b * (1 - alpha)), 255 };
154 const float p = std::clamp(t, 0.0F, 1.0F);
164 return {
static_cast<uint8_t
>(255 -
r),
static_cast<uint8_t
>(255 -
g),
165 static_cast<uint8_t
>(255 -
b),
a };
173 auto gray =
static_cast<uint8_t
>(0.299F *
r + 0.587F *
g + 0.114F *
b);
174 return { gray, gray, gray,
a };
184 return {
r,
g,
b, alpha };
const Color lightslategrey
const Color darkturquoise
const Color blanchedalmond
const Color lightseagreen
const Color mediumseagreen
const Color mediumslateblue
const Color cornflowerblue
const Color lightgoldenrodyellow
const Color palevioletred
const Color rebeccapurple
const Color paleturquoise
const Color mediumaquamarine
const Color darkslategrey
const Color darkslateblue
const Color darkolivegreen
const Color lavenderblush
const Color lightslategray
const Color mediumspringgreen
const Color mediumvioletred
const Color darkslategray
const Color mediumturquoise
const Color darkgoldenrod
const Color lightsteelblue
const Color palegoldenrod
SDL_FColor FColor
A colour with each channel as a float from 0.0 to 1.0.
RGBA color struct with 8-bit channels.
Color with_alpha(uint8_t alpha) const
Adjust the alpha of a color.
Color blend(const Color &color) const
Blend two colors together using alpha blending.
static Color from_float(float r, float g, float b, float a=1.0F)
From float RGBA values (0.0-1.0) to Color.
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Construct a Color from RGBA values.
Color invert() const
Invert a color.
static uint8_t to_channel(float value)
Round a 0-255 channel value and clamp it into range. Casting an out of range float straight to uint8_...
static Color from_hex(const std::string &hex)
From a hex string (e.g. "#RRGGBBAA") to a Color.
Color lerp(const Color &color, float t) const
Mix towards another color, including alpha.
Color grayscale() const
Convert a color to grayscale.
Color()
Construct a default Color (black, fully opaque).
Color darken(float percentage) const
Darken a color by a given percentage.
Color lighten(float percentage) const
Lighten a color by a given percentage.
FColor to_fcolor() const
To a float colour, with each channel from 0.0 to 1.0.