💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.0 KiB

  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. #ifndef ANTKEEPER_COLOR_RGB_HPP
  20. #define ANTKEEPER_COLOR_RGB_HPP
  21. #include "color/cat.hpp"
  22. #include "math/matrix.hpp"
  23. #include "math/vector.hpp"
  24. namespace color {
  25. /// RGB color spaces.
  26. namespace rgb {
  27. /**
  28. * Constructs a matrix which transforms an RGB color into a CIE XYZ color.
  29. *
  30. * @param r CIE xy chromaticity coordinates of the red primary.
  31. * @param g CIE xy chromaticity coordinates of the green primary.
  32. * @param b CIE xy chromaticity coordinates of the blue primary.
  33. * @param w CIE xy chromaticity coordinates of the white point.
  34. *
  35. * @return Matrix which transforms an RGB color into a CIE XYZ color.
  36. *
  37. * @see https://www.ryanjuckett.com/rgb-color-space-conversion/
  38. * @see https://mina86.com/2019/srgb-xyz-matrix/
  39. */
  40. template <class T>
  41. constexpr math::matrix<T, 3, 3> to_xyz(const math::vector2<T>& r, const math::vector2<T>& g, const math::vector2<T>& b, const math::vector2<T>& w)
  42. {
  43. const math::matrix<T, 3, 3> m =
  44. {
  45. r[0], r[1], T{1} - (r[0] + r[1]),
  46. g[0], g[1], T{1} - (g[0] + g[1]),
  47. b[0], b[1], T{1} - (b[0] + b[1])
  48. };
  49. const math::vector3<T> scale = math::inverse(m) * math::vector3<T>{w[0] / w[1], T{1}, (T{1} - (w[0] + w[1])) / w[1]};
  50. return math::matrix<T, 3, 3>
  51. {
  52. m[0][0] * scale[0], m[0][1] * scale[0], m[0][2] * scale[0],
  53. m[1][0] * scale[1], m[1][1] * scale[1], m[1][2] * scale[1],
  54. m[2][0] * scale[2], m[2][1] * scale[2], m[2][2] * scale[2],
  55. };
  56. }
  57. /**
  58. * RGB color space.
  59. */
  60. template <class T>
  61. struct color_space
  62. {
  63. /// Transfer function function pointer type.
  64. typedef math::vector3<T> (*transfer_function_type)(const math::vector3<T>&);
  65. /// CIE xy chromaticity coordinates of the red primary.
  66. const math::vector2<T> r;
  67. /// CIE xy chromaticity coordinates of the green primary.
  68. const math::vector2<T> g;
  69. /// CIE xy chromaticity coordinates of the blue primary.
  70. const math::vector2<T> b;
  71. /// CIE xy chromaticity coordinates of the white point.
  72. const math::vector2<T> w;
  73. /// Function pointer to the electro-optical transfer function.
  74. const transfer_function_type eotf;
  75. /// Function pointer to the inverse electro-optical transfer function.
  76. const transfer_function_type inverse_eotf;
  77. /// Matrix which transforms an RGB color to a CIE XYZ color.
  78. const math::matrix3x3<T> to_xyz;
  79. /// Matrix which transforms a CIE XYZ color to an RGB color.
  80. const math::matrix3x3<T> from_xyz;
  81. /// Vector which gives the luminance of an RGB color via dot product.
  82. const math::vector3<T> to_y;
  83. /**
  84. * Constructs an RGB color space.
  85. *
  86. * @param r CIE xy chromaticity coordinates of the red primary.
  87. * @param g CIE xy chromaticity coordinates of the green primary.
  88. * @param b CIE xy chromaticity coordinates of the blue primary.
  89. * @param w CIE xy chromaticity coordinates of the white point.
  90. */
  91. constexpr color_space(const math::vector2<T>& r, const math::vector2<T>& g, const math::vector2<T>& b, const math::vector2<T>& w, transfer_function_type eotf, transfer_function_type inverse_eotf);
  92. /**
  93. * Measures the luminance of a linear RGB color.
  94. *
  95. * @param x Linear RGB color.
  96. * @return return Luminance of @p x.
  97. */
  98. constexpr T luminance(const math::vector3<T>& x) const;
  99. };
  100. template <class T>
  101. constexpr color_space<T>::color_space(const math::vector2<T>& r, const math::vector2<T>& g, const math::vector2<T>& b, const math::vector2<T>& w, transfer_function_type eotf, transfer_function_type inverse_eotf):
  102. r(r),
  103. g(g),
  104. b(b),
  105. w(w),
  106. eotf(eotf),
  107. inverse_eotf(inverse_eotf),
  108. to_xyz(color::rgb::to_xyz<T>(r, g, b, w)),
  109. from_xyz(math::inverse(to_xyz)),
  110. to_y{to_xyz[0][1], to_xyz[1][1], to_xyz[2][1]}
  111. {}
  112. template <class T>
  113. constexpr T color_space<T>::luminance(const math::vector3<T>& x) const
  114. {
  115. return math::dot(x, to_y);
  116. }
  117. /**
  118. * Constructs a matrix which transforms a color from one RGB color space to another RGB color space.
  119. *
  120. * @param s0 Source color space.
  121. * @param s1 Destination color space.
  122. * @param cone_response Chromatic adaptation transform cone response matrix.
  123. *
  124. * @return Color space transformation matrix.
  125. */
  126. template <class T>
  127. constexpr math::matrix3x3<T> to_rgb(const color_space<T>& s0, const color_space<T>& s1, const math::matrix3x3<T>& cone_response = color::cat::bradford<T>)
  128. {
  129. return s1.from_xyz * color::cat::matrix(s0.w, s1.w, cone_response) * s0.to_xyz;
  130. }
  131. } // namespace rgb
  132. } // namespace color
  133. #endif // ANTKEEPER_COLOR_RGB_HPP