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

54 lines
1.8 KiB

3 years ago
  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. #include "blackbody.hpp"
  20. #include <algorithm>
  21. namespace ast
  22. {
  23. /// Transforms colors from CIE XYZ to linear RGB
  24. static constexpr double3x3 xyz_to_rgb =
  25. {
  26. 3.2404542, -0.9692660, 0.0556434,
  27. -1.5371385, 1.8760108, -0.2040259,
  28. -0.4985314, 0.0415560, 1.0572252
  29. };
  30. double3 blackbody(double t)
  31. {
  32. // Approximate the Planckian locus in CIE 1960 UCS color space (Krystek's algorithm)
  33. double tt = t * t;
  34. double u = (0.860117757 + 1.54118254e-4 * t + 1.28641212e-7 * tt) / (1.0 + 8.42420235e-4 * t + 7.08145163e-7 * tt);
  35. double v = (0.317398726 + 4.22806245e-5 * t + 4.20481691e-8 * tt) / (1.0 - 2.89741816e-5 * t + 1.61456053e-7 * tt);
  36. // CIE 1960 UCS -> CIE xyY, Y = 1
  37. double2 xyy = double2{3.0 * u, 2.0 * v} / (2.0 * u - 8.0 * v + 4.0);
  38. // CIE xyY -> CIE XYZ
  39. double3 xyz = {xyy.x / xyy.y, 1.0, (1.0 - xyy.x - xyy.y) / xyy.y};
  40. // CIE XYZ -> linear RGB
  41. double3 rgb = xyz_to_rgb * xyz;
  42. // Normalize RGB to preserve chromaticity
  43. return rgb / std::max<double>(rgb.x, std::max<double>(rgb.y, rgb.z));
  44. }
  45. } // namespace ast