Euphoria
colors.h
Go to the documentation of this file.
1#pragma once
2
3#include "base/angle.h"
4#include "base/vec3.h"
5
6// todo(Gustav): split into 2 headers, one color (rgb + linear) and one extra
7
8namespace eu
9{
10
13struct Rgb
14{
15 float r;
16 float g;
17 float b;
18
19 constexpr Rgb(float red, float green, float blue)
20 : r(red)
21 , g(green)
22 , b(blue)
23 {}
24};
25
28struct Lin_rgb
29{
30 float r; float g; float b;
31};
32
34struct OkHsv
35{
38 float value;
39};
40
42struct OkHsl
43{
46 float lightness;
47};
48
50struct Hsl
51{
54 float lightness;
55};
56
58struct OkLch
59{
60 float l;
61 float c;
63};
64
66struct OkLab
67{
68 float l;
69 float a;
70 float b;
71};
72
74struct Color
75{
77 float alpha;
78
79 constexpr Color(const Rgb& a_rgb, float a_alpha = 1.0f)
80 : rgb(a_rgb)
81 , alpha(a_alpha)
82 {
83 }
84};
85
86// linear-space gamma-space
87// todo(Gustav): should the functions that take gamma still be used?
88float linear_from_srgb(float value, float gamma);
89Lin_rgb linear_from_srgb(const Rgb& value, float gamma);
90
91float linear_from_srgb(float value);
93
94float srgb_from_linear(float value);
96
97
98// oklab & oklch
99// from: https://bottosson.github.io/posts/oklab/
104
105
106// gammut clipping functions from https://bottosson.github.io/posts/gamutclipping/
110Lin_rgb gamut_clip_adaptive_L0_0_5(const Lin_rgb& rgb, float alpha = 0.05f);
111Lin_rgb gamut_clip_adaptive_l0_l_cusp(const Lin_rgb& rgb, float alpha = 0.05f);
112
113
114
115
118
121
122// srgb from hsl (not ok hsl)
124
126
127}
128
129namespace eu::hues
130{
131// https://tympanus.net/codrops/css_reference/hsl/
132constexpr eu::An red = An::from_degrees(0.0f);
133constexpr eu::An orange = An::from_degrees(30.0f);
134constexpr eu::An yellow = An::from_degrees(60.0f);
135constexpr eu::An green = An::from_degrees(120.0f);
136constexpr eu::An cyan = An::from_degrees(180.0f);
137constexpr eu::An blue = An::from_degrees(240.0f);
138constexpr eu::An magenta = An::from_degrees(300.0f);
139}
140
141namespace eu::colors
142{
143
145constexpr eu::Rgb rgb(int r, int g, int b)
146{
147 return {static_cast<float>(r) / 255.0f, static_cast<float>(g) / 255.0f, static_cast<float>(b) / 255.0f};
148}
149
151constexpr Hsl hsl(int deg, float s, float l)
152{
153 return {.hue = An::from_degrees(static_cast<float>(deg)), .saturation = s / 100.0f, .lightness = l / 100.0f};
154}
155
156constexpr auto white = eu::Rgb{1.0f, 1.0f, 1.0f};
157constexpr auto black = eu::Rgb{0.0f, 0.0f, 0.0f};
158
159// https://www.nature.com/articles/nmeth.1618
160constexpr eu::Rgb orange = rgb(230, 159, 0);
161constexpr eu::Rgb blue_sky = rgb(86, 180, 233);
162constexpr eu::Rgb green_bluish = rgb(0, 158, 115);
163constexpr eu::Rgb yellow = rgb(240, 228, 66);
164constexpr eu::Rgb blue = rgb(0, 114, 178);
165constexpr eu::Rgb red_vermillion = rgb(213, 94, 0);
166constexpr eu::Rgb purple_redish = rgb(204, 121, 167);
167
168} // namespace eu::colors
constexpr eu::Rgb yellow
Definition colors.h:163
constexpr eu::Rgb red_vermillion
Definition colors.h:165
constexpr eu::Rgb blue_sky
Definition colors.h:161
constexpr auto white
Definition colors.h:156
constexpr eu::Rgb orange
Definition colors.h:160
constexpr eu::Rgb green_bluish
Definition colors.h:162
constexpr eu::Rgb rgb(int r, int g, int b)
helper function to create a rgb color with code similar to css causing some editors to display a colo...
Definition colors.h:145
constexpr auto black
Definition colors.h:157
constexpr Hsl hsl(int deg, float s, float l)
helper function to create a hsl color with code similar to css causing some editors to display a colo...
Definition colors.h:151
constexpr eu::Rgb purple_redish
Definition colors.h:166
constexpr eu::Rgb blue
Definition colors.h:164
constexpr eu::An cyan
Definition colors.h:136
constexpr eu::An orange
Definition colors.h:133
constexpr eu::An magenta
Definition colors.h:138
constexpr eu::An blue
Definition colors.h:137
constexpr eu::An green
Definition colors.h:135
constexpr eu::An yellow
Definition colors.h:134
constexpr eu::An red
Definition colors.h:132
Definition assert.h:111
Rgb srgb_from_hsl(const Hsl &hsl)
float srgb_from_linear(float value)
Rgb srgb_from_okhsv(const OkHsv &hsv)
OkLch oklch_from_oklab(const OkLab &c)
OkHsv okhsv_from_srgb(const Rgb &rgb)
Lin_rgb gamut_clip_adaptive_l0_l_cusp(const Lin_rgb &rgb, float alpha=0.05f)
Lin_rgb linear_from_oklab(const OkLab &c)
OkLab oklab_from_linear(const Lin_rgb &c)
Lin_rgb gamut_clip_preserve_chroma(const Lin_rgb &rgb)
OkHsl okhsl_from_srgb(const Rgb &rgb)
Lin_rgb keep_within(Lin_rgb c)
Lin_rgb gamut_clip_project_to_0_5(const Lin_rgb &rgb)
Lin_rgb gamut_clip_project_to_l_cusp(const Lin_rgb &rgb)
Rgb srgb_from_okhsl(const OkHsl &hsl)
float linear_from_srgb(float value, float gamma)
OkLab oklab_from_oklch(const OkLch &c)
Lin_rgb gamut_clip_adaptive_L0_0_5(const Lin_rgb &rgb, float alpha=0.05f)
An angle in both degrees and radians.
Definition angle.h:13
static constexpr An from_degrees(float degrees)
Definition angle.h:15
Represents a color with an alpha component. The color is stored in gamma (non-linear) space (aka sRGB...
Definition colors.h:75
float alpha
Definition colors.h:77
constexpr Color(const Rgb &a_rgb, float a_alpha=1.0f)
Definition colors.h:79
Rgb rgb
Definition colors.h:76
Represents a color in the sRGB color space.
Definition colors.h:51
float lightness
Definition colors.h:54
An hue
Definition colors.h:52
float saturation
Definition colors.h:53
Represents a linear sRGB color.
Definition colors.h:29
float g
Definition colors.h:30
float b
Definition colors.h:30
float r
Definition colors.h:30
Represents a color in the OKhsl color space.
Definition colors.h:43
float lightness
Definition colors.h:46
float saturation
Definition colors.h:45
An hue
Definition colors.h:44
Represents a color in the OKhsv color space.
Definition colors.h:35
float saturation
Definition colors.h:37
An hue
Definition colors.h:36
float value
Definition colors.h:38
Represents a color in the OKlab color space.
Definition colors.h:67
float b
Definition colors.h:70
float l
Definition colors.h:68
float a
Definition colors.h:69
Represents a color in the OKlch color space.
Definition colors.h:59
float l
Definition colors.h:60
float c
Definition colors.h:61
Represents color in gamma (non-linear) space (aka sRGB).
Definition colors.h:14
constexpr Rgb(float red, float green, float blue)
Definition colors.h:19
float b
Definition colors.h:17
float g
Definition colors.h:16
float r
Definition colors.h:15