HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
sRGB.hpp
1// Copyright Take Vos 2020.
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
5#pragma once
6
7#include "../float16.hpp"
8#include "../geometry/matrix.hpp"
9#include "../codec/base_n.hpp"
10#include "color.hpp"
11#include <cmath>
12#include <array>
13
14namespace tt {
15
16constexpr matrix3 sRGB_to_XYZ =
17 matrix3{
18 0.41239080f, 0.35758434f, 0.18048079f,
19 0.21263901f, 0.71516868f, 0.07219232f,
20 0.01933082f, 0.11919478f, 0.95053215f
21};
22
23constexpr matrix3 XYZ_to_sRGB =
24 matrix3{
25 3.24096994f, -1.53738318f, -0.49861076f,
26 -0.96924364f, 1.87596750f, 0.04155506f,
27 0.05563008f, -0.20397696f, 1.05697151f
28};
29
30[[nodiscard]] inline float sRGB_linear_to_gamma(float u) noexcept
31{
32 if (u <= 0.0031308) {
33 return 12.92f * u;
34 } else {
35 return 1.055f * std::pow(u, 0.416f) - 0.055f;
36 }
37}
38
39[[nodiscard]] inline float sRGB_gamma_to_linear(float u) noexcept
40{
41 if (u <= 0.04045) {
42 return u / 12.92f;
43 } else {
44 return std::pow((u + 0.055f) / 1.055f, 2.4f);
45 }
46}
47
48[[nodiscard]] inline auto sRGB_linear16_to_gamma8_table_generator() noexcept
49{
51
52 for (int i = 0; i != 65536; ++i) {
53 r[i] = static_cast<uint8_t>(
54 std::clamp(sRGB_linear_to_gamma(float16::from_uint16_t(narrow_cast<uint16_t>(i))), 0.0f, 1.0f) * 255.0f
55 );
56 }
57
58 return r;
59}
60
61inline auto sRGB_linear16_to_gamma8_table = sRGB_linear16_to_gamma8_table_generator();
62
63[[nodiscard]] inline uint8_t sRGB_linear16_to_gamma8(float16 u) noexcept
64{
65 return sRGB_linear16_to_gamma8_table[u.get()];
66}
67
68
69
70[[nodiscard]] inline auto sRGB_gamma8_to_linear16_table_generator() noexcept
71{
73
74 for (int i = 0; i != 256; ++i) {
75 r[i] = static_cast<float16>(sRGB_gamma_to_linear(i / 255.0f));
76 }
77
78 return r;
79}
80
81inline auto sRGB_gamma8_to_linear16_table = sRGB_gamma8_to_linear16_table_generator();
82
83[[nodiscard]] inline float16 sRGB_gamma8_to_linear16(uint8_t u) noexcept
84{
85 return sRGB_gamma8_to_linear16_table[u];
86}
87
88[[nodiscard]] inline color color_from_sRGB(float r, float g, float b, float a) noexcept
89{
90 return color{
91 sRGB_gamma_to_linear(narrow_cast<float>(r)),
92 sRGB_gamma_to_linear(narrow_cast<float>(g)),
93 sRGB_gamma_to_linear(narrow_cast<float>(b)),
94 a};
95}
96
97[[nodiscard]] inline color color_from_sRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
98{
99 return color_from_sRGB(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
100}
101
102[[nodiscard]] inline color color_from_sRGB(std::string_view str)
103{
104 auto tmp = std::string{str};
105
106 if (tmp.starts_with("#")) {
107 tmp = tmp.substr(1);
108 }
109 if (std::ssize(tmp) != 6 && std::ssize(tmp) != 8) {
110 throw parse_error("Expecting 6 or 8 hex-digit sRGB color string, got {}.", str);
111 }
112 if (std::ssize(tmp) == 6) {
113 tmp += "ff";
114 }
115
116 uint8_t r = (base16::int_from_char<uint8_t>(tmp[0]) << 4) | base16::int_from_char<uint8_t>(tmp[1]);
117 uint8_t g = (base16::int_from_char<uint8_t>(tmp[2]) << 4) | base16::int_from_char<uint8_t>(tmp[3]);
118 uint8_t b = (base16::int_from_char<uint8_t>(tmp[4]) << 4) | base16::int_from_char<uint8_t>(tmp[5]);
119 uint8_t a = (base16::int_from_char<uint8_t>(tmp[6]) << 4) | base16::int_from_char<uint8_t>(tmp[7]);
120 return color_from_sRGB(r, g, b, a);
121}
122
123}
124
T pow(T... args)