17#include <egt/detail/math.h>
18#include <egt/detail/meta.h>
19#include <egt/detail/string.h>
20#include <initializer_list>
110 EGT_NODISCARD
constexpr float redf()
const {
return m_rgba[0] / 255.f; }
111 EGT_NODISCARD
constexpr float greenf()
const {
return m_rgba[1] / 255.f; }
112 EGT_NODISCARD
constexpr float bluef()
const {
return m_rgba[2] / 255.f; }
113 EGT_NODISCARD
constexpr float alphaf()
const {
return m_rgba[3] / 255.f; }
118 constexpr void redf(
float v) { m_rgba[0] = detail::clamp(v, 0.f, 1.f) * 255.f; }
119 constexpr void greenf(
float v) { m_rgba[1] = detail::clamp(v, 0.f, 1.f) * 255.f; }
120 constexpr void bluef(
float v) { m_rgba[2] = detail::clamp(v, 0.f, 1.f) * 255.f; }
121 constexpr void alphaf(
float v) { m_rgba[3] = detail::clamp(v, 0.f, 1.f) * 255.f; }
126 EGT_NODISCARD
constexpr ComponentType red()
const {
return assert(m_rgba[0] <= 255), m_rgba[0]; }
128 EGT_NODISCARD
constexpr ComponentType blue()
const {
return assert(m_rgba[2] <= 255), m_rgba[2]; }
141 EGT_NODISCARD
constexpr uint16_t
pixel16()
const
143 const uint16_t b = (blue() >> 3u) & 0x1fu;
144 const uint16_t g = ((green() >> 2u) & 0x3fu) << 5u;
145 const uint16_t r = ((red() >> 3u) & 0x1fu) << 11u;
147 return static_cast<uint16_t
>(r | g | b);
153 const uint16_t b = (c) & 0x1fu;
154 const uint16_t g = (c >> 5u) & 0x3fu;
155 const uint16_t r = (c >> 11u) & 0x1fu;
157 return Color(r, g, b, 0xffu);
163 return (red() << 16u) |
172 return Color((c >> 16u) & 0xffu,
181 return (red() << 16u) |
190 return Color((c >> 16u) & 0xffu,
204 void get_hsvf(
float& h,
float& s,
float& v,
float* alpha =
nullptr);
214 void get_hslf(
float& h,
float& s,
float& l,
float* alpha =
nullptr);
224 void get_rgbaf(
float& r,
float& g,
float& b,
float* alpha =
nullptr);
239 return Color(c << 8u | alpha);
250 EGT_NODISCARD
static constexpr Color rgbaf(
float r,
float g,
float b,
float a = 1.0)
272 EGT_NODISCARD
static Color css(
const std::string& hex)
274 return css(hex.c_str());
277 EGT_NODISCARD
constexpr static Color css(
const char* hex)
279 return Color((detail::hextoul(hex) << 8) | 0xff);
285 EGT_NODISCARD std::string
hex()
const;
295 EGT_NODISCARD
static Color hsvf(
float h,
float s,
float v,
float alpha = 1.0);
305 EGT_NODISCARD
static Color hslf(
float h,
float s,
float l,
float alpha = 1.0);
316 return Color(red() * (1.0f - factor),
317 green() * (1.0f - factor),
318 blue() * (1.0f - factor),
331 return Color(red() + ((255 - red()) * factor),
332 green() + ((255 - green()) * factor),
333 blue() + ((255 - blue()) * factor),
342 auto u = std::cos(h * detail::pi<float>() / 180.0f);
343 auto w = std::sin(h * detail::pi<float>() / 180.0f);
346 ret.
red((0.299f + 0.701f * u + 0.168f * w) * red()
347 + (0.587f - 0.587f * u + 0.330f * w) * green()
348 + (0.114f - 0.114f * u - 0.497f * w) * blue());
349 ret.
green((0.299f - 0.299f * u - 0.328f * w) * red()
350 + (0.587f + 0.413f * u + 0.035f * w) * green()
351 + (0.114f - 0.114f * u + 0.292f * w) * blue());
352 ret.
blue((0.299f - 0.3f * u + 1.25f * w) * red()
353 + (0.587f - 0.588f * u - 1.05f * w) * green()
354 + (0.114f + 0.886f * u - 0.203f * w) * blue());
370 m_rgba[0] = (c >> 24u) & 0xffu;
371 m_rgba[1] = (c >> 16u) & 0xffu;
372 m_rgba[2] = (c >> 8u) & 0xffu;
373 m_rgba[3] = c & 0xffu;
382 return Color::rgbaf(redf() + scalar,
392 return Color::rgbaf(redf() - scalar,
402 return Color::rgbaf(redf() * scalar,
412 return Color::rgbaf(redf() / scalar,
421 return Color::rgbaf(redf() + rhs.
redf(),
423 bluef() + rhs.
bluef(),
430 return Color::rgbaf(redf() - rhs.
redf(),
432 bluef() - rhs.
bluef(),
439 return Color::rgbaf(redf() * rhs.
redf(),
441 bluef() * rhs.
bluef(),
448 return Color::rgbaf(redf() / rhs.
redf(),
450 bluef() / rhs.
bluef(),
497static_assert(detail::rule_of_5<Color>(),
"must fulfill rule of 5");
502 return lhs.
red() == rhs.
red() &&
511 return !(lhs == rhs);
520 rhs.
bluef() + scalar,
530 rhs.
bluef() - scalar,
540 rhs.
bluef() * scalar,
550 rhs.
bluef() / scalar,
557namespace experimental
591 : m_steps(std::move(steps)),
603 m_steps.insert(m_steps.end(), steps.begin(), steps.end());
609 m_steps.emplace_back(color);
610 for (
auto& x : m_cache)
618 for (
auto& x : m_cache)
643 bool empty()
const {
return m_steps.empty(); }
646 size_t count()
const {
return m_steps.size(); }
654 mutable std::array<std::map<size_t, Color>, 3> m_cache{};
32 bit RGBA color.
Definition color.h:41
constexpr void red(ComponentType r)
Set RGBA component value individually from 0 to 255.
Definition color.h:134
constexpr Color operator-(T scalar) const
Color operator.
Definition color.h:390
EGT_NODISCARD constexpr ComponentType blue() const
Definition color.h:128
static EGT_NODISCARD constexpr Color pixel32(RGBAType c)
Create a Color from a 24 bit pixel representation.
Definition color.h:188
EGT_NODISCARD constexpr uint16_t pixel16() const
Get a 16 bit pixel representation of the Color.
Definition color.h:141
EGT_NODISCARD constexpr float greenf() const
Definition color.h:111
constexpr Color operator/(const Color &rhs) const
Color operator.
Definition color.h:446
constexpr void green(ComponentType g)
Definition color.h:135
EGT_NODISCARD constexpr Color tint(float factor) const
Create a tint (lighter) color based off this color given a factor.
Definition color.h:329
constexpr Color(ComponentType r, ComponentType g, ComponentType b, ComponentType a=255) noexcept
Create a color with the specified RGBA component values.
Definition color.h:98
EGT_NODISCARD static constexpr Color css(const char *hex)
Definition color.h:277
EGT_NODISCARD constexpr RGBAType pixel32() const
Get a 32 bit pixel representation of the Color.
Definition color.h:179
EGT_NODISCARD constexpr RGBAType pixel24() const
Get a 24 bit pixel representation of the Color.
Definition color.h:161
static EGT_NODISCARD Color css(const std::string &hex)
Create a Color with a hex CSS string.
Definition color.h:272
static EGT_NODISCARD constexpr Color pixel24(RGBAType c)
Create a Color from a 24 bit pixel representation.
Definition color.h:170
constexpr void bluef(float v)
Definition color.h:120
EGT_NODISCARD constexpr ComponentType red() const
Get RGBA component value as value from 0 to 255.
Definition color.h:126
Color operator*(const Color &rhs) const
Color operator.
Definition color.h:437
static EGT_NODISCARD constexpr Color rgbaf(float r, float g, float b, float a=1.0)
Create a color from float values.
Definition color.h:250
constexpr void blue(ComponentType b)
Definition color.h:136
static Color interp_hsl(const Color &a, const Color &b, float t)
Perform linear interpolation between two colors in the HSL color space.
EGT_NODISCARD constexpr ComponentType alpha() const
Definition color.h:129
constexpr Color operator+(const Color &rhs) const
Color operator.
Definition color.h:419
EGT_NODISCARD std::string hex() const
Return a string hex representation of the color.
constexpr void greenf(float v)
Definition color.h:119
constexpr void redf(float v)
Set RGBA component value as a float from 0.0 to 1.0.
Definition color.h:118
void get_hsvf(float &h, float &s, float &v, float *alpha=nullptr)
Get the hue, saturation, value, and alpha channel components of the color.
EGT_NODISCARD constexpr Color shade(float factor) const
Create a shade (darker) color based off this color given a factor.
Definition color.h:314
static Color interp_hsv(const Color &a, const Color &b, float t)
Perform linear interpolation between two colors in the HSV color space.
EGT_NODISCARD constexpr float bluef() const
Definition color.h:112
constexpr Color operator-(const Color &rhs) const
Color operator.
Definition color.h:428
EGT_NODISCARD constexpr float redf() const
Get RGBA component value as a float from 0.0 to 1.0.
Definition color.h:110
static EGT_NODISCARD Color hsvf(float h, float s, float v, float alpha=1.0)
Create a color from HSV values.
uint32_t RGBAType
Red/Green/Blue/Alpha type.
Definition color.h:47
Color & operator=(RGBAType c)
Assign an RGBA value.
Definition color.h:368
constexpr Color(const Color &color, ComponentType alpha) noexcept
Create a color from an existing color, but with the specified alpha value.
Definition color.h:80
static EGT_NODISCARD Color hslf(float h, float s, float l, float alpha=1.0)
Create a color from HSL values.
constexpr Color()=default
constexpr Color operator+(T scalar) const
Color operator.
Definition color.h:380
static Color interp_rgba(const Color &a, const Color &b, float t)
Perform linear interpolation between two colors in the RGB color space.
constexpr void alpha(ComponentType a)
Definition color.h:137
static EGT_NODISCARD constexpr Color pixel16(uint16_t c)
Create a Color from a 16 bit pixel representation.
Definition color.h:151
EGT_NODISCARD constexpr float alphaf() const
Definition color.h:113
EGT_NODISCARD Color hue(float h) const
Create a new color by applying a hue value.
Definition color.h:340
constexpr Color operator/(T scalar) const
Color operator.
Definition color.h:410
constexpr Color(RGBAType c) noexcept
Create a color with the specified RGBA value.
Definition color.h:63
EGT_NODISCARD constexpr ComponentType green() const
Definition color.h:127
void get_hslf(float &h, float &s, float &l, float *alpha=nullptr)
Get the hue, saturation, lightness, and alpha channel components of the color.
void get_rgbaf(float &r, float &g, float &b, float *alpha=nullptr)
Get the red, green, blue, and alpha channel components of the color.
constexpr Color operator*(T scalar) const
Color operator.
Definition color.h:400
constexpr void alphaf(float v)
Definition color.h:121
uint32_t RGBType
Red/Green/Blue type.
Definition color.h:45
uint32_t ComponentType
Single component of Red/Green/Blue/Alpha type.
Definition color.h:49
static EGT_NODISCARD constexpr Color rgb(RGBType c, ComponentType alpha=255) noexcept
Create a color from only a RGB value with separate alpha channel component.
Definition color.h:237
A ColorMap contains a series of sequential color steps that can be used for generating colors by inte...
Definition color.h:565
Color interp_cached(float t, size_t accuracy=1000) const
Get a color at the specified offset.
size_t count() const
Count the number of color steps.
Definition color.h:646
bool empty() const
Are there any color steps?
Definition color.h:643
std::vector< Color > StepsArray
Type used for color steps array.
Definition color.h:577
void steps(const StepsArray &steps)
Set the color steps.
Definition color.h:615
void step(const Color &color)
Append a color step.
Definition color.h:607
Interpolation
Interpolation color space method.
Definition color.h:570
ColorMap(StepsArray steps, Interpolation interp=Interpolation::rgba)
Definition color.h:590
ColorMap(Interpolation interp) noexcept
Definition color.h:582
const StepsArray & steps() const
Get a reference to the color steps array.
Definition color.h:640
Color interp(float t) const
Get a color at the specified offset.
StepsArray m_steps
Steps in the color map.
Definition color.h:651
ColorMap(std::initializer_list< T > steps, Interpolation interp=Interpolation::rgba)
Definition color.h:600
constexpr Color operator-(T scalar, const Color &rhs)
Color operator.
Definition color.h:526
constexpr bool operator!=(const Color &lhs, const Color &rhs)
Color operator.
Definition color.h:509
EGT_API std::ostream & operator<<(std::ostream &os, const Color &color)
Overloaded std::ostream insertion operator.
constexpr Color operator+(T scalar, const Color &rhs)
Color operator.
Definition color.h:516
constexpr Color operator/(T scalar, const Color &rhs)
Color operator.
Definition color.h:546
constexpr bool operator==(const Color &lhs, const Color &rhs)
Color operator.
Definition color.h:500
constexpr Color operator*(T scalar, const Color &rhs)
Color operator.
Definition color.h:536
EGT framework namespace.
Definition animation.h:24