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

167 lines
4.2 KiB

  1. /*
  2. * Copyright (C) 2021 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_XYZ_HPP
  20. #define ANTKEEPER_COLOR_XYZ_HPP
  21. #include "math/math.hpp"
  22. namespace color {
  23. /// Functions which operate in the CIE XYZ colorspace.
  24. namespace xyz {
  25. /**
  26. * Returns the luminance of a CIE XYZ color.
  27. *
  28. * @param x CIE XYZ color.
  29. * @return return Luminance of @p x.
  30. */
  31. template <class T>
  32. T luminance(const math::vector3<T>& x);
  33. /**
  34. * Transforms a CIE XYZ color into the ACEScg colorspace.
  35. *
  36. * @param x CIE XYZ color.
  37. * @return ACEScg color.
  38. */
  39. template <class T>
  40. math::vector3<T> to_acescg(const math::vector3<T>& x);
  41. /**
  42. * Transforms a CIE XYZ color into the linear sRGB colorspace.
  43. *
  44. * @param x CIE XYZ color.
  45. * @return Linear sRGB color.
  46. */
  47. template <class T>
  48. math::vector3<T> to_srgb(const math::vector3<T>& x);
  49. /**
  50. * Transforms a CIE XYZ color into the CIE xyY colorspace.
  51. *
  52. * @param x CIE XYZ color.
  53. * @return CIE xyY color.
  54. */
  55. template <class T>
  56. math::vector3<T> to_xyy(const math::vector3<T>& x);
  57. /// Chromatic Adaptation Transforms (CATs).
  58. namespace cat {
  59. /**
  60. * Transforms a CIE XYZ color with an ACES (~D60) illuminant to a CIE XYZ color with a D65 illuminant using the Bradford chromatic adaption transform.
  61. *
  62. * @param x CIE XYZ color with an ACES (~D60) illuminant.
  63. * @return CIE XYZ color with a D65 illuminant.
  64. */
  65. template <class T>
  66. math::vector3<T> aces_to_d65(const math::vector3<T>& x);
  67. /**
  68. * Transforms a CIE XYZ color with a D65 illuminant to a CIE XYZ color with an ACES (~D60) illuminant using the Bradford chromatic adaption transform.
  69. *
  70. * @param x CIE XYZ color with a D65 illuminant.
  71. * @return CIE XYZ color with an ACES (~D60) illuminant.
  72. */
  73. template <class T>
  74. math::vector3<T> d65_to_aces(const math::vector3<T>& x);
  75. } // namespace cat
  76. template <class T>
  77. inline T luminance(const math::vector3<T>& x)
  78. {
  79. return x[1];
  80. }
  81. template <class T>
  82. math::vector3<T> to_acescg(const math::vector3<T>& x)
  83. {
  84. static const math::matrix3<T> xyz_to_acescg
  85. {{
  86. { 1.641023379694326, -0.663662858722983, 0.011721894328375},
  87. {-0.324803294184790, 1.615331591657338, -0.008284441996237},
  88. {-0.236424695237612, 0.016756347685530, 0.988394858539022}
  89. }};
  90. return xyz_to_acescg * x;
  91. }
  92. template <class T>
  93. math::vector3<T> to_srgb(const math::vector3<T>& x)
  94. {
  95. static const math::matrix3<T> xyz_to_srgb
  96. {{
  97. { 3.240969941904523, -0.969243636280880, 0.055630079696994},
  98. {-1.537383177570094, 1.875967501507721, -0.203976958888977},
  99. {-0.498610760293003, 0.041555057407176, 1.056971514242879}
  100. }};
  101. return xyz_to_srgb * x;
  102. }
  103. template <class T>
  104. math::vector3<T> to_xyy(const math::vector3<T>& x)
  105. {
  106. const T sum = x[0] + x[1] + x[2];
  107. return math::vector3<T>
  108. {
  109. x[0] / sum
  110. x[1] / sum,
  111. x[1]
  112. };
  113. }
  114. namespace cat {
  115. template <class T>
  116. math::vector3<T> aces_to_d65(const math::vector3<T>& x)
  117. {
  118. static const math::matrix3<T> cat_aces_to_d65
  119. {{
  120. { 0.987225397551079, -0.007603864790602, 0.003066041131217},
  121. {-0.006114800968213, 1.001874800208719, -0.005084242870792},
  122. { 0.015926393295811, 0.005322023893623, 1.081519155692042}
  123. }};
  124. return cat_aces_to_d65 * x;
  125. }
  126. template <class T>
  127. math::vector3<T> d65_to_aces(const math::vector3<T>& x)
  128. {
  129. static const math::matrix3<T> cat_d65_to_aces
  130. {{
  131. { 1.013033366661459, 0.007703617525351, -0.002835673220262},
  132. { 0.006107049021859, 0.998150224406968, 0.004675010768250},
  133. {-0.014947927269674, -0.005025218608126, 0.924644077051100}
  134. }};
  135. return cat_d65_to_aces * x;
  136. }
  137. } // namespace cat
  138. } // namespace xyz
  139. } // namespace color
  140. #endif // ANTKEEPER_COLOR_XYZ_HPP