color_space.inl 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /// @ref gtc_color_space
  2. namespace glm{
  3. namespace detail
  4. {
  5. template<length_t L, typename T, qualifier Q>
  6. struct compute_rgbToSrgb
  7. {
  8. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& ColorRGB, T GammaCorrection)
  9. {
  10. vec<L, T, Q> const ClampedColor(clamp(ColorRGB, static_cast<T>(0), static_cast<T>(1)));
  11. return mix(
  12. pow(ClampedColor, vec<L, T, Q>(GammaCorrection)) * static_cast<T>(1.055) - static_cast<T>(0.055),
  13. ClampedColor * static_cast<T>(12.92),
  14. lessThan(ClampedColor, vec<L, T, Q>(static_cast<T>(0.0031308))));
  15. }
  16. };
  17. template<typename T, qualifier Q>
  18. struct compute_rgbToSrgb<4, T, Q>
  19. {
  20. GLM_FUNC_QUALIFIER static vec<4, T, Q> call(vec<4, T, Q> const& ColorRGB, T GammaCorrection)
  21. {
  22. return vec<4, T, Q>(compute_rgbToSrgb<3, T, Q>::call(vec<3, T, Q>(ColorRGB), GammaCorrection), ColorRGB.w);
  23. }
  24. };
  25. template<length_t L, typename T, qualifier Q>
  26. struct compute_srgbToRgb
  27. {
  28. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& ColorSRGB, T Gamma)
  29. {
  30. return mix(
  31. pow((ColorSRGB + static_cast<T>(0.055)) * static_cast<T>(0.94786729857819905213270142180095), vec<L, T, Q>(Gamma)),
  32. ColorSRGB * static_cast<T>(0.07739938080495356037151702786378),
  33. lessThanEqual(ColorSRGB, vec<L, T, Q>(static_cast<T>(0.04045))));
  34. }
  35. };
  36. template<typename T, qualifier Q>
  37. struct compute_srgbToRgb<4, T, Q>
  38. {
  39. GLM_FUNC_QUALIFIER static vec<4, T, Q> call(vec<4, T, Q> const& ColorSRGB, T Gamma)
  40. {
  41. return vec<4, T, Q>(compute_srgbToRgb<3, T, Q>::call(vec<3, T, Q>(ColorSRGB), Gamma), ColorSRGB.w);
  42. }
  43. };
  44. }//namespace detail
  45. template<length_t L, typename T, qualifier Q>
  46. GLM_FUNC_QUALIFIER vec<L, T, Q> convertLinearToSRGB(vec<L, T, Q> const& ColorLinear)
  47. {
  48. return detail::compute_rgbToSrgb<L, T, Q>::call(ColorLinear, static_cast<T>(0.41666));
  49. }
  50. // Based on Ian Taylor http://chilliant.blogspot.fr/2012/08/srgb-approximations-for-hlsl.html
  51. template<>
  52. GLM_FUNC_QUALIFIER vec<3, float, lowp> convertLinearToSRGB(vec<3, float, lowp> const& ColorLinear)
  53. {
  54. vec<3, float, lowp> S1 = sqrt(ColorLinear);
  55. vec<3, float, lowp> S2 = sqrt(S1);
  56. vec<3, float, lowp> S3 = sqrt(S2);
  57. return 0.662002687f * S1 + 0.684122060f * S2 - 0.323583601f * S3 - 0.0225411470f * ColorLinear;
  58. }
  59. template<length_t L, typename T, qualifier Q>
  60. GLM_FUNC_QUALIFIER vec<L, T, Q> convertLinearToSRGB(vec<L, T, Q> const& ColorLinear, T Gamma)
  61. {
  62. return detail::compute_rgbToSrgb<L, T, Q>::call(ColorLinear, static_cast<T>(1) / Gamma);
  63. }
  64. template<length_t L, typename T, qualifier Q>
  65. GLM_FUNC_QUALIFIER vec<L, T, Q> convertSRGBToLinear(vec<L, T, Q> const& ColorSRGB)
  66. {
  67. return detail::compute_srgbToRgb<L, T, Q>::call(ColorSRGB, static_cast<T>(2.4));
  68. }
  69. template<length_t L, typename T, qualifier Q>
  70. GLM_FUNC_QUALIFIER vec<L, T, Q> convertSRGBToLinear(vec<L, T, Q> const& ColorSRGB, T Gamma)
  71. {
  72. return detail::compute_srgbToRgb<L, T, Q>::call(ColorSRGB, Gamma);
  73. }
  74. }//namespace glm