💿🐜 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.

103 lines
3.4 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_CAT_HPP
  20. #define ANTKEEPER_COLOR_CAT_HPP
  21. #include <engine/math/matrix.hpp>
  22. #include <engine/math/vector.hpp>
  23. namespace color {
  24. /// @name Chromatic adaption transforms (CAT)
  25. /// @{
  26. /**
  27. * Bradford cone response matrix.
  28. *
  29. * @see Specification ICC.1:2010 (Profile version 4.3.0.0). Image technology colour management Architecture, profile format, and data structure, Annex E.3, pp. 102.
  30. * @see http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html
  31. */
  32. template <class T>
  33. constexpr math::mat3<T> bradford_cone_response =
  34. {
  35. T{ 0.8951}, T{-0.7502}, T{ 0.0389},
  36. T{ 0.2664}, T{ 1.7135}, T{-0.0685},
  37. T{-0.1614}, T{ 0.0367}, T{ 1.0296}
  38. };
  39. /**
  40. * von Kries cone response matrix.
  41. *
  42. * @see http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html
  43. */
  44. template <class T>
  45. constexpr math::mat3<T> von_kries_cone_response =
  46. {
  47. T{ 0.40024}, T{-0.22630}, T{0.00000},
  48. T{ 0.70760}, T{ 1.16532}, T{0.00000},
  49. T{-0.08081}, T{ 0.04570}, T{0.91822}
  50. };
  51. /**
  52. * XYZ scaling cone response matrix.
  53. *
  54. * @see http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html
  55. */
  56. template <class T>
  57. constexpr math::mat3<T> xyz_scaling_cone_response = math::mat3<T>::identity();
  58. /**
  59. * Constructs a chromatic adaptation transform (CAT) matrix.
  60. *
  61. * @param w0 CIE xy chromaticity coordinates of the source illuminant.
  62. * @param w1 CIE xy chromaticity coordinates of the destination illuminant.
  63. * @param cone_response Cone response matrix.
  64. *
  65. * @return CAT matrix.
  66. *
  67. * @see Specification ICC.1:2010 (Profile version 4.3.0.0). Image technology colour management Architecture, profile format, and data structure, Annex E.3, pp. 102.
  68. * @see http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html
  69. */
  70. template <class T>
  71. [[nodiscard]] 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
  72. {
  73. // Convert CIE xy chromaticity coordinates to CIE XYZ colors
  74. const math::vec3<T> w0_xyz = {w0[0] / w0[1], T{1}, (T{1} - w0[0] - w0[1]) / w0[1]};
  75. const math::vec3<T> w1_xyz = {w1[0] / w1[1], T{1}, (T{1} - w1[0] - w1[1]) / w1[1]};
  76. // Calculate cone response of CIE XYZ colors
  77. const math::vec3<T> w0_cone_response = cone_response * w0_xyz;
  78. const math::vec3<T> w1_cone_response = cone_response * w1_xyz;
  79. const math::mat3<T> scale =
  80. {
  81. w1_cone_response[0] / w0_cone_response[0], T{0}, T{0},
  82. T{0}, w1_cone_response[1] / w0_cone_response[1], T{0},
  83. T{0}, T{0}, w1_cone_response[2] / w0_cone_response[2],
  84. };
  85. return math::inverse(cone_response) * scale * cone_response;
  86. }
  87. /// @}
  88. } // namespace color
  89. #endif // ANTKEEPER_COLOR_CAT_HPP