hsv_to_rgb.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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)std::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. }
  40. template <typename DerivedH, typename DerivedR>
  41. void igl::hsv_to_rgb(
  42. const Eigen::PlainObjectBase<DerivedH> & H,
  43. Eigen::PlainObjectBase<DerivedR> & R)
  44. {
  45. assert(H.cols() == 3);
  46. R.resize(H.rows(),H.cols());
  47. for(typename DerivedH::Index r = 0;r<H.rows();r++)
  48. {
  49. typename DerivedH::Scalar hsv[3];
  50. hsv[0] = H(r,0);
  51. hsv[1] = H(r,1);
  52. hsv[2] = H(r,2);
  53. typename DerivedR::Scalar rgb[] = {0,0,0};
  54. hsv_to_rgb(hsv,rgb);
  55. R(r,0) = rgb[0];
  56. R(r,1) = rgb[1];
  57. R(r,2) = rgb[2];
  58. }
  59. }
  60. #ifdef IGL_STATIC_LIBRARY
  61. template void igl::hsv_to_rgb<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  62. template void igl::hsv_to_rgb<Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&);
  63. template void igl::hsv_to_rgb<Eigen::Matrix<unsigned char, 64, 3, 1, 64, 3>, Eigen::Matrix<unsigned char, 64, 3, 1, 64, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<unsigned char, 64, 3, 1, 64, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned char, 64, 3, 1, 64, 3> >&);
  64. template void igl::hsv_to_rgb<Eigen::Matrix<float, 64, 3, 1, 64, 3>, Eigen::Matrix<float, 64, 3, 1, 64, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<float, 64, 3, 1, 64, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, 64, 3, 1, 64, 3> >&);
  65. #endif