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

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