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

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