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

56 lines
1.7 KiB

  1. /*
  2. * Copyright (C) 2020 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 <cmath>
  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, double l)
  31. {
  32. // Approximate the Planckian locus in CIE 1960 color space
  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. // (u, v) -> (x, y)
  37. double denom = (u * 2.0 - v * 8.0 + 4.0);
  38. double x = u * 3.0 / denom;
  39. double y = v * 2.0 / denom;
  40. // (x, y) -> CIE XYZ
  41. double3 xyz;
  42. xyz.y = l;
  43. xyz.x = (xyz.y / y) * x;
  44. xyz.z = (xyz.y / y) * (1.0 - x - y);
  45. /// CIE XYZ -> linear RGB
  46. return xyz_to_rgb * xyz;
  47. }
  48. } // namespace ast