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

90 lines
2.2 KiB

  1. /*
  2. * Copyright (C) 2023 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 "color/rgb.hpp"
  22. #include "color/illuminant.hpp"
  23. #include "math/vector.hpp"
  24. #include <cmath>
  25. namespace color {
  26. /**
  27. * sRGB electro-optical transfer function (EOTF), also known as the sRGB decoding function.
  28. *
  29. * @param v sRGB electrical signal (gamma-encoded sRGB).
  30. *
  31. * @return Corresponding luminance of the signal (linear sRGB).
  32. */
  33. template <class T>
  34. math::vector3<T> srgb_eotf(const math::vector3<T>& v)
  35. {
  36. auto f = [](T x) -> T
  37. {
  38. return x < T{0.04045} ? x / T{12.92} : std::pow((x + T{0.055}) / T{1.055}, T{2.4});
  39. };
  40. return math::vector3<T>
  41. {
  42. f(v[0]),
  43. f(v[1]),
  44. f(v[2])
  45. };
  46. }
  47. /**
  48. * sRGB inverse electro-optical transfer function (EOTF), also known as the sRGB encoding function.
  49. *
  50. * @param l sRGB luminance (linear sRGB).
  51. *
  52. * @return Corresponding electrical signal (gamma-encoded sRGB).
  53. */
  54. template <class T>
  55. math::vector3<T> srgb_inverse_eotf(const math::vector3<T>& l)
  56. {
  57. auto f = [](T x) -> T
  58. {
  59. return x <= T{0.0031308} ? x * T{12.92} : std::pow(x, T{1} / T{2.4}) * T{1.055} - T{0.055};
  60. };
  61. return math::vector3<T>
  62. {
  63. f(l[0]),
  64. f(l[1]),
  65. f(l[2])
  66. };
  67. }
  68. /// sRGB color space.
  69. template <class T>
  70. constexpr rgb::color_space<T> srgb
  71. (
  72. {T{0.64}, T{0.33}},
  73. {T{0.30}, T{0.60}},
  74. {T{0.15}, T{0.06}},
  75. color::illuminant::deg2::d65<T>,
  76. &srgb_eotf<T>,
  77. &srgb_inverse_eotf
  78. );
  79. } // namespace color
  80. #endif // ANTKEEPER_COLOR_SRGB_HPP