aest 0.1.0
Collection of C++ utility libraries
Loading...
Searching...
No Matches
Gamut.cppm
Go to the documentation of this file.
1
9module;
10#include <array>
11#include <concepts>
12#include <cstdint>
13#include <string_view>
14#include <type_traits>
15export module aest.colors:gamut;
16
17import aest.blas;
18import :color;
19import :transfer;
20
21namespace aest::colors
22{
24export using Chromaticity = std::array<double, 2>;
25
27export inline constexpr Chromaticity D65 = {0.3127, 0.3290};
28
33export inline constexpr Chromaticity D50 = {0.3457, 0.3585};
34
46export template <typename T>
47concept RGBGamut = requires {
48 { T::NAME } -> std::convertible_to<std::string_view>;
49 { T::RED } -> std::convertible_to<Chromaticity>;
50 { T::GREEN } -> std::convertible_to<Chromaticity>;
51 { T::BLUE } -> std::convertible_to<Chromaticity>;
52 { T::WHITE } -> std::convertible_to<Chromaticity>;
53};
54
56export namespace gamut
57{
62struct SRGB
63{
64 static constexpr std::string_view NAME = "SRGB";
65 static constexpr Chromaticity RED = {0.64, 0.33};
66 static constexpr Chromaticity GREEN = {0.30, 0.60};
67 static constexpr Chromaticity BLUE = {0.15, 0.06};
68 static constexpr Chromaticity WHITE = D65;
69};
70
76{
77 static constexpr std::string_view NAME = "DisplayP3";
78 static constexpr Chromaticity RED = {0.680, 0.320};
79 static constexpr Chromaticity GREEN = {0.265, 0.690};
80 static constexpr Chromaticity BLUE = {0.150, 0.060};
81 static constexpr Chromaticity WHITE = D65;
82};
83
88struct Rec2020
89{
90 static constexpr std::string_view NAME = "Rec2020";
91 static constexpr Chromaticity RED = {0.708, 0.292};
92 static constexpr Chromaticity GREEN = {0.170, 0.797};
93 static constexpr Chromaticity BLUE = {0.131, 0.046};
94 static constexpr Chromaticity WHITE = D65;
95};
96
102struct AP1
103{
104 static constexpr std::string_view NAME = "AP1";
105 static constexpr Chromaticity RED = {0.713, 0.293};
106 static constexpr Chromaticity GREEN = {0.165, 0.830};
107 static constexpr Chromaticity BLUE = {0.128, 0.044};
108 static constexpr Chromaticity WHITE = {0.32168, 0.33767};
109};
110} // namespace gamut
111
114
117
119constexpr Matrix3 BRADFORD = {{
120 {0.8951, 0.2664, -0.1614},
121 {-0.7502, 1.7135, 0.0367},
122 {0.0389, -0.0685, 1.0296},
123}};
124
130constexpr Vector3 xyz_of(const Chromaticity& chromaticity)
131{
132 const double x = chromaticity[0];
133 const double y = chromaticity[1];
134 return {x / y, 1, (1 - x - y) / y};
135}
136
149constexpr Matrix3 rgb_to_xyz(const Chromaticity& red,
150 const Chromaticity& green,
151 const Chromaticity& blue,
152 const Chromaticity& white)
153{
154 // The primaries as columns, each weighted so that together they add up to white
155 const Matrix3 primaries = blas::transpose(Matrix3{xyz_of(red), xyz_of(green), xyz_of(blue)});
156 const Vector3 weights = blas::multiply(blas::inverse(primaries), xyz_of(white));
157 return blas::multiply(primaries, blas::diagonal(weights));
158}
159
166constexpr Matrix3 bradford_to_d65(const Chromaticity& white)
167{
168 // Scale each cone response by the ratio of the two whites
169 const Vector3 source = blas::multiply(BRADFORD, xyz_of(white));
170 const Vector3 target = blas::multiply(BRADFORD, xyz_of(D65));
171 const Vector3 ratios = {target[0] / source[0], target[1] / source[1], target[2] / source[2]};
174}
175
181template <RGBGamut Gamut>
183{
184 const Matrix3 native = rgb_to_xyz(Gamut::RED, Gamut::GREEN, Gamut::BLUE, Gamut::WHITE);
185 const bool isD65 = Gamut::WHITE[0] == D65[0] && Gamut::WHITE[1] == D65[1];
186 return isD65 ? native : blas::multiply(bradford_to_d65(Gamut::WHITE), native);
187}
188
193template <RGBGamut Gamut>
195
200template <RGBGamut Gamut>
202
209template <RGBGamut Gamut, TransferFunction Transfer>
210inline constexpr auto RGB_NAME_CHARS =
211 join_names<Transfer::PREFIX.size() + Gamut::NAME.size()>({Transfer::PREFIX, Gamut::NAME});
212
223export template <RGBGamut Gamut, TransferFunction Transfer>
225{
227 static constexpr std::string_view VALUE{RGB_NAME_CHARS<Gamut, Transfer>.data(),
229};
230
232template <>
234{
236 static constexpr std::string_view VALUE = "ACEScg";
237};
238
240template <>
242{
244 static constexpr std::string_view VALUE = "Rec709";
245};
246
248template <>
250{
252 static constexpr std::string_view VALUE = "Rec2020";
253};
254
256template <>
258{
260 static constexpr std::string_view VALUE = "Rec2100PQ";
261};
262
264template <>
266{
268 static constexpr std::string_view VALUE = "Rec2100HLG";
269};
270
273{};
274
291export template <RGBGamut Gamut, TransferFunction Transfer>
292class BasicRGB final : public ColorBase<BasicRGB<Gamut, Transfer>>,
293 public std::conditional_t<std::same_as<Transfer, tf::Linear>,
294 ColorArithmetic<BasicRGB<Gamut, Transfer>>,
295 NoArithmetic>
296{
297public:
299 static constexpr std::string_view TYPE = RGBName<Gamut, Transfer>::VALUE;
300
302 constexpr BasicRGB()
303 : ColorBase<BasicRGB>(1.0f, 0.0f, 0.0f, 0.0f)
304 {}
305
314 constexpr BasicRGB(const float r, const float g, const float b, const float a = 1.0f)
315 : ColorBase<BasicRGB>(a, Transfer::clamp(r), Transfer::clamp(g), Transfer::clamp(b))
316 {}
317
320
324 [[nodiscard]] constexpr float red() const noexcept { return this->_ch1; }
325
330 [[nodiscard]] constexpr float green() const noexcept { return this->_ch2; }
331
336 [[nodiscard]] constexpr float blue() const noexcept { return this->_ch3; }
337
339};
340
345template <typename T>
347{};
348
354template <RGBGamut Gamut, TransferFunction Transfer>
355struct RGBParts<BasicRGB<Gamut, Transfer>>
356{
357 using GamutTag = Gamut;
358 using TransferTag = Transfer;
359};
360} // namespace aest::colors
361
362export namespace aest::colors
363{
371
377
380
383
386
389
392
395
398
401
406template <typename T>
407concept RGBSpace = requires { typename RGBParts<T>::TransferTag; };
408
413template <typename T>
414concept LinearRGBSpace = RGBSpace<T> && std::same_as<typename RGBParts<T>::TransferTag, tf::Linear>;
415
421template <typename T>
424} // namespace aest::colors
constexpr float clamp(const float value)
Clamp a floating-point value to the 0-1 range.
Definition Color.cppm:29
constexpr std::array< char, Size > join_names(const std::initializer_list< std::string_view > parts)
Concatenate names into a character array, for a color space name built at compile time.
Definition Color.cppm:100
constexpr Chromaticity D50
Chromaticity of the D50 white point, as CSS Color 4 and the ICC profile connection space define it: t...
Definition Gamut.cppm:33
constexpr Matrix3 gamut_to_xyz()
Derives the matrix from linear RGB of a gamut to XYZ relative to D65.
Definition Gamut.cppm:182
constexpr auto RGB_NAME_CHARS
The characters of the default BasicRGB name: the transfer prefix, then the gamut name.
Definition Gamut.cppm:210
BasicRGB< gamut::Rec2020, tf::PQ > Rec2100PQ
Rec.2100 PQ: Rec. 2020 primaries with the PQ signal of HDR10, linear 1.0 at 203 cd/m²
Definition Gamut.cppm:397
constexpr Chromaticity D65
Chromaticity of the D65 white point, as sRGB defines it: the white of XYZ.
Definition Gamut.cppm:27
BasicRGB< gamut::SRGB, tf::Rec709 > Rec709
Rec. 709: the sRGB primaries with the Rec. 709 OETF, the encoding of HD video.
Definition Gamut.cppm:385
BasicRGB< gamut::SRGB, tf::SRGB > SRGB
sRGB with gamma-encoded channels: the encoding of hex codes, 8-bit channels and CSS colors.
Definition Gamut.cppm:370
std::array< double, 2 > Chromaticity
CIE 1931 xy chromaticity coordinates of a primary or white point.
Definition Gamut.cppm:24
constexpr Matrix3 bradford_to_d65(const Chromaticity &white)
Derives the Bradford matrix that adapts XYZ colors from a white point to D65.
Definition Gamut.cppm:166
constexpr Matrix3 BRADFORD
The Bradford cone response matrix, used to adapt a white point.
Definition Gamut.cppm:119
constexpr Matrix3 rgb_to_xyz(const Chromaticity &red, const Chromaticity &green, const Chromaticity &blue, const Chromaticity &white)
Derives the matrix from linear RGB to XYZ for a set of primaries.
Definition Gamut.cppm:149
constexpr Vector3 xyz_of(const Chromaticity &chromaticity)
The XYZ of a chromaticity at luminance Y = 1.
Definition Gamut.cppm:130
blas::Vector< double, 3 > Vector3
A column of three values, such as XYZ or linear RGB channels.
Definition Gamut.cppm:113
constexpr Matrix3 XYZ_TO_RGB
Matrix from XYZ, D65, to linear RGB of Gamut.
Definition Gamut.cppm:201
BasicRGB< gamut::DisplayP3, tf::Linear > LinDisplayP3
Linear Display P3.
Definition Gamut.cppm:382
blas::Matrix< double, 3, 3 > Matrix3
A 3x3 matrix, stored as three rows.
Definition Gamut.cppm:116
BasicRGB< gamut::AP1, tf::Linear > ACEScg
ACEScg: linear AP1, the working space of ACES rendering and compositing.
Definition Gamut.cppm:394
BasicRGB< gamut::Rec2020, tf::Rec709 > Rec2020
Rec. 2020: the Rec. 2020 primaries with the Rec. 709 OETF, the encoding of SDR UHD video.
Definition Gamut.cppm:388
BasicRGB< gamut::Rec2020, tf::HLG > Rec2100HLG
Rec.2100 HLG: Rec. 2020 primaries with the HLG signal, linear 1.0 at 75%.
Definition Gamut.cppm:400
constexpr Matrix3 RGB_TO_XYZ
Matrix from linear RGB of Gamut to XYZ, D65.
Definition Gamut.cppm:194
BasicRGB< gamut::DisplayP3, tf::SRGB > DisplayP3
Display P3: the P3 primaries with the sRGB transfer function, as on Apple displays and in CSS.
Definition Gamut.cppm:379
BasicRGB< gamut::SRGB, tf::Linear > LinSRGB
Linear sRGB: the sRGB primaries without the gamma encoding, so channels are proportional to light and...
Definition Gamut.cppm:376
BasicRGB< gamut::Rec2020, tf::Linear > LinRec2020
Linear Rec. 2020, the gamut of UHD and HDR video.
Definition Gamut.cppm:391
constexpr Vector< T, Rows > multiply(const Matrix< T, Rows, Columns > &matrix, const Vector< T, Columns > &vector)
Multiplies a matrix by a column vector.
Definition Matrix.cppm:69
constexpr Matrix< T, Columns, Rows > transpose(const Matrix< T, Rows, Columns > &matrix)
Swaps the rows and columns of a matrix.
Definition Matrix.cppm:110
constexpr Matrix< T, 3, 3 > inverse(const Matrix< T, 3, 3 > &matrix)
Inverts a 3x3 matrix through its adjugate.
Definition Matrix.cppm:158
std::array< Vector< T, Columns >, Rows > Matrix
A matrix of fixed size, stored as an array of rows.
Definition Matrix.cppm:57
constexpr Matrix< T, Size, Size > diagonal(const Vector< T, Size > &vector)
Builds a square matrix with a vector on its diagonal and 0 elsewhere.
Definition Matrix.cppm:128
std::array< T, Size > Vector
A column vector of fixed size.
Definition Matrix.cppm:48
A color in an RGB color space: the primaries and white point of Gamut, with channels encoded by Trans...
Definition Gamut.cppm:296
constexpr float green() const noexcept
Get the green channel value.
Definition Gamut.cppm:330
constexpr float red() const noexcept
Get the red channel value.
Definition Gamut.cppm:324
constexpr BasicRGB()
Default constructor initializes the color to opaque black (0, 0, 0, 1).
Definition Gamut.cppm:302
constexpr BasicRGB(const float r, const float g, const float b, const float a=1.0f)
Construct a color from floating-point RGBA values.
Definition Gamut.cppm:314
static constexpr std::string_view TYPE
Definition Gamut.cppm:299
constexpr float blue() const noexcept
Get the blue channel value.
Definition Gamut.cppm:336
A BasicRGB space with an SDR encoding: channels in the 0-1 range, relative to the display's white....
Definition Gamut.cppm:422
A transfer function for HDR signals, such as PQ or HLG: a tag with a static IS_HDR set to true....
A BasicRGB space with linear channels, tf::Linear.
Definition Gamut.cppm:414
A gamut tag, the first parameter of BasicRGB.
Definition Gamut.cppm:47
Any BasicRGB color space.
Definition Gamut.cppm:407
Gamut tags, the first parameter of BasicRGB.
Definition Gamut.cppm:57
constexpr ColorBase(const float alpha, const float channel1, const float channel2, const float channel3)
Definition Color.cppm:158
Empty base standing in for ColorArithmetic in encoded RGB spaces.
Definition Gamut.cppm:273
static constexpr std::string_view VALUE
The name.
Definition Gamut.cppm:236
static constexpr std::string_view VALUE
The name.
Definition Gamut.cppm:268
static constexpr std::string_view VALUE
The name.
Definition Gamut.cppm:260
static constexpr std::string_view VALUE
The name.
Definition Gamut.cppm:252
static constexpr std::string_view VALUE
The name.
Definition Gamut.cppm:244
Name of the BasicRGB space of a gamut and a transfer function, printed by std::format.
Definition Gamut.cppm:225
static constexpr std::string_view VALUE
The name.
Definition Gamut.cppm:227
Transfer TransferTag
The transfer function tag.
Definition Gamut.cppm:358
The gamut and transfer function of a BasicRGB space; empty for other types.
Definition Gamut.cppm:347
The ACES AP1 primaries with the ACES white point (about D60), from SMPTE ST 2065-1,...
Definition Gamut.cppm:103
static constexpr std::string_view NAME
Color space name.
Definition Gamut.cppm:104
static constexpr Chromaticity BLUE
Blue primary.
Definition Gamut.cppm:107
static constexpr Chromaticity WHITE
White point.
Definition Gamut.cppm:108
static constexpr Chromaticity GREEN
Green primary.
Definition Gamut.cppm:106
static constexpr Chromaticity RED
Red primary.
Definition Gamut.cppm:105
The DCI-P3 primaries with D65 white, from SMPTE EG 432-1.
Definition Gamut.cppm:76
static constexpr std::string_view NAME
Color space name.
Definition Gamut.cppm:77
static constexpr Chromaticity WHITE
White point.
Definition Gamut.cppm:81
static constexpr Chromaticity BLUE
Blue primary.
Definition Gamut.cppm:80
static constexpr Chromaticity GREEN
Green primary.
Definition Gamut.cppm:79
static constexpr Chromaticity RED
Red primary.
Definition Gamut.cppm:78
The Rec. 2020 / Rec. 2100 primaries, D65 white, from ITU-R BT.2020.
Definition Gamut.cppm:89
static constexpr Chromaticity GREEN
Green primary.
Definition Gamut.cppm:92
static constexpr Chromaticity WHITE
White point.
Definition Gamut.cppm:94
static constexpr Chromaticity BLUE
Blue primary.
Definition Gamut.cppm:93
static constexpr Chromaticity RED
Red primary.
Definition Gamut.cppm:91
static constexpr std::string_view NAME
Color space name.
Definition Gamut.cppm:90
The sRGB and Rec. 709 primaries, D65 white, from IEC 61966-2-1.
Definition Gamut.cppm:63
static constexpr Chromaticity WHITE
White point.
Definition Gamut.cppm:68
static constexpr std::string_view NAME
Color space name.
Definition Gamut.cppm:64
static constexpr Chromaticity GREEN
Green primary.
Definition Gamut.cppm:66
static constexpr Chromaticity RED
Red primary.
Definition Gamut.cppm:65
static constexpr Chromaticity BLUE
Blue primary.
Definition Gamut.cppm:67
The Hybrid Log-Gamma OETF of ITU-R BT.2100 and ARIB STD-B67, the relative HDR transfer function of br...
No transfer function: channel values are proportional to light.
The Perceptual Quantizer of SMPTE ST 2084 and ITU-R BT.2100, the absolute HDR transfer function of HD...
The OETF of ITU-R BT.709, which BT.2020 shares for SDR: a linear segment near black,...