Antkeeper  0.0.1
rgb.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_COLOR_RGB_HPP
21 #define ANTKEEPER_COLOR_RGB_HPP
22 
23 #include <engine/color/cat.hpp>
24 #include <engine/math/matrix.hpp>
25 #include <engine/math/vector.hpp>
26 
27 namespace color {
28 
31 
45 template <class T>
46 [[nodiscard]] constexpr math::mat3<T> rgb_to_xyz(const math::vec2<T>& r, const math::vec2<T>& g, const math::vec2<T>& b, const math::vec2<T>& w)
47 {
48  const math::mat3<T> m =
49  {
50  r[0], r[1], T{1} - r[0] - r[1],
51  g[0], g[1], T{1} - g[0] - g[1],
52  b[0], b[1], T{1} - b[0] - b[1]
53  };
54 
55  const math::vec3<T> scale = math::inverse(m) * math::vec3<T>{w[0] / w[1], T{1}, (T{1} - w[0] - w[1]) / w[1]};
56 
57  return math::mat3<T>
58  {
59  m[0][0] * scale[0], m[0][1] * scale[0], m[0][2] * scale[0],
60  m[1][0] * scale[1], m[1][1] * scale[1], m[1][2] * scale[1],
61  m[2][0] * scale[2], m[2][1] * scale[2], m[2][2] * scale[2],
62  };
63 }
64 
68 template <class T>
70 {
73 
76 
79 
82 
85 
88 
91 
94 
97 
100 
110  r(r),
111  g(g),
112  b(b),
113  w(w),
114  eotf(eotf),
115  oetf(oetf),
116  to_xyz(rgb_to_xyz<T>(r, g, b, w)),
118  to_y{to_xyz[0][1], to_xyz[1][1], to_xyz[2][1]}
119  {}
120 
127  [[nodiscard]] inline constexpr T luminance(const math::vec3<T>& x) const noexcept
128  {
129  return math::dot(x, to_y);
130  }
131 };
132 
142 template <class T>
143 [[nodiscard]] constexpr math::mat3<T> rgb_to_rgb(const rgb_color_space<T>& s0, const rgb_color_space<T>& s1, const math::mat3<T>& cone_response = bradford_cone_response<T>)
144 {
145  return s1.from_xyz * cat_matrix(s0.w, s1.w, cone_response) * s0.to_xyz;
146 }
147 
149 
150 } // namespace color
151 
152 #endif // ANTKEEPER_COLOR_RGB_HPP
Color science.
Definition: aces.hpp:27
constexpr math::mat3< T > rgb_to_rgb(const rgb_color_space< T > &s0, const rgb_color_space< T > &s1, const math::mat3< T > &cone_response=bradford_cone_response< T >)
Constructs a matrix which transforms a color from one RGB color space to another RGB color space.
Definition: rgb.hpp:143
constexpr math::mat3< T > cat_matrix(const math::vec2< T > &w0, const math::vec2< T > &w1, const math::mat3< T > &cone_response=bradford_cone_response< T >) noexcept
Constructs a chromatic adaptation transform (CAT) matrix.
Definition: cat.hpp:79
constexpr math::mat3< T > rgb_to_xyz(const math::vec2< T > &r, const math::vec2< T > &g, const math::vec2< T > &b, const math::vec2< T > &w)
Constructs a matrix which transforms an RGB color into a CIE XYZ color.
Definition: rgb.hpp:46
Mathematical functions and data types.
Definition: angles.hpp:26
constexpr mat4< T > scale(const vec3< T > &v)
Constructs a scale matrix.
constexpr T dot(const quaternion< T > &a, const quaternion< T > &b) noexcept
Calculates the dot product of two quaternions.
Definition: quaternion.hpp:572
constexpr matrix< T, N, N > inverse(const matrix< T, N, N > &m) noexcept
Calculates the inverse of a square matrix.
RGB color space.
Definition: rgb.hpp:70
const transfer_function_type oetf
Function pointer to the opto-electrical transfer function.
Definition: rgb.hpp:90
const math::vec2< T > w
CIE xy chromaticity coordinates of the white point.
Definition: rgb.hpp:84
const math::vec2< T > g
CIE xy chromaticity coordinates of the green primary.
Definition: rgb.hpp:78
const math::vec3< T > to_y
Vector which gives the luminance of an RGB color via dot product.
Definition: rgb.hpp:99
const math::vec2< T > r
CIE xy chromaticity coordinates of the red primary.
Definition: rgb.hpp:75
constexpr T luminance(const math::vec3< T > &x) const noexcept
Measures the luminance of a linear RGB color.
Definition: rgb.hpp:127
math::vec3< T >(*)(const math::vec3< T > &) transfer_function_type
Transfer function function pointer type.
Definition: rgb.hpp:72
const transfer_function_type eotf
Function pointer to the electro-optical transfer function.
Definition: rgb.hpp:87
const math::vec2< T > b
CIE xy chromaticity coordinates of the blue primary.
Definition: rgb.hpp:81
constexpr rgb_color_space(const math::vec2< T > &r, const math::vec2< T > &g, const math::vec2< T > &b, const math::vec2< T > &w, transfer_function_type eotf, transfer_function_type oetf)
Constructs an RGB color space.
Definition: rgb.hpp:109
const math::mat3< T > from_xyz
Matrix which transforms a CIE XYZ color to an RGB color.
Definition: rgb.hpp:96
const math::mat3< T > to_xyz
Matrix which transforms an RGB color to a CIE XYZ color.
Definition: rgb.hpp:93
n by m column-major matrix.
Definition: math/matrix.hpp:44
n-dimensional vector.
Definition: vector.hpp:44