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

80 lines
2.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_ACES_HPP
  20. #define ANTKEEPER_ACES_HPP
  21. #include "math/vector-type.hpp"
  22. /**
  23. * Transforms a linear sRGB color into the ACEScg colorspace, using the Bradford chromatic adaption transform.
  24. *
  25. * @param x Linear sRGB color.
  26. * @return Linear ACEScg color.
  27. *
  28. * @see https://www.colour-science.org/apps/
  29. */
  30. template <class T>
  31. math::vector<T, 3> srgb_to_acescg(const math::vector<T, 3>& x)
  32. {
  33. static const math::matrix<T, 3, 3> matrix
  34. {{
  35. {0.6131324224, 0.0701243808, 0.0205876575},
  36. {0.3395380158, 0.9163940113, 0.1095745716},
  37. {0.0474166960, 0.0134515240, 0.8697854040}
  38. }};
  39. return matrix * x;
  40. }
  41. /**
  42. * Transforms an ACEScg color into the linear sRGB colorspace, using the Bradford chromatic adaption transform.
  43. *
  44. * @param x Linear ACEScg color.
  45. * @param return Linear sRGB color.
  46. *
  47. * @see https://www.colour-science.org/apps/
  48. */
  49. template <class T>
  50. math::vector<T, 3> acescg_to_srgb(const math::vector<T, 3>& x)
  51. {
  52. static const math::matrix<T, 3, 3> matrix
  53. {{
  54. { 1.7048586763, -0.1300768242, -0.0239640729},
  55. {-0.6217160219, 1.1407357748, -0.1289755083},
  56. {-0.0832993717, -0.0105598017, 1.1530140189}
  57. }};
  58. return matrix * x;
  59. }
  60. /**
  61. * Calculates the luminance of an ACEScg color.
  62. *
  63. * @param x Linear ACEScg color.
  64. * @param return Luminance of @p x.
  65. */
  66. template <class T>
  67. T acescg_to_luminance(const math::vector<T, 3>& x)
  68. {
  69. static const math::vector<T, 3> luma = {0.2722287168, 0.6740817658, 0.0536895174};
  70. return math::dot(x, luma);
  71. }
  72. #endif // ANTKEEPER_ACES_HPP