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

107 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. /// Chromatic adaption transforms (CAT).
  25. namespace cat {
  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 =
  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 =
  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 =
  58. {
  59. T{1}, T{0}, T{0},
  60. T{0}, T{1}, T{0},
  61. T{0}, T{0}, T{1}
  62. };
  63. /**
  64. * Constructs a chromatic adaptation transform (CAT) matrix.
  65. *
  66. * @param w0 CIE xy chromaticity coordinates of the source illuminant.
  67. * @param w1 CIE xy chromaticity coordinates of the destination illuminant.
  68. * @param cone_response Cone response matrix.
  69. *
  70. * @return CAT matrix.
  71. *
  72. * @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.
  73. * @see http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html
  74. */
  75. template <class T>
  76. [[nodiscard]] constexpr math::mat3<T> matrix(const math::vec2<T>& w0, const math::vec2<T>& w1, const math::mat3<T>& cone_response = bradford<T>) noexcept
  77. {
  78. // Convert CIE xy chromaticity coordinates to CIE XYZ colors
  79. const math::vec3<T> w0_xyz = {w0[0] / w0[1], T{1}, (T{1} - w0[0] - w0[1]) / w0[1]};
  80. const math::vec3<T> w1_xyz = {w1[0] / w1[1], T{1}, (T{1} - w1[0] - w1[1]) / w1[1]};
  81. // Calculate cone response of CIE XYZ colors
  82. const math::vec3<T> w0_cone_response = cone_response * w0_xyz;
  83. const math::vec3<T> w1_cone_response = cone_response * w1_xyz;
  84. const math::mat3<T> scale =
  85. {
  86. w1_cone_response[0] / w0_cone_response[0], T{0}, T{0},
  87. T{0}, w1_cone_response[1] / w0_cone_response[1], T{0},
  88. T{0}, T{0}, w1_cone_response[2] / w0_cone_response[2],
  89. };
  90. return math::inverse(cone_response) * scale * cone_response;
  91. }
  92. } // namespace cat
  93. } // namespace color
  94. #endif // ANTKEEPER_COLOR_CAT_HPP