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

78 lines
2.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_CCT_HPP
  20. #define ANTKEEPER_COLOR_CCT_HPP
  21. #include "ucs.hpp"
  22. #include "xyy.hpp"
  23. #include "math/vector.hpp"
  24. namespace color {
  25. /// 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. const T tt = t * t;
  39. return math::vector2<T>
  40. {
  41. (T{0.860117757} + T{1.54118254e-4} * t + T{1.28641212e-7} * tt) / (T{1} + T{8.42420235e-4} * t + T{7.08145163e-7} * tt),
  42. (T{0.317398726} + T{4.22806245e-5} * t + T{4.20481691e-8} * tt) / (T{1} - T{2.89741816e-5} * t + T{1.61456053e-7} * tt)
  43. };
  44. }
  45. /**
  46. * Calculates CIE xyY colorspace chromaticity coordinates given a correlated color temperature using Krystek's algorithm.
  47. *
  48. * @param t Correlated color temperature, in Kelvin.
  49. * @return CIE xyY color with `Y = 1`.
  50. */
  51. template <class T>
  52. math::vector3<T> to_xyy(T t)
  53. {
  54. return ucs::to_xyy(to_ucs(t), T{1});
  55. }
  56. /**
  57. * Calculates CIE XYZ colorspace chromaticity coordinates given a correlated color temperature using Krystek's algorithm.
  58. *
  59. * @param t Correlated color temperature, in Kelvin.
  60. * @return CIE XYZ color with `Y = 1`.
  61. */
  62. template <class T>
  63. math::vector3<T> to_xyz(T t)
  64. {
  65. return xyy::to_xyz(to_xyy(t));
  66. }
  67. } // namespace cct
  68. } // namespace color
  69. #endif // ANTKEEPER_COLOR_CCT_HPP