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

99 lines
2.7 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_ACESCG_HPP
  20. #define ANTKEEPER_COLOR_ACESCG_HPP
  21. #include "math/math.hpp"
  22. namespace color {
  23. /// Functions which operate in the ACEScg colorspace.
  24. namespace acescg {
  25. /// CIE chromaticity coordinates of the ACEScg white point (~D60).
  26. template <class T>
  27. constexpr math::vector2<T> whitepoint = {0.32168, 0.33767};
  28. /**
  29. * Calculates the luminance of an ACEScg color.
  30. *
  31. * @param x ACEScg color.
  32. * @return return Luminance of @p x.
  33. */
  34. template <class T>
  35. T luminance(const math::vector3<T>& x);
  36. /**
  37. * Transforms an ACEScg color into the linear sRGB colorspace using the Bradford chromatic adaption transform.
  38. *
  39. * @param x ACEScg color.
  40. * @return return Linear sRGB color.
  41. *
  42. * @see https://www.colour-science.org/apps/
  43. */
  44. template <class T>
  45. math::vector3<T> to_srgb(const math::vector3<T>& x);
  46. /**
  47. * Transforms an ACEScg color into the CIE XYZ colorspace.
  48. *
  49. * @param x ACEScg color.
  50. * @return return CIE XYZ color.
  51. */
  52. template <class T>
  53. math::vector3<T> to_xyz(const math::vector3<T>& x);
  54. template <class T>
  55. T luminance(const math::vector3<T>& x)
  56. {
  57. static const math::vector3<T> luma = {0.272228716780914, 0.674081765811148, 0.053689517407937};
  58. return math::dot(x, luma);
  59. }
  60. template <class T>
  61. math::vector3<T> to_srgb(const math::vector3<T>& x)
  62. {
  63. static const math::matrix3<T> acescg_to_srgb
  64. {{
  65. { 1.704858676289160, -0.130076824208823, -0.023964072927574},
  66. {-0.621716021885330, 1.140735774822504, -0.128975508299318},
  67. {-0.083299371729058, -0.010559801677511, 1.153014018916862}
  68. }};
  69. return acescg_to_srgb * x;
  70. }
  71. template <class T>
  72. math::vector3<T> to_xyz(const math::vector3<T>& x)
  73. {
  74. static const math::matrix3<T> acescg_to_xyz
  75. {{
  76. {0.662454181108505, 0.272228716780914, -0.005574649490394},
  77. {0.134004206456433, 0.674081765811148, 0.004060733528983},
  78. {0.156187687004908, 0.053689517407937, 1.010339100312997}
  79. }};
  80. return acescg_to_xyz * x;
  81. }
  82. } // namespace acescg
  83. } // namespace color
  84. #endif // ANTKEEPER_COLOR_ACESCG_HPP