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 hi::inline v1 {
15
16constexpr matrix3 sRGB_to_XYZ =
17 matrix3{0.41239080f, 0.35758434f, 0.18048079f, 0.21263901f, 0.71516868f, 0.07219232f, 0.01933082f, 0.11919478f, 0.95053215f};
18
19constexpr matrix3 XYZ_to_sRGB = matrix3{
20 3.24096994f,
21 -1.53738318f,
22 -0.49861076f,
23 -0.96924364f,
24 1.87596750f,
25 0.04155506f,
26 0.05563008f,
27 -0.20397696f,
28 1.05697151f};
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 return r;
58}
59
60inline auto sRGB_linear16_to_gamma8_table = sRGB_linear16_to_gamma8_table_generator();
61
62[[nodiscard]] inline uint8_t sRGB_linear16_to_gamma8(float16 u) noexcept
63{
64 return sRGB_linear16_to_gamma8_table[u.get()];
65}
66
67[[nodiscard]] inline auto sRGB_gamma8_to_linear16_table_generator() noexcept
68{
70
71 for (int i = 0; i != 256; ++i) {
72 r[i] = static_cast<float16>(sRGB_gamma_to_linear(i / 255.0f));
73 }
74
75 return r;
76}
77
78inline auto sRGB_gamma8_to_linear16_table = sRGB_gamma8_to_linear16_table_generator();
79
80[[nodiscard]] inline float16 sRGB_gamma8_to_linear16(uint8_t u) noexcept
81{
82 return sRGB_gamma8_to_linear16_table[u];
83}
84
85[[nodiscard]] inline color color_from_sRGB(float r, float g, float b, float a) noexcept
86{
87 return color{
88 sRGB_gamma_to_linear(narrow_cast<float>(r)),
89 sRGB_gamma_to_linear(narrow_cast<float>(g)),
90 sRGB_gamma_to_linear(narrow_cast<float>(b)),
91 a};
92}
93
94[[nodiscard]] inline color color_from_sRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept
95{
96 return color_from_sRGB(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
97}
98
99[[nodiscard]] inline color color_from_sRGB(std::string_view str)
100{
101 auto tmp = std::string{str};
102
103 if (tmp.starts_with("#")) {
104 tmp = tmp.substr(1);
105 }
106 if (ssize(tmp) != 6 && ssize(tmp) != 8) {
107 throw parse_error(std::format("Expecting 6 or 8 hex-digit sRGB color string, got {}.", str));
108 }
109 if (ssize(tmp) == 6) {
110 tmp += "ff";
111 }
112
113 uint8_t r = (base16::int_from_char<uint8_t>(tmp[0]) << 4) | base16::int_from_char<uint8_t>(tmp[1]);
114 uint8_t g = (base16::int_from_char<uint8_t>(tmp[2]) << 4) | base16::int_from_char<uint8_t>(tmp[3]);
115 uint8_t b = (base16::int_from_char<uint8_t>(tmp[4]) << 4) | base16::int_from_char<uint8_t>(tmp[5]);
116 uint8_t a = (base16::int_from_char<uint8_t>(tmp[6]) << 4) | base16::int_from_char<uint8_t>(tmp[7]);
117 return color_from_sRGB(r, g, b, a);
118}
119
120} // namespace hi::inline v1
T pow(T... args)