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.

149 lines
3.6 KiB

  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2013 G-Truc Creation (www.g-truc.net)
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Created : 2005-12-21
  5. // Updated : 2007-02-22
  6. // Licence : This source is under MIT License
  7. // File : glm/gtx/color_space.inl
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. namespace glm
  10. {
  11. template <typename T>
  12. GLM_FUNC_QUALIFIER detail::tvec3<T> rgbColor(const detail::tvec3<T>& hsvColor)
  13. {
  14. detail::tvec3<T> hsv = hsvColor;
  15. detail::tvec3<T> rgbColor;
  16. if(hsv.y == T(0))
  17. // achromatic (grey)
  18. rgbColor = detail::tvec3<T>(hsv.z);
  19. else
  20. {
  21. T sector = floor(hsv.x / T(60));
  22. T frac = (hsv.x / T(60)) - sector;
  23. // factorial part of h
  24. T o = hsv.z * (T(1) - hsv.y);
  25. T p = hsv.z * (T(1) - hsv.y * frac);
  26. T q = hsv.z * (T(1) - hsv.y * (T(1) - frac));
  27. switch(int(sector))
  28. {
  29. default:
  30. case 0:
  31. rgbColor.r = hsv.z;
  32. rgbColor.g = q;
  33. rgbColor.b = o;
  34. break;
  35. case 1:
  36. rgbColor.r = p;
  37. rgbColor.g = hsv.z;
  38. rgbColor.b = o;
  39. break;
  40. case 2:
  41. rgbColor.r = o;
  42. rgbColor.g = hsv.z;
  43. rgbColor.b = q;
  44. break;
  45. case 3:
  46. rgbColor.r = o;
  47. rgbColor.g = p;
  48. rgbColor.b = hsv.z;
  49. break;
  50. case 4:
  51. rgbColor.r = q;
  52. rgbColor.g = o;
  53. rgbColor.b = hsv.z;
  54. break;
  55. case 5:
  56. rgbColor.r = hsv.z;
  57. rgbColor.g = o;
  58. rgbColor.b = p;
  59. break;
  60. }
  61. }
  62. return rgbColor;
  63. }
  64. template <typename T>
  65. GLM_FUNC_QUALIFIER detail::tvec3<T> hsvColor(const detail::tvec3<T>& rgbColor)
  66. {
  67. detail::tvec3<T> hsv = rgbColor;
  68. float Min = min(min(rgbColor.r, rgbColor.g), rgbColor.b);
  69. float Max = max(max(rgbColor.r, rgbColor.g), rgbColor.b);
  70. float Delta = Max - Min;
  71. hsv.z = Max;
  72. if(Max != T(0))
  73. {
  74. hsv.y = Delta / hsv.z;
  75. T h = T(0);
  76. if(rgbColor.r == Max)
  77. // between yellow & magenta
  78. h = T(0) + T(60) * (rgbColor.g - rgbColor.b) / Delta;
  79. else if(rgbColor.g == Max)
  80. // between cyan & yellow
  81. h = T(120) + T(60) * (rgbColor.b - rgbColor.r) / Delta;
  82. else
  83. // between magenta & cyan
  84. h = T(240) + T(60) * (rgbColor.r - rgbColor.g) / Delta;
  85. if(h < T(0))
  86. hsv.x = h + T(360);
  87. else
  88. hsv.x = h;
  89. }
  90. else
  91. {
  92. // If r = g = b = 0 then s = 0, h is undefined
  93. hsv.y = T(0);
  94. hsv.x = T(0);
  95. }
  96. return hsv;
  97. }
  98. template <typename T>
  99. GLM_FUNC_QUALIFIER detail::tmat4x4<T> saturation(const T s)
  100. {
  101. detail::tvec3<T> rgbw = detail::tvec3<T>(T(0.2126), T(0.7152), T(0.0722));
  102. T col0 = (T(1) - s) * rgbw.r;
  103. T col1 = (T(1) - s) * rgbw.g;
  104. T col2 = (T(1) - s) * rgbw.b;
  105. detail::tmat4x4<T> result(T(1));
  106. result[0][0] = col0 + s;
  107. result[0][1] = col0;
  108. result[0][2] = col0;
  109. result[1][0] = col1;
  110. result[1][1] = col1 + s;
  111. result[1][2] = col1;
  112. result[2][0] = col2;
  113. result[2][1] = col2;
  114. result[2][2] = col2 + s;
  115. return result;
  116. }
  117. template <typename T>
  118. GLM_FUNC_QUALIFIER detail::tvec3<T> saturation(const T s, const detail::tvec3<T>& color)
  119. {
  120. return detail::tvec3<T>(saturation(s) * detail::tvec4<T>(color, T(0)));
  121. }
  122. template <typename T>
  123. GLM_FUNC_QUALIFIER detail::tvec4<T> saturation(const T s, const detail::tvec4<T>& color)
  124. {
  125. return saturation(s) * color;
  126. }
  127. template <typename T>
  128. GLM_FUNC_QUALIFIER T luminosity(const detail::tvec3<T>& color)
  129. {
  130. const detail::tvec3<T> tmp = detail::tvec3<T>(0.33, 0.59, 0.11);
  131. return dot(color, tmp);
  132. }
  133. }//namespace glm