ImathColorAlgo.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMATHCOLORALGO_H
  35. #define INCLUDED_IMATHCOLORALGO_H
  36. #include "ImathColor.h"
  37. #include "ImathExport.h"
  38. #include "ImathMath.h"
  39. #include "ImathLimits.h"
  40. #include "ImathNamespace.h"
  41. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  42. //
  43. // Non-templated helper routines for color conversion.
  44. // These routines eliminate type warnings under g++.
  45. //
  46. IMATH_EXPORT Vec3<double> hsv2rgb_d(const Vec3<double> &hsv);
  47. IMATH_EXPORT Color4<double> hsv2rgb_d(const Color4<double> &hsv);
  48. IMATH_EXPORT Vec3<double> rgb2hsv_d(const Vec3<double> &rgb);
  49. IMATH_EXPORT Color4<double> rgb2hsv_d(const Color4<double> &rgb);
  50. //
  51. // Color conversion functions and general color algorithms
  52. //
  53. // hsv2rgb(), rgb2hsv(), rgb2packed(), packed2rgb()
  54. // see each funtion definition for details.
  55. //
  56. template<class T>
  57. Vec3<T>
  58. hsv2rgb(const Vec3<T> &hsv)
  59. {
  60. if ( limits<T>::isIntegral() )
  61. {
  62. Vec3<double> v = Vec3<double>(hsv.x / double(limits<T>::max()),
  63. hsv.y / double(limits<T>::max()),
  64. hsv.z / double(limits<T>::max()));
  65. Vec3<double> c = hsv2rgb_d(v);
  66. return Vec3<T>((T) (c.x * limits<T>::max()),
  67. (T) (c.y * limits<T>::max()),
  68. (T) (c.z * limits<T>::max()));
  69. }
  70. else
  71. {
  72. Vec3<double> v = Vec3<double>(hsv.x, hsv.y, hsv.z);
  73. Vec3<double> c = hsv2rgb_d(v);
  74. return Vec3<T>((T) c.x, (T) c.y, (T) c.z);
  75. }
  76. }
  77. template<class T>
  78. Color4<T>
  79. hsv2rgb(const Color4<T> &hsv)
  80. {
  81. if ( limits<T>::isIntegral() )
  82. {
  83. Color4<double> v = Color4<double>(hsv.r / float(limits<T>::max()),
  84. hsv.g / float(limits<T>::max()),
  85. hsv.b / float(limits<T>::max()),
  86. hsv.a / float(limits<T>::max()));
  87. Color4<double> c = hsv2rgb_d(v);
  88. return Color4<T>((T) (c.r * limits<T>::max()),
  89. (T) (c.g * limits<T>::max()),
  90. (T) (c.b * limits<T>::max()),
  91. (T) (c.a * limits<T>::max()));
  92. }
  93. else
  94. {
  95. Color4<double> v = Color4<double>(hsv.r, hsv.g, hsv.b, hsv.a);
  96. Color4<double> c = hsv2rgb_d(v);
  97. return Color4<T>((T) c.r, (T) c.g, (T) c.b, (T) c.a);
  98. }
  99. }
  100. template<class T>
  101. Vec3<T>
  102. rgb2hsv(const Vec3<T> &rgb)
  103. {
  104. if ( limits<T>::isIntegral() )
  105. {
  106. Vec3<double> v = Vec3<double>(rgb.x / double(limits<T>::max()),
  107. rgb.y / double(limits<T>::max()),
  108. rgb.z / double(limits<T>::max()));
  109. Vec3<double> c = rgb2hsv_d(v);
  110. return Vec3<T>((T) (c.x * limits<T>::max()),
  111. (T) (c.y * limits<T>::max()),
  112. (T) (c.z * limits<T>::max()));
  113. }
  114. else
  115. {
  116. Vec3<double> v = Vec3<double>(rgb.x, rgb.y, rgb.z);
  117. Vec3<double> c = rgb2hsv_d(v);
  118. return Vec3<T>((T) c.x, (T) c.y, (T) c.z);
  119. }
  120. }
  121. template<class T>
  122. Color4<T>
  123. rgb2hsv(const Color4<T> &rgb)
  124. {
  125. if ( limits<T>::isIntegral() )
  126. {
  127. Color4<double> v = Color4<double>(rgb.r / float(limits<T>::max()),
  128. rgb.g / float(limits<T>::max()),
  129. rgb.b / float(limits<T>::max()),
  130. rgb.a / float(limits<T>::max()));
  131. Color4<double> c = rgb2hsv_d(v);
  132. return Color4<T>((T) (c.r * limits<T>::max()),
  133. (T) (c.g * limits<T>::max()),
  134. (T) (c.b * limits<T>::max()),
  135. (T) (c.a * limits<T>::max()));
  136. }
  137. else
  138. {
  139. Color4<double> v = Color4<double>(rgb.r, rgb.g, rgb.b, rgb.a);
  140. Color4<double> c = rgb2hsv_d(v);
  141. return Color4<T>((T) c.r, (T) c.g, (T) c.b, (T) c.a);
  142. }
  143. }
  144. template <class T>
  145. PackedColor
  146. rgb2packed(const Vec3<T> &c)
  147. {
  148. if ( limits<T>::isIntegral() )
  149. {
  150. float x = c.x / float(limits<T>::max());
  151. float y = c.y / float(limits<T>::max());
  152. float z = c.z / float(limits<T>::max());
  153. return rgb2packed( V3f(x,y,z) );
  154. }
  155. else
  156. {
  157. return ( (PackedColor) (c.x * 255) |
  158. (((PackedColor) (c.y * 255)) << 8) |
  159. (((PackedColor) (c.z * 255)) << 16) | 0xFF000000 );
  160. }
  161. }
  162. template <class T>
  163. PackedColor
  164. rgb2packed(const Color4<T> &c)
  165. {
  166. if ( limits<T>::isIntegral() )
  167. {
  168. float r = c.r / float(limits<T>::max());
  169. float g = c.g / float(limits<T>::max());
  170. float b = c.b / float(limits<T>::max());
  171. float a = c.a / float(limits<T>::max());
  172. return rgb2packed( C4f(r,g,b,a) );
  173. }
  174. else
  175. {
  176. return ( (PackedColor) (c.r * 255) |
  177. (((PackedColor) (c.g * 255)) << 8) |
  178. (((PackedColor) (c.b * 255)) << 16) |
  179. (((PackedColor) (c.a * 255)) << 24));
  180. }
  181. }
  182. //
  183. // This guy can't return the result because the template
  184. // parameter would not be in the function signiture. So instead,
  185. // its passed in as an argument.
  186. //
  187. template <class T>
  188. void
  189. packed2rgb(PackedColor packed, Vec3<T> &out)
  190. {
  191. if ( limits<T>::isIntegral() )
  192. {
  193. T f = limits<T>::max() / ((PackedColor)0xFF);
  194. out.x = (packed & 0xFF) * f;
  195. out.y = ((packed & 0xFF00) >> 8) * f;
  196. out.z = ((packed & 0xFF0000) >> 16) * f;
  197. }
  198. else
  199. {
  200. T f = T(1) / T(255);
  201. out.x = (packed & 0xFF) * f;
  202. out.y = ((packed & 0xFF00) >> 8) * f;
  203. out.z = ((packed & 0xFF0000) >> 16) * f;
  204. }
  205. }
  206. template <class T>
  207. void
  208. packed2rgb(PackedColor packed, Color4<T> &out)
  209. {
  210. if ( limits<T>::isIntegral() )
  211. {
  212. T f = limits<T>::max() / ((PackedColor)0xFF);
  213. out.r = (packed & 0xFF) * f;
  214. out.g = ((packed & 0xFF00) >> 8) * f;
  215. out.b = ((packed & 0xFF0000) >> 16) * f;
  216. out.a = ((packed & 0xFF000000) >> 24) * f;
  217. }
  218. else
  219. {
  220. T f = T(1) / T(255);
  221. out.r = (packed & 0xFF) * f;
  222. out.g = ((packed & 0xFF00) >> 8) * f;
  223. out.b = ((packed & 0xFF0000) >> 16) * f;
  224. out.a = ((packed & 0xFF000000) >> 24) * f;
  225. }
  226. }
  227. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  228. #endif // INCLUDED_IMATHCOLORALGO_H