4#ifndef GREM_CORE_DATA_COLOR_HPP
5#define GREM_CORE_DATA_COLOR_HPP
18[[nodiscard]]
constexpr float constexprFifthRoot(
float value) {
19 float previous = 0.0f;
21 while (x != previous) {
23 x = 0.2f * (4.0f * x + value / (x * x * x * x));
28[[nodiscard]]
constexpr float constexprTwelvthRoot(
float value) {
29 float previous = 0.0f;
31 while (x != previous) {
33 x = (1.0f / 12.0f) * (11.0f * x + value / (x * x * x * x * x * x * x * x * x * x * x));
38[[nodiscard]]
constexpr float powTwoPointFour(
float value) {
39 if (std::is_constant_evaluated()) {
41 return value * value * constexprFifthRoot(value * value);
43 return pow(value, 2.4f);
46[[nodiscard]]
constexpr float powInverseTwoPointFour(
float value) {
47 if (std::is_constant_evaluated()) {
49 return constexprTwelvthRoot(value * value * value * value * value);
51 return pow(value, 1.0f / 2.4f);
228 return (x <= 0.0031308f) ? x * 12.92f : 1.055f * detail::powInverseTwoPointFour(x) - 0.055f;
270 return (x <= 0.04045f) ? x / 12.92f : detail::powTwoPointFour((x + 0.055f) / 1.055f);
313 return vec4{
vec3{rgba} * rgba.w, rgba.w};
324 return vec4{(rgba.w > 0.0001f) ?
vec3{rgba} / rgba.w :
vec3{}, rgba.w};
351 [[nodiscard]]
static constexpr Color fromLinear(
float r,
float g,
float b,
float a = 1.0f) noexcept {
427 [[nodiscard]]
static constexpr Color fromSRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) noexcept {
432 bit_cast<u8norm>(a));
446 return fromSRGB(rgb.x, rgb.y, rgb.z, a);
461 return fromSRGB(rgb.x, rgb.y, rgb.z, a);
489 return fromSRGB(rgba.x, rgba.y, rgba.z, rgba.w);
504 return fromSRGB(rgba.x, rgba.y, rgba.z, rgba.w);
537 constexpr Color() noexcept = default;
546 [[nodiscard]] constexpr
bool operator==(const
Color& other) const noexcept = default;
624 [[nodiscard]]
constexpr float getAlpha() const noexcept {
701 rgba.x *= rgbCoefficients.x;
702 rgba.y *= rgbCoefficients.y;
703 rgba.z *= rgbCoefficients.z;
717 rgba *= rgbaCoefficients;
731 rgba.x /= rgbDenominators.x;
732 rgba.y /= rgbDenominators.y;
733 rgba.z /= rgbDenominators.z;
747 rgba /= rgbaDenominators;
760 return Color{a.rgba * b.rgba};
774 return Color{a.rgba * b};
788 return Color{a * b.rgba};
800 return Color{a.rgba / b.rgba};
814 return Color{a.rgba / b};
831 constexpr explicit Color(
vec4 rgba) noexcept
834 vec4 rgba{0.0f, 0.0f, 0.0f, 0.0f};
Floating-point linear RGBA color in the Rec.
Definition Color.hpp:60
static const Color FUCHSIA
Definition Color.hpp:119
constexpr vec3 toFloatSRGB() const noexcept
Convert the color to a vector with 3 floating-point sRGB components, XYZ, that are mapped from the co...
Definition Color.hpp:586
static const Color DARK_SALMON
Definition Color.hpp:104
static const Color SEA_SHELL
Definition Color.hpp:198
static const Color ANTIQUE_WHITE
Definition Color.hpp:72
static const Color CORNFLOWER_BLUE
Definition Color.hpp:88
static const Color DARK_BLUE
Definition Color.hpp:92
constexpr Color & operator*=(vec4 rgbaCoefficients) noexcept
Multiply the RGBA component values of this color by the corresponding values in a 4-component vector.
Definition Color.hpp:716
static const Color MEDIUM_TURQUOISE
Definition Color.hpp:165
static const Color LIGHT_PINK
Definition Color.hpp:145
static const Color NAVAJO_WHITE
Definition Color.hpp:171
static const Color BLANCHED_ALMOND
Definition Color.hpp:79
static const Color BLUE_VIOLET
Definition Color.hpp:81
static constexpr Color fromSRGB(u8norm r, u8norm g, u8norm b, u8norm a=1.0f) noexcept
Create a color from sRGB-encoded components.
Definition Color.hpp:408
static constexpr Color fromSRGB(u8vec4norm rgba) noexcept
Create a color from a vector of 3 sRGB-encoded components, XYZ, and 1 linear component,...
Definition Color.hpp:488
static const Color MEDIUM_BLUE
Definition Color.hpp:159
static const Color CYAN
Definition Color.hpp:91
static const Color LAVENDER
Definition Color.hpp:134
static const Color TOMATO
Definition Color.hpp:211
static const Color LIGHT_GREY
Definition Color.hpp:143
static const Color DARK_GREY
Definition Color.hpp:96
static const Color REBECCA_PURPLE
Definition Color.hpp:190
static const Color DARK_SEA_GREEN
Definition Color.hpp:105
static const Color POWDER_BLUE
Definition Color.hpp:188
static const Color VIOLET
Definition Color.hpp:213
static const Color LIGHT_STEEL_BLUE
Definition Color.hpp:151
static const Color DARK_MAGENTA
Definition Color.hpp:99
static const Color ALICE_BLUE
Definition Color.hpp:71
static const Color FLORAL_WHITE
Definition Color.hpp:117
friend constexpr Color operator*(const Color &a, const Color &b)
Get the result of component-wise multiplication between two colors.
Definition Color.hpp:759
friend constexpr Color operator/(const Color &a, float b)
Get the result of dividing a color by a scalar.
Definition Color.hpp:813
static constexpr vec4 convertSRGBToLinear(vec4 rgba)
Convert a color in the sRGB color space to linear space.
Definition Color.hpp:295
static const Color MEDIUM_PURPLE
Definition Color.hpp:161
static const Color BURLY_WOOD
Definition Color.hpp:83
static const Color GREY
Definition Color.hpp:125
static const Color MOCCASIN
Definition Color.hpp:170
static const Color DEEP_PINK
Definition Color.hpp:111
static const Color MEDIUM_VIOLET_RED
Definition Color.hpp:166
static const Color BLACK
Definition Color.hpp:78
static const Color MEDIUM_SPRING_GREEN
Definition Color.hpp:164
static const Color OLIVE_DRAB
Definition Color.hpp:175
static const Color DARK_ORANGE
Definition Color.hpp:101
static const Color NAVY
Definition Color.hpp:172
static const Color DARK_CYAN
Definition Color.hpp:93
static const Color CHARTREUSE
Definition Color.hpp:85
static const Color RED
Definition Color.hpp:191
static const Color SIENNA
Definition Color.hpp:199
static const Color MAGENTA
Definition Color.hpp:156
static const Color GOLD
Definition Color.hpp:122
static constexpr Color fromLinear(vec3 rgb, float a) noexcept
Create a color from a vector of 3 components, XYZ, and a scalar, A, that map to the linear color comp...
Definition Color.hpp:393
static const Color PALE_VIOLET_RED
Definition Color.hpp:182
static constexpr vec4 convertStraightToPremultipliedAlpha(vec4 rgba)
Convert a linear color from straight to pre-multiplied alpha.
Definition Color.hpp:312
static const Color PERU
Definition Color.hpp:185
static const Color OLD_LACE
Definition Color.hpp:173
static const Color DARK_VIOLET
Definition Color.hpp:110
static const Color SADDLE_BROWN
Definition Color.hpp:194
static const Color ROYAL_BLUE
Definition Color.hpp:193
static const Color DIM_GREY
Definition Color.hpp:114
static const Color LIGHT_SKY_BLUE
Definition Color.hpp:148
static constexpr vec3 convertSRGBToLinear(vec3 rgb)
Convert a color in the sRGB color space to linear space.
Definition Color.hpp:280
constexpr void setAlpha(float newAlpha) noexcept
Set the linear alpha component of the color.
Definition Color.hpp:633
static const Color DODGER_BLUE
Definition Color.hpp:115
static const Color LIGHT_SALMON
Definition Color.hpp:146
static constexpr Color fromAlpha(float a) noexcept
Create a color from a linear alpha component.
Definition Color.hpp:530
static const Color HONEY_DEW
Definition Color.hpp:128
static const Color SLATE_GRAY
Definition Color.hpp:203
TransferFunction
Color transfer function.
Definition Color.hpp:65
@ SRGB
sRGB transfer function.
Definition Color.hpp:67
@ LINEAR
Linear transfer function.
Definition Color.hpp:66
static const Color SKY_BLUE
Definition Color.hpp:201
static const Color AQUA
Definition Color.hpp:73
constexpr vec4 toFloatSRGBA() const noexcept
Convert the color to a vector with 3 floating-point sRGB components, XYZ, and 1 linear component,...
Definition Color.hpp:610
constexpr Color & operator*=(const Color &other) noexcept
Multiply the component values of this color with their respective component values in another color.
Definition Color.hpp:645
static const Color DIM_GRAY
Definition Color.hpp:113
static const Color MIDNIGHT_BLUE
Definition Color.hpp:167
static const Color GOLDEN_ROD
Definition Color.hpp:123
static constexpr float convertLinearToSRGB(float x)
Convert a color component in linear space to the sRGB color space.
Definition Color.hpp:227
static const Color PEACH_PUFF
Definition Color.hpp:184
static constexpr vec4 convertLinearToSRGB(vec4 rgba)
Convert a color in linear space to the sRGB color space.
Definition Color.hpp:253
static const Color KHAKI
Definition Color.hpp:133
static const Color SPRING_GREEN
Definition Color.hpp:206
static const Color ROSY_BROWN
Definition Color.hpp:192
static constexpr Color fromSRGB(vec3 rgb, float a=1.0f) noexcept
Create a color from a vector of 3 floating-point sRGB components, XYZ, and a scalar,...
Definition Color.hpp:474
static const Color DARK_TURQUOISE
Definition Color.hpp:109
static const Color INDIAN_RED
Definition Color.hpp:130
static constexpr Color fromSRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255) noexcept
Create a color from unnormalized sRGB-encoded components.
Definition Color.hpp:427
static const Color GAINSBORO
Definition Color.hpp:120
static constexpr Color fromLinear(vec4 rgba) noexcept
Construct a color from a vector of 4 components, XYZW, that map to the linear color components RGBA,...
Definition Color.hpp:379
static const Color GRAY
Definition Color.hpp:124
static const Color PLUM
Definition Color.hpp:187
constexpr Color & operator*=(vec3 rgbCoefficients) noexcept
Multiply the RGB component values of this color by the corresponding values in a 3-component vector.
Definition Color.hpp:700
static const Color OLIVE
Definition Color.hpp:174
static const Color CADET_BLUE
Definition Color.hpp:84
static const Color MEDIUM_AQUA_MARINE
Definition Color.hpp:158
static const Color CORNSILK
Definition Color.hpp:89
friend constexpr Color mix(Color a, Color b, float alpha)
Linearly blend between two colors based on an alpha value.
Definition Color.hpp:826
static const Color DARK_GREEN
Definition Color.hpp:97
static const Color DARK_GRAY
Definition Color.hpp:95
static constexpr Color fromSRGB(u8vec3 rgb, uint8_t a=255) noexcept
Create a color from a vector of 3 unnormalized sRGB-encoded components, XYZ, and a scalar,...
Definition Color.hpp:460
static const Color LIGHT_GOLDEN_ROD_YELLOW
Definition Color.hpp:141
static constexpr Color fromSRGB(u8vec3norm rgb, u8norm a=1.0f) noexcept
Create a color from a vector of 3 sRGB-encoded components, XYZ, and a scalar, A, that map to the colo...
Definition Color.hpp:445
static const Color LIGHT_SLATE_GRAY
Definition Color.hpp:149
static const Color SLATE_BLUE
Definition Color.hpp:202
static const Color DARK_SLATE_GREY
Definition Color.hpp:108
static constexpr Color fromLinear(vec3 rgb) noexcept
Create a color from a vector of 3 components, XYZ, that map to the linear color components RGB,...
Definition Color.hpp:366
static const Color INVISIBLE
Definition Color.hpp:70
static constexpr vec3 convertLinearToSRGB(vec3 rgb)
Convert a color in linear space to the sRGB color space.
Definition Color.hpp:238
static const Color PALE_GREEN
Definition Color.hpp:180
static const Color MEDIUM_SLATE_BLUE
Definition Color.hpp:163
static const Color LEMON_CHIFFON
Definition Color.hpp:137
static const Color DARK_GOLDEN_ROD
Definition Color.hpp:94
static const Color IVORY
Definition Color.hpp:132
constexpr vec4 toLinearRGBA() const noexcept
Convert the color to a vector with 4 components, XYZW, that are mapped from the color components RGBA...
Definition Color.hpp:564
static const Color GHOST_WHITE
Definition Color.hpp:121
friend constexpr Color operator*(const Color &a, float b)
Get the result of multiplying a color by a scalar.
Definition Color.hpp:773
constexpr u8vec4norm toSRGBA() const noexcept
Convert the color to a vector with 3 sRGB-encoded components, XYZ, and 1 linear component,...
Definition Color.hpp:598
static const Color AZURE
Definition Color.hpp:75
constexpr Color & operator/=(float denominator) noexcept
Divide the component values of this color by a scalar.
Definition Color.hpp:686
static constexpr vec4 convertPremultipliedToStraightAlpha(vec4 rgba)
Convert a linear color from pre-multiplied to straight alpha.
Definition Color.hpp:323
static const Color SLATE_GREY
Definition Color.hpp:204
static const Color LAWN_GREEN
Definition Color.hpp:136
static const Color LIME_GREEN
Definition Color.hpp:154
static const Color LIGHT_SLATE_GREY
Definition Color.hpp:150
static const Color MISTY_ROSE
Definition Color.hpp:169
constexpr Color & operator/=(const Color &other) noexcept
Divide the component values of this color by their respective component values in another color.
Definition Color.hpp:672
static const Color LINEN
Definition Color.hpp:155
static const Color BLUE
Definition Color.hpp:80
constexpr Color & operator*=(float coefficient) noexcept
Multiply the component values of this color by a scalar.
Definition Color.hpp:659
static const Color STEEL_BLUE
Definition Color.hpp:207
static const Color GREEN_YELLOW
Definition Color.hpp:127
static const Color DARK_OLIVE_GREEN
Definition Color.hpp:100
static const Color LIGHT_GRAY
Definition Color.hpp:142
static const Color DARK_RED
Definition Color.hpp:103
static const Color DARK_KHAKI
Definition Color.hpp:98
static const Color ORCHID
Definition Color.hpp:178
static const Color SNOW
Definition Color.hpp:205
constexpr u8vec3norm toSRGB() const noexcept
Convert the color to a vector with 3 sRGB-encoded components, XYZ, that are mapped from the color com...
Definition Color.hpp:575
static const Color BISQUE
Definition Color.hpp:77
static const Color SANDY_BROWN
Definition Color.hpp:196
static const Color INDIGO
Definition Color.hpp:131
static const Color DARK_SLATE_BLUE
Definition Color.hpp:106
static const Color TURQUOISE
Definition Color.hpp:212
constexpr float getAlpha() const noexcept
Get the linear alpha component of the color.
Definition Color.hpp:624
constexpr vec3 toLinearRGB() const noexcept
Convert the color to a vector with 3 components, XYZ, that are mapped from the color components RGB,...
Definition Color.hpp:554
static const Color DEEP_SKY_BLUE
Definition Color.hpp:112
static const Color LAVENDER_BLUSH
Definition Color.hpp:135
static const Color YELLOW_GREEN
Definition Color.hpp:218
constexpr Color() noexcept=default
Construct a transparent color with a value of 0 in all components.
static const Color BROWN
Definition Color.hpp:82
static const Color SEA_GREEN
Definition Color.hpp:197
static const Color PAPAYA_WHIP
Definition Color.hpp:183
static const Color HOT_PINK
Definition Color.hpp:129
static const Color TEAL
Definition Color.hpp:209
static const Color PALE_GOLDEN_ROD
Definition Color.hpp:179
static const Color LIGHT_GREEN
Definition Color.hpp:144
static const Color YELLOW
Definition Color.hpp:217
static const Color AQUAMARINE
Definition Color.hpp:74
static const Color LIGHT_YELLOW
Definition Color.hpp:152
constexpr Color & operator/=(vec4 rgbaDenominators) noexcept
Divide the RGBA component values of this color by the corresponding values in a 4-component vector.
Definition Color.hpp:746
static const Color FIRE_BRICK
Definition Color.hpp:116
static constexpr Color fromLinear(float x, float a=1.0f) noexcept
Create a grayscale color from a linear scalar.
Definition Color.hpp:336
static const Color ORANGE_RED
Definition Color.hpp:177
static const Color SALMON
Definition Color.hpp:195
static const Color LIME
Definition Color.hpp:153
static const Color PURPLE
Definition Color.hpp:189
static const Color MAROON
Definition Color.hpp:157
static const Color LIGHT_CYAN
Definition Color.hpp:140
static constexpr Color fromSRGB(vec4 rgba) noexcept
Create a color from a vector of 3 floating-point sRGB components, XYZ, and 1 linear component,...
Definition Color.hpp:518
static const Color SILVER
Definition Color.hpp:200
static const Color GREEN
Definition Color.hpp:126
static const Color LIGHT_BLUE
Definition Color.hpp:138
static const Color PALE_TURQUOISE
Definition Color.hpp:181
static const Color LIGHT_CORAL
Definition Color.hpp:139
static const Color THISTLE
Definition Color.hpp:210
static const Color MINT_CREAM
Definition Color.hpp:168
static const Color FOREST_GREEN
Definition Color.hpp:118
static const Color WHITE
Definition Color.hpp:215
static const Color ORANGE
Definition Color.hpp:176
static const Color CORAL
Definition Color.hpp:87
static const Color DARK_ORCHID
Definition Color.hpp:102
static constexpr float convertSRGBToLinear(float x)
Convert a color component in the sRGB color space to linear space.
Definition Color.hpp:269
static const Color MEDIUM_ORCHID
Definition Color.hpp:160
constexpr Color & operator/=(vec3 rgbDenominators) noexcept
Divide the RGB component values of this color by the corresponding values in a 3-component vector.
Definition Color.hpp:730
static constexpr Color fromSRGB(u8vec4 rgba) noexcept
Create a color from a vector of 3 unnormalized sRGB-encoded components, XYZ, and 1 linear component,...
Definition Color.hpp:503
static const Color BEIGE
Definition Color.hpp:76
static const Color CRIMSON
Definition Color.hpp:90
static const Color WHITE_SMOKE
Definition Color.hpp:216
static const Color LIGHT_SEA_GREEN
Definition Color.hpp:147
static const Color CHOCOLATE
Definition Color.hpp:86
static const Color DARK_SLATE_GRAY
Definition Color.hpp:107
static const Color WHEAT
Definition Color.hpp:214
static const Color PINK
Definition Color.hpp:186
static const Color TAN
Definition Color.hpp:208
friend constexpr Color operator/(const Color &a, const Color &b)
Get the result of component-wise division between two colors.
Definition Color.hpp:799
friend constexpr Color operator*(float a, const Color &b)
Get the result of multiplying a scalar by a color.
Definition Color.hpp:787
static constexpr Color fromLinear(float r, float g, float b, float a=1.0f) noexcept
Create a color from linear RGB components.
Definition Color.hpp:351
static const Color MEDIUM_SEA_GREEN
Definition Color.hpp:162
vec< 4, float > vec4
Definition math.hpp:440
vec< 3, u8norm > u8vec3norm
Definition math.hpp:463
vec< 3, uint8_t > u8vec3
Definition math.hpp:403
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL pow(T a, T b) noexcept
Calculate the result of raising a given value to a given power.
Definition math.hpp:1179
detail::NormalizedInteger< uint8_t > u8norm
Definition fundamentals.hpp:377
vec< 3, float > vec3
Definition math.hpp:439
vec< 4, uint8_t > u8vec4
Definition math.hpp:404
vec< 4, u8norm > u8vec4norm
Definition math.hpp:464