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/module.hpp"
13#include "color.hpp"
14#include "../macros.hpp"
15#include <cmath>
16#include <array>
17
18
19
20hi_warning_push();
21// C26426: Global initializer calls a non-constexpr function '...' (i.22).
22// std::pow() is not constexpr and needed to fill in the gamma conversion tables.
23hi_warning_ignore_msvc(26426);
24
25namespace hi { inline namespace v1 {
26
31 matrix3{0.41239080f, 0.35758434f, 0.18048079f, 0.21263901f, 0.71516868f, 0.07219232f, 0.01933082f, 0.11919478f, 0.95053215f};
32
37 3.24096994f,
38 -1.53738318f,
39 -0.49861076f,
40 -0.96924364f,
41 1.87596750f,
42 0.04155506f,
43 0.05563008f,
44 -0.20397696f,
45 1.05697151f};
46
53[[nodiscard]] inline float sRGB_linear_to_gamma(float u) noexcept
54{
55 if (u <= 0.0031308) {
56 return 12.92f * u;
57 } else {
58 return 1.055f * std::pow(u, 0.416f) - 0.055f;
59 }
60}
61
68[[nodiscard]] inline float sRGB_gamma_to_linear(float u) noexcept
69{
70 if (u <= 0.04045) {
71 return u / 12.92f;
72 } else {
73 return std::pow((u + 0.055f) / 1.055f, 2.4f);
74 }
75}
76
77namespace detail {
78
79[[nodiscard]] inline auto sRGB_linear16_to_gamma8_table_generator() noexcept
80{
82
83 for (int i = 0; i != 65536; ++i) {
85 std::floor(std::clamp(sRGB_linear_to_gamma(float16::from_uint16_t(narrow_cast<uint16_t>(i))), 0.0f, 1.0f) * 255.0f));
86 }
87
88 return r;
89}
90
91[[nodiscard]] inline auto sRGB_gamma8_to_linear16_table_generator() noexcept
92{
94
95 for (int i = 0; i != 256; ++i) {
96 r[i] = static_cast<float16>(sRGB_gamma_to_linear(i / 255.0f));
97 }
98
99 return r;
100}
101
102inline auto sRGB_linear16_to_gamma8_table = sRGB_linear16_to_gamma8_table_generator();
103inline auto sRGB_gamma8_to_linear16_table = sRGB_gamma8_to_linear16_table_generator();
104
105} // namespace detail
106
115[[nodiscard]] inline uint8_t sRGB_linear16_to_gamma8(float16 u) noexcept
116{
117 return detail::sRGB_linear16_to_gamma8_table[u.get()];
118}
119
128[[nodiscard]] inline float16 sRGB_gamma8_to_linear16(uint8_t u) noexcept
129{
130 return detail::sRGB_gamma8_to_linear16_table[u];
131}
132
142[[nodiscard]] inline color color_from_sRGB(float r, float g, float b, float a) noexcept
143{
145}
146
156[[nodiscard]] inline color color_from_sRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
157{
158 return color_from_sRGB(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
159}
160
161[[nodiscard]] inline color color_from_sRGB(std::string_view str)
162{
163 auto tmp = std::string{str};
164
165 if (tmp.starts_with("#")) {
166 tmp = tmp.substr(1);
167 }
168 if (ssize(tmp) != 6 && ssize(tmp) != 8) {
169 throw parse_error(std::format("Expecting 6 or 8 hex-digit sRGB color string, got {}.", str));
170 }
171 if (ssize(tmp) == 6) {
172 tmp += "ff";
173 }
174
176
177 uint8_t const r = truncate<uint8_t>(packed >> 24);
178 uint8_t const g = truncate<uint8_t>(packed >> 16);
179 uint8_t const b = truncate<uint8_t>(packed >> 8);
180 uint8_t const a = truncate<uint8_t>(packed);
181 return color_from_sRGB(r, g, b, a);
182}
183
184}} // namespace hi::v1
185
186hi_warning_pop();
Defined the color type.
constexpr matrix3 XYZ_to_sRGB
Matrix to convert XYZ to sRGB.
Definition sRGB.hpp:36
float16 sRGB_gamma8_to_linear16(uint8_t u) noexcept
sRGB gamma to linear float-16 transfer function.
Definition sRGB.hpp:128
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:142
uint8_t sRGB_linear16_to_gamma8(float16 u) noexcept
sRGB linear float-16 to gamma transfer function.
Definition sRGB.hpp:115
float sRGB_linear_to_gamma(float u) noexcept
sRGB linear to gamma transfer function.
Definition sRGB.hpp:53
constexpr matrix3 sRGB_to_XYZ
Matrix to convert sRGB to XYZ.
Definition sRGB.hpp:30
float sRGB_gamma_to_linear(float u) noexcept
sRGB gamma to linear transfer function.
Definition sRGB.hpp:68
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
This is a RGBA floating point color.
Definition color.hpp:45
A 2D or 3D homogenius matrix for transforming homogenious vectors and points.
Definition matrix3.hpp:30
T floor(T... args)
T pow(T... args)