VisionCpp  0.0.1
OP_HSVToRGB.hpp
Go to the documentation of this file.
1 // This file is part of VisionCpp, a lightweight C++ template library
2 // for computer vision and image processing.
3 //
4 // Copyright (C) 2016 Codeplay Software Limited. All Rights Reserved.
5 //
6 // Contact: visioncpp@codeplay.com
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 
20 /// \file OP_HSVToRGB.hpp
21 /// \brief it converts HSV pixel to RGB pixel
22 
23 namespace visioncpp {
24 /// \brief functor converts HSV(H:[0.0f, 360.f], S:[0.0f, 1.0f] V: [0.0f, 1.0f])
25 /// to color RGB(R:[0.0f, 1.0f], G:[0.0f, 1.0f], B:[0.0f, 1.0f])
26 struct OP_HSVToRGB {
27  /// \param inHSV
28  /// \return RGB
30  float fR = 0.0f, fG = 0.0f, fB = 0.0f;
31  float fH = inHSV[0]; // H component
32  float fS = inHSV[1]; // S component
33  float fV = inHSV[2]; // V component
34 
35  // Convert from HSV to RGB, using float ranges 0.0 to 1.0
36  int iI = 0;
37  float fI = 0.0f, fF = 0.0f, p = 0.0f, q = 0.0f, t = 0.0f;
38 
39  if (fS == 0.0f) {
40  // achromatic (grey)
41  fR = fG = fB = fV;
42  } else {
43  // If Hue == 1.0, then wrap it around the circle to 0.0
44  if (fH >= 1.0f) fH = 0.0f;
45 
46  fH *= 6.0f; // sector 0 to 5
47  fI = cl::sycl::floor(fH); // integer part of h (0,1,2,3,4,5 or 6)
48  iI = static_cast<int>(fH); // " " " "
49  fF = fH - fI; // factorial part of h (0 to 1)
50 
51  p = fV * (1.0f - fS);
52  q = fV * (1.0f - fS * fF);
53  t = fV * (1.0f - fS * (1.0f - fF));
54 
55  switch (iI) {
56  case 0:
57  fR = fV;
58  fG = t;
59  fB = p;
60  break;
61  case 1:
62  fR = q;
63  fG = fV;
64  fB = p;
65  break;
66  case 2:
67  fR = p;
68  fG = fV;
69  fB = t;
70  break;
71  case 3:
72  fR = p;
73  fG = q;
74  fB = fV;
75  break;
76  case 4:
77  fR = t;
78  fG = p;
79  fB = fV;
80  break;
81  default: // case 5 (or 6):
82  fR = fV;
83  fG = p;
84  fB = q;
85  break;
86  }
87  }
88 
89  return visioncpp::pixel::F32C3(fR, fG, fB);
90  }
91 };
92 }
Storage< float, 3 > F32C3
Definition: pixel.hpp:116
VisionCpp namespace.
Definition: sycl/device.hpp:24
functor converts HSV(H:[0.0f, 360.f], S:[0.0f, 1.0f] V: [0.0f, 1.0f]) to color RGB(R:[0....
Definition: OP_HSVToRGB.hpp:26
visioncpp::pixel::F32C3 operator()(visioncpp::pixel::F32C3 inHSV)
Definition: OP_HSVToRGB.hpp:29