Browse Source

Replace sRGB<->Linear RGB conversion functions with more accurate versions

master
C. J. Howard 4 years ago
parent
commit
db50cec8d3
1 changed files with 16 additions and 6 deletions
  1. +16
    -6
      src/utility/gamma.hpp

+ 16
- 6
src/utility/gamma.hpp View File

@ -26,11 +26,16 @@
template <typename T, std::size_t N>
math::vector<T, N> to_linear(math::vector<T, N> v)
{
constexpr T gamma = T(2.2);
for (std::size_t i = 0; i < N; ++i)
{
v[i] = pow(v[i], gamma);
if (v[i] <= T(0.04045))
{
v[i] /= T(12.92);
}
else
{
v[i] = std::pow((v[i] + T(0.055)) / T(1.055), T(2.4));
}
}
return v;
@ -39,11 +44,16 @@ math::vector to_linear(math::vector v)
template <typename T, std::size_t N>
math::vector<T, N> to_srgb(math::vector<T, N> v)
{
constexpr T inverse_gamma = T(1) / T(2.2);
for (std::size_t i = 0; i < N; ++i)
{
v[i] = pow(v[i], inverse_gamma);
if (v[i] <= T(0.0031308))
{
v[i] *= T(12.92);
}
else
{
v[i] = std::pow(v[i], T(1.0 / 2.4)) * T(1.055) - T(0.055);
}
}
return v;

Loading…
Cancel
Save