hsv_to_rgb.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "hsv_to_rgb.h"
  9. #include <cmath>
  10. template <typename T>
  11. IGL_INLINE void igl::hsv_to_rgb(const T * hsv, T * rgb)
  12. {
  13. igl::hsv_to_rgb(
  14. hsv[0],hsv[1],hsv[2],
  15. rgb[0],rgb[1],rgb[2]);
  16. }
  17. template <typename T>
  18. IGL_INLINE void igl::hsv_to_rgb(
  19. const T & h, const T & s, const T & v,
  20. T & r, T & g, T & b)
  21. {
  22. // From medit
  23. double f,p,q,t,hh;
  24. int i;
  25. hh = ((int)h % 360) / 60.;
  26. i = (int)floor(hh); /* largest int <= h */
  27. f = hh - i; /* fractional part of h */
  28. p = v * (1.0 - s);
  29. q = v * (1.0 - (s * f));
  30. t = v * (1.0 - (s * (1.0 - f)));
  31. switch(i) {
  32. case 0: r = v; g = t; b = p; break;
  33. case 1: r = q; g = v; b = p; break;
  34. case 2: r = p; g = v; b = t; break;
  35. case 3: r = p; g = q; b = v; break;
  36. case 4: r = t; g = p; b = v; break;
  37. case 5: r = v; g = p; b = q; break;
  38. }
  39. }