libs/base/src/base/colors.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 8 | namespace eu | ||
| 9 | { | ||
| 10 | |||
| 11 | /// Represents color in gamma (non-linear) space (aka sRGB). | ||
| 12 | /// @see Lin_rgb | ||
| 13 | struct 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 | |||
| 26 | /// Represents a linear sRGB color. | ||
| 27 | /// @see Rgb | ||
| 28 | struct Lin_rgb | ||
| 29 | { | ||
| 30 | float r; float g; float b; | ||
| 31 | }; | ||
| 32 | |||
| 33 | /// Represents a color in the OKhsv color space | ||
| 34 | struct OkHsv | ||
| 35 | { | ||
| 36 | An hue; | ||
| 37 | float saturation; | ||
| 38 | float value; | ||
| 39 | }; | ||
| 40 | |||
| 41 | /// Represents a color in the OKhsl color space | ||
| 42 | struct OkHsl | ||
| 43 | { | ||
| 44 | An hue; | ||
| 45 | float saturation; | ||
| 46 | float lightness; | ||
| 47 | }; | ||
| 48 | |||
| 49 | /// Represents a color in the sRGB color space. | ||
| 50 | struct Hsl | ||
| 51 | { | ||
| 52 | An hue; | ||
| 53 | float saturation; | ||
| 54 | float lightness; | ||
| 55 | }; | ||
| 56 | |||
| 57 | /// Represents a color in the OKlch color space. | ||
| 58 | struct OkLch | ||
| 59 | { | ||
| 60 | float l; | ||
| 61 | float c; | ||
| 62 | An h; | ||
| 63 | }; | ||
| 64 | |||
| 65 | /// Represents a color in the OKlab color space. | ||
| 66 | struct OkLab | ||
| 67 | { | ||
| 68 | float l; | ||
| 69 | float a; | ||
| 70 | float b; | ||
| 71 | }; | ||
| 72 | |||
| 73 | /// Represents a color with an alpha component. The color is stored in gamma (non-linear) space (aka sRGB). | ||
| 74 | struct Color | ||
| 75 | { | ||
| 76 | Rgb rgb; | ||
| 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? | ||
| 88 | float linear_from_srgb(float value, float gamma); | ||
| 89 | Lin_rgb linear_from_srgb(const Rgb& value, float gamma); | ||
| 90 | |||
| 91 | float linear_from_srgb(float value); | ||
| 92 | Lin_rgb linear_from_srgb(const Rgb& value); | ||
| 93 | |||
| 94 | float srgb_from_linear(float value); | ||
| 95 | Rgb srgb_from_linear(const Lin_rgb& value); | ||
| 96 | |||
| 97 | |||
| 98 | // oklab & oklch | ||
| 99 | // from: https://bottosson.github.io/posts/oklab/ | ||
| 100 | OkLab oklab_from_linear(const Lin_rgb& c); | ||
| 101 | Lin_rgb linear_from_oklab(const OkLab& c); | ||
| 102 | OkLch oklch_from_oklab(const OkLab& c); | ||
| 103 | OkLab oklab_from_oklch(const OkLch& c); | ||
| 104 | |||
| 105 | |||
| 106 | // gammut clipping functions from https://bottosson.github.io/posts/gamutclipping/ | ||
| 107 | Lin_rgb gamut_clip_preserve_chroma(const Lin_rgb& rgb); | ||
| 108 | Lin_rgb gamut_clip_project_to_0_5(const Lin_rgb& rgb); | ||
| 109 | Lin_rgb gamut_clip_project_to_l_cusp(const Lin_rgb& rgb); | ||
| 110 | Lin_rgb gamut_clip_adaptive_L0_0_5(const Lin_rgb& rgb, float alpha = 0.05f); | ||
| 111 | Lin_rgb gamut_clip_adaptive_l0_l_cusp(const Lin_rgb& rgb, float alpha = 0.05f); | ||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | Rgb srgb_from_okhsv(const OkHsv& hsv); | ||
| 117 | OkHsv okhsv_from_srgb(const Rgb& rgb); | ||
| 118 | |||
| 119 | Rgb srgb_from_okhsl(const OkHsl& hsl); | ||
| 120 | OkHsl okhsl_from_srgb(const Rgb& rgb); | ||
| 121 | |||
| 122 | // srgb from hsl (not ok hsl) | ||
| 123 | Rgb srgb_from_hsl(const Hsl& hsl); | ||
| 124 | |||
| 125 | Lin_rgb keep_within(Lin_rgb c); | ||
| 126 | |||
| 127 | } | ||
| 128 | |||
| 129 | namespace eu::hues | ||
| 130 | { | ||
| 131 | // https://tympanus.net/codrops/css_reference/hsl/ | ||
| 132 | constexpr eu::An red = An::from_degrees(0.0f); | ||
| 133 | constexpr eu::An orange = An::from_degrees(30.0f); | ||
| 134 | constexpr eu::An yellow = An::from_degrees(60.0f); | ||
| 135 | constexpr eu::An green = An::from_degrees(120.0f); | ||
| 136 | constexpr eu::An cyan = An::from_degrees(180.0f); | ||
| 137 | constexpr eu::An blue = An::from_degrees(240.0f); | ||
| 138 | constexpr eu::An magenta = An::from_degrees(300.0f); | ||
| 139 | } | ||
| 140 | |||
| 141 | namespace eu::colors | ||
| 142 | { | ||
| 143 | |||
| 144 | /// helper function to create a rgb color with code similar to css causing some editors to display a color box | ||
| 145 | constexpr 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 | |||
| 150 | /// helper function to create a hsl color with code similar to css causing some editors to display a color box | ||
| 151 | constexpr 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 | |||
| 156 | constexpr auto white = eu::Rgb{1.0f, 1.0f, 1.0f}; | ||
| 157 | constexpr auto black = eu::Rgb{0.0f, 0.0f, 0.0f}; | ||
| 158 | |||
| 159 | // https://www.nature.com/articles/nmeth.1618 | ||
| 160 | constexpr eu::Rgb orange = rgb(230, 159, 0); | ||
| 161 | constexpr eu::Rgb blue_sky = rgb(86, 180, 233); | ||
| 162 | constexpr eu::Rgb green_bluish = rgb(0, 158, 115); | ||
| 163 | constexpr eu::Rgb yellow = rgb(240, 228, 66); | ||
| 164 | constexpr eu::Rgb blue = rgb(0, 114, 178); | ||
| 165 | constexpr eu::Rgb red_vermillion = rgb(213, 94, 0); | ||
| 166 | constexpr eu::Rgb purple_redish = rgb(204, 121, 167); | ||
| 167 | |||
| 168 | } // namespace eu::colors | ||
| 169 |