HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
sRGB.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2020-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
9#pragma once
10
11#include "../utility/utility.hpp"
12#include "../geometry/geometry.hpp"
13#include "color_intf.hpp"
14#include "../macros.hpp"
15#include <cmath>
16#include <array>
17#include <algorithm>
18#include <string_view>
19#include <format>
20
21hi_export_module(hikogui.color.sRGB);
22
23hi_warning_push();
24// C26426: Global initializer calls a non-constexpr function '...' (i.22).
25// std::pow() is not constexpr and needed to fill in the gamma conversion tables.
26hi_warning_ignore_msvc(26426);
27
28hi_export namespace hi { inline namespace v1 {
29
34 matrix3{0.41239080f, 0.35758434f, 0.18048079f, 0.21263901f, 0.71516868f, 0.07219232f, 0.01933082f, 0.11919478f, 0.95053215f};
35
40 3.24096994f,
41 -1.53738318f,
42 -0.49861076f,
43 -0.96924364f,
44 1.87596750f,
45 0.04155506f,
46 0.05563008f,
47 -0.20397696f,
48 1.05697151f};
49
56[[nodiscard]] inline float sRGB_linear_to_gamma(float u) noexcept
57{
58 if (u <= 0.0031308) {
59 return 12.92f * u;
60 } else {
61 return 1.055f * std::pow(u, 0.416f) - 0.055f;
62 }
63}
64
71[[nodiscard]] inline float sRGB_gamma_to_linear(float u) noexcept
72{
73 if (u <= 0.04045) {
74 return u / 12.92f;
75 } else {
76 return std::pow((u + 0.055f) / 1.055f, 2.4f);
77 }
78}
79
80namespace detail {
81
82[[nodiscard]] inline auto sRGB_linear16_to_gamma8_table_generator() noexcept
83{
85
86 for (int i = 0; i != 65536; ++i) {
87 auto f = static_cast<float>(half(std::in_place, narrow_cast<uint16_t>(i)));
88 if (f != f) {
89 f = 0.0f;
90 }
91
92 r[i] = round_cast<uint8_t>(std::floor(std::clamp(sRGB_linear_to_gamma(f), 0.0f, 1.0f) * 255.0f));
93 }
94
95 return r;
96}
97
98[[nodiscard]] inline auto sRGB_gamma8_to_linear16_table_generator() noexcept
99{
101
102 for (int i = 0; i != 256; ++i) {
103 r[i] = static_cast<half>(sRGB_gamma_to_linear(i / 255.0f));
104 }
105
106 return r;
107}
108
109inline auto sRGB_linear16_to_gamma8_table = sRGB_linear16_to_gamma8_table_generator();
110inline auto sRGB_gamma8_to_linear16_table = sRGB_gamma8_to_linear16_table_generator();
111
112} // namespace detail
113
122[[nodiscard]] inline uint8_t sRGB_linear16_to_gamma8(half u) noexcept
123{
124 return detail::sRGB_linear16_to_gamma8_table[u.intrinsic()];
125}
126
135[[nodiscard]] inline half sRGB_gamma8_to_linear16(uint8_t u) noexcept
136{
137 return detail::sRGB_gamma8_to_linear16_table[u];
138}
139
149[[nodiscard]] inline color color_from_sRGB(float r, float g, float b, float a) noexcept
150{
152}
153
163[[nodiscard]] inline color color_from_sRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
164{
165 return color_from_sRGB(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
166}
167
168[[nodiscard]] inline color color_from_sRGB(std::string_view str)
169{
170 auto tmp = std::string{str};
171
172 if (tmp.starts_with("#")) {
173 tmp = tmp.substr(1);
174 }
175 if (ssize(tmp) != 6 && ssize(tmp) != 8) {
176 throw parse_error(std::format("Expecting 6 or 8 hex-digit sRGB color string, got {}.", str));
177 }
178 if (ssize(tmp) == 6) {
179 tmp += "ff";
180 }
181
182 auto packed = from_string<uint32_t>(tmp);
183
184 uint8_t const r = truncate<uint8_t>(packed >> 24);
185 uint8_t const g = truncate<uint8_t>(packed >> 16);
186 uint8_t const b = truncate<uint8_t>(packed >> 8);
187 uint8_t const a = truncate<uint8_t>(packed);
188 return color_from_sRGB(r, g, b, a);
189}
190
191}} // namespace hi::v1
192
193hi_warning_pop();
half sRGB_gamma8_to_linear16(uint8_t u) noexcept
sRGB gamma to linear float-16 transfer function.
Definition sRGB.hpp:135
constexpr matrix3 XYZ_to_sRGB
Matrix to convert XYZ to sRGB.
Definition sRGB.hpp:39
color color_from_sRGB(float r, float g, float b, float a) noexcept
Convert gama corrected sRGB color to the linear color.
Definition sRGB.hpp:149
uint8_t sRGB_linear16_to_gamma8(half u) noexcept
sRGB linear float-16 to gamma transfer function.
Definition sRGB.hpp:122
float sRGB_linear_to_gamma(float u) noexcept
sRGB linear to gamma transfer function.
Definition sRGB.hpp:56
constexpr matrix3 sRGB_to_XYZ
Matrix to convert sRGB to XYZ.
Definition sRGB.hpp:33
float sRGB_gamma_to_linear(float u) noexcept
sRGB gamma to linear transfer function.
Definition sRGB.hpp:71
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition half.hpp:27
This is a RGBA floating point color.
Definition color_intf.hpp:49
A 2D or 3D homogenius matrix for transforming homogenious vectors and points.
Definition matrix3.hpp:36
T floor(T... args)
T pow(T... args)