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

168 lines
4.2 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_SRGB_HPP
  20. #define ANTKEEPER_COLOR_SRGB_HPP
  21. #include "math/math.hpp"
  22. #include <cmath>
  23. namespace color {
  24. /// Functions which operate in the sRGB colorspace.
  25. namespace srgb {
  26. /// CIE chromaticity coordinates of the sRGB white point (D65)
  27. template <class T>
  28. constexpr math::vector2<T> whitepoint = {0.31271, 0.32902};
  29. /**
  30. * Performs the sRGB Electro-Optical Transfer Function (EOTF), also known as the sRGB decoding function.
  31. *
  32. * @param v sRGB electrical signal (gamma-encoded sRGB).
  33. * @return Corresponding luminance of the signal (linear sRGB).
  34. */
  35. /// @{
  36. float eotf(float v);
  37. double eotf(double v);
  38. template <class T>
  39. math::vector3<T> eotf(const math::vector3<T>& v);
  40. /// @}
  41. /**
  42. * Performs the sRGB inverse Electro-Optical Transfer Function (EOTF), also known as the sRGB encoding function.
  43. *
  44. * @param l sRGB luminance (linear sRGB).
  45. * @return Corresponding electrical signal (gamma-encoded sRGB).
  46. */
  47. /// @{
  48. float eotf_inverse(float v);
  49. double eotf_inverse(double v);
  50. template <class T>
  51. math::vector3<T> eotf_inverse(const math::vector3<T>& l);
  52. /// @}
  53. /**
  54. * Calculates the luminance of a linear sRGB color.
  55. *
  56. * @param x Linear sRGB color.
  57. * @return return Luminance of @p x.
  58. */
  59. template <class T>
  60. T luminance(const math::vector3<T>& x);
  61. /**
  62. * Transforms a linear sRGB color into the ACEScg colorspace using the Bradford chromatic adaption transform.
  63. *
  64. * @param x Linear sRGB color.
  65. * @return ACEScg color.
  66. *
  67. * @see https://www.colour-science.org/apps/
  68. */
  69. template <class T>
  70. math::vector3<T> to_acescg(const math::vector3<T>& x);
  71. /**
  72. * Transforms a linear sRGB color into the CIE XYZ colorspace.
  73. *
  74. * @param x Linear sRGB color.
  75. * @return CIE XYZ color.
  76. */
  77. template <class T>
  78. math::vector3<T> to_xyz(const math::vector3<T>& x);
  79. inline float eotf(float v)
  80. {
  81. return (v < 0.04045f) ? (v / 12.92f) : std::pow((v + 0.055f) / 1.055f, 2.4f);
  82. }
  83. inline double eotf(double v)
  84. {
  85. return (v < 0.04045) ? (v / 12.92) : std::pow((v + 0.055) / 1.055, 2.4);
  86. }
  87. template <class T>
  88. math::vector3<T> eotf(const math::vector3<T>& v)
  89. {
  90. return math::vector3<T>
  91. {
  92. eotf(v[0]),
  93. eotf(v[1]),
  94. eotf(v[2])
  95. };
  96. }
  97. inline float eotf_inverse(float l)
  98. {
  99. return (l <= 0.0031308f) ? (l * 12.92f) : (std::pow(l, 1.0f / 2.4f) * 1.055f - 0.055f);
  100. }
  101. inline double eotf_inverse(double l)
  102. {
  103. return (l <= 0.0031308) ? (l * 12.92) : (std::pow(l, 1.0 / 2.4) * 1.055 - 0.055);
  104. }
  105. template <class T>
  106. math::vector3<T> eotf_inverse(const math::vector3<T>& l)
  107. {
  108. return math::vector3<T>
  109. {
  110. eotf_inverse(l[0]),
  111. eotf_inverse(l[1]),
  112. eotf_inverse(l[2])
  113. };
  114. }
  115. template <class T>
  116. T luminance(const math::vector3<T>& x)
  117. {
  118. static const math::vector3<T> luma = {0.212639005871510, 0.715168678767756, 0.072192315360734};
  119. return math::dot(x, luma);
  120. }
  121. template <class T>
  122. math::vector3<T> to_acescg(const math::vector3<T>& x)
  123. {
  124. static const math::matrix3<T> srgb_to_acescg
  125. {{
  126. {0.613132422390542, 0.070124380833917, 0.020587657528185},
  127. {0.339538015799666, 0.916394011313573, 0.109574571610682},
  128. {0.047416696048269, 0.013451523958235, 0.869785404035327}
  129. }};
  130. return srgb_to_acescg * x;
  131. }
  132. template <class T>
  133. math::vector3<T> to_xyz(const math::vector3<T>& x)
  134. {
  135. static const math::matrix3<T> srgb_to_xyz
  136. {{
  137. {0.412390799265959, 0.212639005871510, 0.019330818715592},
  138. {0.357584339383878, 0.715168678767756, 0.119194779794626},
  139. {0.180480788401834, 0.072192315360734, 0.950532152249661}
  140. }};
  141. return srgb_to_xyz * x;
  142. }
  143. } // namespace srgb
  144. } // namespace color
  145. #endif // ANTKEEPER_COLOR_SRGB_HPP