9#include <glm/common.hpp>
10#include <glm/ext/vector_float4.hpp>
12#include "constexpr_math.h"
49#define DEF_COLOR(name, r, g, b) \
50 constexpr color_t get_##name(float alpha) { return color_t{ float(r), float(g), float(b), alpha }; } \
51 constexpr inline color_t name = get_##name(1.0f);
52#define DEF_COLORS(name, r, g, b) \
53 DEF_COLOR(name, r, g, b) \
54 constexpr color_t get_dark_##name(float alpha) { return color_t{ float(r) * 0.5f, float(g) * 0.5f, float(b) * 0.5f, alpha }; } \
55 constexpr inline color_t dark_##name = get_dark_##name(1.0f); \
56 constexpr color_t get_light_##name(float alpha) { return color_t{ r + float(1.0f-r) * 0.5f, g + float(1.0f-g) * 0.5f, b + float(1.0f-b) * 0.5f, alpha }; } \
57 constexpr inline color_t light_##name = get_light_##name(1.0f);
59 DEF_COLORS(red, 1, 0, 0)
60 DEF_COLORS(green, 0, 1, 0)
61 DEF_COLORS(blue, 0, 0, 1)
62 DEF_COLORS(yellow, 1, 1, 0)
63 DEF_COLORS(magenta, 1, 0, 1)
64 DEF_COLORS(cyan, 0, 1, 1)
65 DEF_COLORS(gray, 0.5f, 0.5f, 0.5f)
66 DEF_COLORS(grey, 0.5f, 0.5f, 0.5f)
68 DEF_COLORS(orange, 1, 0.65f, 0)
69 DEF_COLORS(brown, 0.59f, 0.29f, 0)
71 DEF_COLOR(black, 0, 0, 0)
72 DEF_COLOR(white, 1, 1, 1)
73 constexpr inline color_t transparent = get_black(0.0f);
88 return glm::clamp(
color_t(
color.x,
color.y,
color.z,
color.w),
color_t{ 0,0,0,0 },
color_t{ 1,1,1,1 });
115 constexpr double m = 1.0156862745098039215686274509804;
154 constexpr float b2f(
uint32_t byte) {
return (
byte & 0xFF) / 255.0f; }
156 constexpr uint32_t f2u4(
float b1,
float b2,
float b3,
float b4) {
return (f2b(
b1) << 24) | (f2b(
b2) << 16) | (f2b(
b3) << 8) | f2b(
b4); }
157 constexpr uint32_t f2u4(
float b1,
float b2,
float b3) {
return (f2b(
b1) << 16) | (f2b(
b2) << 8) | f2b(
b3); }
160 template <
typename T>
constexpr auto min(
const T& x,
const T& y,
const T&
z) {
return glm::min(glm::min(x, y),
z); }
161 template <
typename T>
constexpr auto max(
const T& x,
const T& y,
const T&
z) {
return glm::max(glm::max(x, y),
z); }
190 template <std::same_as<color_rgba_u32_t> TO>
196 template <std::same_as<color_abgr_u32_t> TO>
202 template <
typename TO,
typename FROM>
203 TO color_cast(
FROM const& from)
209 constexpr glm::tvec4<uint8_t> to_u8(
color_t const&
rgba) {
return { detail::f2b(
rgba.x), detail::f2b(
rgba.y), detail::f2b(
rgba.z), detail::f2b(
rgba.w) }; }
214 const auto hue = hsva->r;
216 const auto value = hsva->b;
217 const auto alpha = hsva->a;
241 const auto delta = max - min;
249 else if (
rgba.y == max)
257 (max != 0) ? (
delta / max) : 0,
280 std::from_chars(str + 3, str + 4, a, 16); a = a << 4 | a;
283 std::from_chars(str + 0, str + 1,
r, 16);
r =
r << 4 |
r;
284 std::from_chars(str + 1, str + 2,
g, 16);
g =
g << 4 |
g;
285 std::from_chars(str + 2, str + 3, b, 16); b = b << 4 | b;
288 std::from_chars(str + 6, str + 8, a, 16);
291 std::from_chars(str + 0, str + 2,
r, 16);
292 std::from_chars(str + 2, str + 4,
g, 16);
293 std::from_chars(str + 4, str + 6, b, 16);
295 default:
throw "invalid number of characters";
297 return {
r / 255.0f,
g / 255.0f, b / 255.0f, a / 255.0f };
317 return R*R +
G*
G +
B*
B;
320 return R*R +
G*
G +
B*
B +
A*
A;
325 const auto diff_a = a - *this;
326 const auto diff_b = b - *this;
327 return diff_a.GetDotProduct() < diff_b.GetDotProduct();
333 const auto diff_a = a.FullDifference(*this);
334 const auto diff_b = b.FullDifference(*this);
335 return diff_a.GetFullDotProduct() < diff_b.GetFullDotProduct();
347 template <GrayscaleType TYPE = GrayscaleType::Luminosity>
352 case GrayscaleType::Average:
return Color((R+
G+
B)/3.0,
A);
354 case GrayscaleType::Luminosity:
return Color(R*0.21 +
G*0.72 +
B*0.07,
A);
359 const auto oma = 1.0f -
A;
363 template <
typename FUNC>
423 return colors::FromRGBInt(
hex);
425 else if (str.size() == 8)
428 return colors::FromRGBAInt(
hex);
430 else if (str.size() == 3)
433 hex = ((
hex << 12) & 0xF00000) | ((
hex << 8) & 0xF000) | ((
hex << 4) & 0xF0);
435 return colors::FromRGBInt(
hex);
constexpr auto bit_count
Equal to the number of bits in the type.
constexpr float luminance(color_t const &color)
Get brightness of color.
named< uint32_t, "color_abgr_u32"> color_abgr_u32_t
Represent a packed ABGR color with 8 bits per pixel.
color_rgba_t color_t
Default color_t type is RGBA.
constexpr uint32_t to_u32_bgra(color_t const &rgba)
Creates an 8bpp BGRA integer from a color.
color_space
Implementation note: These functions use the .xyzw members instead of the .rgba members because acces...
constexpr uint32_t to_u32_rgb(color_t const &rgba)
Creates a 32 bitbpp RGB integer from a color, with the most significant 8 bits set to 0.
constexpr color_t premultiplied(color_t const &color)
Returns the color multiplied by its own alpha.
constexpr color_t from_u32_rgba(uint32_t rgb)
Gets a color from an RGBA 8bpp integer, with R being most significant.
constexpr color_t from_u32_bgr(uint32_t rgb)
Gets a color from an BGR 8bpp integer, with R being least significant.
constexpr color_t lighten(color_t const &color, float coef)
Returns a color lightened by a coefficient.
constexpr color_t saturated(color_t const &color)
Returns a color with all elements clamped between 0 and 1.
constexpr color_t to_rgb(color_hsva_t const &hsva)
Converts a HSVA color to RGBA space.
constexpr uint32_t to_u32_rgba(color_t const &rgba)
Creates an 8bpp RGBA integer from a color.
constexpr uint32_t to_u32_abgr(color_t const &rgba)
Creates an 8bpp ABGR integer from a color.
glm::vec4 color_rgba_t
Represents a color in RGBA color space, with 0.0-1.0 float elements.
constexpr color_t contrast(color_t const &color, float contrast)
Returns a color with its contrast changed.
named< uint32_t, "color_rgba_u32"> color_rgba_u32_t
Represent a packed RGBA color with 8 bits per pixel.
constexpr color_t contrast2(color_t const &color, float contrast)
Returns a color with its contrast changed This uses a differen algorithm and contrast value than cont...
constexpr color_t from_u32_abgr(uint32_t rgb)
Gets a color from an ABGR 8bpp integer, with A being most significant, and R being least significant.
constexpr color_t from_u32_argb(uint32_t rgb)
Gets a color from an ARGB 8bpp integer, with A being most significant, and B being least significant.
constexpr color_rgba_t from_html(const char *str, size_t n)
Converts a HTML color string (like #FBA or fafafa) to an RGBA color.
constexpr color_t gamma_correct(color_t const &color, const float gamma)
Returns a gamma corrected color.
constexpr color_t contrasting(color_t const &color)
Returns a color that's a good contrasting color for the original.
constexpr color_hsva_t to_hsv(color_t const &rgba)
Converts an RGBA color to HSVA space.
constexpr color_t inverted(color_t const &color)
Returns an inverted color, i.e. with (1.0-x) on all of its elements, excluding its alpha.
constexpr uint32_t to_u32_argb(color_t const &rgba)
Creates an 8bpp ARGB integer from a color.
constexpr color_t desaturated(color_t const &color, float desaturation)
Returns a color sapped of desaturation percent (0-1) of its saturation.
constexpr color_t from_u32_rgb(uint32_t rgb)
Gets a color from an RGB 8bpp integer, with R being most significant.
constexpr color_t from_u32_bgra(uint32_t rgb)
Gets a color from an BGRA 8bpp integer, with A being least significant, and B being most significant.
constexpr uint32_t to_u32_bgr(color_t const &rgba)
Creates a 32 bitbpp BGR integer from a color, with the most significant 8 bits set to 0.
named< glm::vec4, "color_hsva"> color_hsva_t
Represents a color in HSVA color space, with H=0.0-6.0, S=0.0-1.0, V=0.0-1.0, A=0....
The below code is based on Sun's libm library code, which is licensed under the following license:
Primary namespace for everything in this library.