Welcome to mirror list, hosted at ThFree Co, Russian Federation.

hsv_to_rgb.cpp « igl « libigl « src - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a1727ff50b637823a38ffce35bea46ef181083ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// This file is part of libigl, a simple c++ geometry processing library.
//
// Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "hsv_to_rgb.h"
#include <cmath>


template <typename T>
IGL_INLINE void igl::hsv_to_rgb(const T * hsv, T * rgb)
{
  igl::hsv_to_rgb(
      hsv[0],hsv[1],hsv[2],
      rgb[0],rgb[1],rgb[2]);
}

template <typename T>
IGL_INLINE void igl::hsv_to_rgb(
  const T & h, const T & s, const T & v,
  T & r, T & g, T & b)
{
  // From medit
  double f,p,q,t,hh;
  int    i;
  hh = ((int)h % 360) / 60.;
  i = (int)std::floor(hh);    /* largest int <= h     */
  f = hh - i;                    /* fractional part of h */
  p = v * (1.0 - s);
  q = v * (1.0 - (s * f));
  t = v * (1.0 - (s * (1.0 - f)));

  switch(i) {
  case 0: r = v; g = t; b = p; break;
  case 1: r = q; g = v; b = p; break;
  case 2: r = p; g = v; b = t; break;
  case 3: r = p; g = q; b = v; break;
  case 4: r = t; g = p; b = v; break;
  case 5: r = v; g = p; b = q; break;
  }
}

template <typename DerivedH, typename DerivedR>
void igl::hsv_to_rgb(
  const Eigen::PlainObjectBase<DerivedH> & H,
  Eigen::PlainObjectBase<DerivedR> & R)
{
  assert(H.cols() == 3);
  R.resizeLike(H);
  for(typename DerivedH::Index r = 0;r<H.rows();r++)
  {
    typename DerivedH::Scalar hsv[3];
    hsv[0] = H(r,0);
    hsv[1] = H(r,1);
    hsv[2] = H(r,2);
    typename DerivedR::Scalar rgb[] = {0,0,0};
    hsv_to_rgb(hsv,rgb);
    R(r,0) = rgb[0];
    R(r,1) = rgb[1];
    R(r,2) = rgb[2];
  }
}

#ifdef IGL_STATIC_LIBRARY
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> >&);
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> >&);
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> >&);
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> >&);
template void igl::hsv_to_rgb<double>(double const*, double*);
#endif