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

88 lines
2.6 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_CCT_HPP
  20. #define ANTKEEPER_COLOR_CCT_HPP
  21. #include "math/math.hpp"
  22. #include "ucs.hpp"
  23. #include "xyy.hpp"
  24. namespace color {
  25. /// Functions which operate on correlated color temperature (CCT).
  26. namespace cct {
  27. /**
  28. * Calculates CIE 1960 UCS colorspace chromaticity coordinates given a correlated color temperature using Krystek's algorithm.
  29. *
  30. * @param t Correlated color temperature, in Kelvin.
  31. * @return CIE 1960 UCS colorspace chromaticity coordinates.
  32. *
  33. * @see Krystek, M. (1985), An algorithm to calculate correlated colour temperature. Color Res. Appl., 10: 38-40.
  34. */
  35. template <class T>
  36. math::vector2<T> to_ucs(T t);
  37. /**
  38. * Calculates CIE xyY colorspace chromaticity coordinates given a correlated color temperature using Krystek's algorithm.
  39. *
  40. * @param t Correlated color temperature, in Kelvin.
  41. * @return CIE xyY color with `Y = 1`.
  42. */
  43. template <class T>
  44. math::vector3<T> to_xyy(T t);
  45. /**
  46. * Calculates CIE XYZ colorspace chromaticity coordinates given a correlated color temperature using Krystek's algorithm.
  47. *
  48. * @param t Correlated color temperature, in Kelvin.
  49. * @return CIE XYZ color with `Y = 1`.
  50. */
  51. template <class T>
  52. math::vector3<T> to_xyz(T t);
  53. template <class T>
  54. math::vector2<T> to_ucs(T t)
  55. {
  56. // Approximate the Planckian locus in CIE 1960 UCS colorspace (Krystek's algorithm)
  57. const T tt = t * t;
  58. return math::vector2<T>
  59. {
  60. (T(0.860117757) + T(1.54118254e-4) * t + T(1.28641212e-7) * tt) / (T(1.0) + T(8.42420235e-4) * t + T(7.08145163e-7) * tt),
  61. (T(0.317398726) + T(4.22806245e-5) * t + T(4.20481691e-8) * tt) / (T(1.0) - T(2.89741816e-5) * t + T(1.61456053e-7) * tt)
  62. };
  63. }
  64. template <class T>
  65. math::vector3<T> to_xyy(T t)
  66. {
  67. return ucs::to_xyy(to_ucs(t), T(1.0));
  68. }
  69. template <class T>
  70. math::vector3<T> to_xyz(T t)
  71. {
  72. return xyy::to_xyz(to_xyy(t));
  73. }
  74. } // namespace cct
  75. } // namespace color
  76. #endif // ANTKEEPER_COLOR_CCT_HPP