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

147 lines
3.8 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_XYZ_HPP
  20. #define ANTKEEPER_COLOR_XYZ_HPP
  21. #include "math/vector.hpp"
  22. namespace color {
  23. /**
  24. * CIE XYZ color space.
  25. *
  26. * @see https://en.wikipedia.org/wiki/CIE_1931_color_space
  27. */
  28. namespace xyz {
  29. /**
  30. * Returns the luminance of a CIE XYZ color.
  31. *
  32. * @param x CIE XYZ color.
  33. * @return return Luminance of @p x.
  34. */
  35. template <class T>
  36. inline constexpr T luminance(const math::vector3<T>& x)
  37. {
  38. return x[1];
  39. }
  40. /**
  41. * Transforms a CIE XYZ color into the CIE xyY color space.
  42. *
  43. * @param x CIE XYZ color.
  44. * @return CIE xyY color.
  45. */
  46. template <class T>
  47. constexpr math::vector3<T> to_xyy(const math::vector3<T>& x)
  48. {
  49. const T sum = x[0] + x[1] + x[2];
  50. return math::vector3<T>{x[0] / sum, x[1] / sum, x[1]};
  51. }
  52. /**
  53. * CIE 1931 standard observer color matching function for the X tristimulus value.
  54. *
  55. * @param lambda Wavelength of light, in nanometers.
  56. * @return Matching X tristimulus value.
  57. *
  58. * @see match(T)
  59. */
  60. template <class T>
  61. T match_x(T lambda)
  62. {
  63. const T t0 = (lambda - T(442.0)) * ((lambda < T(442.0)) ? T(0.0624) : T(0.0374));
  64. const T t1 = (lambda - T(599.8)) * ((lambda < T(599.8)) ? T(0.0264) : T(0.0323));
  65. const T t2 = (lambda - T(501.1)) * ((lambda < T(501.1)) ? T(0.0490) : T(0.0382));
  66. const T x0 = T( 0.362) * std::exp(T(-0.5) * t0 * t0);
  67. const T x1 = T( 1.056) * std::exp(T(-0.5) * t1 * t1);
  68. const T x2 = T(-0.065) * std::exp(T(-0.5) * t2 * t2);
  69. return x0 + x1 + x2;
  70. }
  71. /**
  72. * CIE 1931 standard observer color matching function for the Y tristimulus value.
  73. *
  74. * @param lambda Wavelength of light, in nanometers.
  75. * @return Matching Y tristimulus value.
  76. *
  77. * @see match(T)
  78. */
  79. template <class T>
  80. T match_y(T lambda)
  81. {
  82. const T t0 = (lambda - T(568.8)) * ((lambda < T(568.8)) ? T(0.0213) : T(0.0247));
  83. const T t1 = (lambda - T(530.9)) * ((lambda < T(530.9)) ? T(0.0613) : T(0.0322));
  84. const T y0 = T(0.821) * std::exp(T(-0.5) * t0 * t0);
  85. const T y1 = T(0.286) * std::exp(T(-0.5) * t1 * t1);
  86. return y0 + y1;
  87. }
  88. /**
  89. * CIE 1931 standard observer color matching function for the Z tristimulus value.
  90. *
  91. * @param lambda Wavelength of light, in nanometers.
  92. * @return Matching Z tristimulus value.
  93. *
  94. * @see match(T)
  95. */
  96. template <class T>
  97. T match_z(T lambda)
  98. {
  99. const T t0 = (lambda - T(437.0)) * ((lambda < T(437.0)) ? T(0.0845) : T(0.0278));
  100. const T t1 = (lambda - T(459.0)) * ((lambda < T(459.0)) ? T(0.0385) : T(0.0725));
  101. const T z0 = T(1.217) * std::exp(T(-0.5) * t0 * t0);
  102. const T z1 = T(0.681) * std::exp(T(-0.5) * t1 * t1);
  103. return z0 + z1;
  104. }
  105. /**
  106. * Fitted piecewise gaussian approximation to the CIE 1931 standard observer color matching function.
  107. *
  108. * @param lambda Wavelength of light, in nanometers.
  109. * @return Matching CIE XYZ color.
  110. *
  111. * @see match_x(T)
  112. * @see match_y(T)
  113. * @see match_z(T)
  114. *
  115. * @see Wyman, C., Sloan, P.J., & Shirley, P. (2013). Simple Analytic Approximations to the CIE XYZ Color Matching Functions.
  116. */
  117. template <class T>
  118. math::vector3<T> match(T lambda)
  119. {
  120. return math::vector3<T>
  121. {
  122. match_x<T>(lambda),
  123. match_y<T>(lambda),
  124. match_z<T>(lambda)
  125. };
  126. }
  127. } // namespace xyz
  128. } // namespace color
  129. #endif // ANTKEEPER_COLOR_XYZ_HPP