ImathColorAlgo.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. //----------------------------------------------------------------------------
  35. //
  36. // Implementation of non-template items declared in ImathColorAlgo.h
  37. //
  38. //----------------------------------------------------------------------------
  39. #include "ImathColorAlgo.h"
  40. IMATH_INTERNAL_NAMESPACE_SOURCE_ENTER
  41. Vec3<double>
  42. hsv2rgb_d(const Vec3<double> &hsv)
  43. {
  44. double hue = hsv.x;
  45. double sat = hsv.y;
  46. double val = hsv.z;
  47. double x = 0.0, y = 0.0, z = 0.0;
  48. if (hue == 1) hue = 0;
  49. else hue *= 6;
  50. int i = int(Math<double>::floor(hue));
  51. double f = hue-i;
  52. double p = val*(1-sat);
  53. double q = val*(1-(sat*f));
  54. double t = val*(1-(sat*(1-f)));
  55. switch (i)
  56. {
  57. case 0: x = val; y = t; z = p; break;
  58. case 1: x = q; y = val; z = p; break;
  59. case 2: x = p; y = val; z = t; break;
  60. case 3: x = p; y = q; z = val; break;
  61. case 4: x = t; y = p; z = val; break;
  62. case 5: x = val; y = p; z = q; break;
  63. }
  64. return Vec3<double>(x,y,z);
  65. }
  66. Color4<double>
  67. hsv2rgb_d(const Color4<double> &hsv)
  68. {
  69. double hue = hsv.r;
  70. double sat = hsv.g;
  71. double val = hsv.b;
  72. double r = 0.0, g = 0.0, b = 0.0;
  73. if (hue == 1) hue = 0;
  74. else hue *= 6;
  75. int i = int(Math<double>::floor(hue));
  76. double f = hue-i;
  77. double p = val*(1-sat);
  78. double q = val*(1-(sat*f));
  79. double t = val*(1-(sat*(1-f)));
  80. switch (i)
  81. {
  82. case 0: r = val; g = t; b = p; break;
  83. case 1: r = q; g = val; b = p; break;
  84. case 2: r = p; g = val; b = t; break;
  85. case 3: r = p; g = q; b = val; break;
  86. case 4: r = t; g = p; b = val; break;
  87. case 5: r = val; g = p; b = q; break;
  88. }
  89. return Color4<double>(r,g,b,hsv.a);
  90. }
  91. Vec3<double>
  92. rgb2hsv_d(const Vec3<double> &c)
  93. {
  94. const double &x = c.x;
  95. const double &y = c.y;
  96. const double &z = c.z;
  97. double max = (x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z);
  98. double min = (x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z);
  99. double range = max - min;
  100. double val = max;
  101. double sat = 0;
  102. double hue = 0;
  103. if (max != 0) sat = range/max;
  104. if (sat != 0)
  105. {
  106. double h;
  107. if (x == max) h = (y - z) / range;
  108. else if (y == max) h = 2 + (z - x) / range;
  109. else h = 4 + (x - y) / range;
  110. hue = h/6.;
  111. if (hue < 0.)
  112. hue += 1.0;
  113. }
  114. return Vec3<double>(hue,sat,val);
  115. }
  116. Color4<double>
  117. rgb2hsv_d(const Color4<double> &c)
  118. {
  119. const double &r = c.r;
  120. const double &g = c.g;
  121. const double &b = c.b;
  122. double max = (r > g) ? ((r > b) ? r : b) : ((g > b) ? g : b);
  123. double min = (r < g) ? ((r < b) ? r : b) : ((g < b) ? g : b);
  124. double range = max - min;
  125. double val = max;
  126. double sat = 0;
  127. double hue = 0;
  128. if (max != 0) sat = range/max;
  129. if (sat != 0)
  130. {
  131. double h;
  132. if (r == max) h = (g - b) / range;
  133. else if (g == max) h = 2 + (b - r) / range;
  134. else h = 4 + (r - g) / range;
  135. hue = h/6.;
  136. if (hue < 0.)
  137. hue += 1.0;
  138. }
  139. return Color4<double>(hue,sat,val,c.a);
  140. }
  141. IMATH_INTERNAL_NAMESPACE_SOURCE_EXIT