Fermat
hsv.h
1 /*
2  * Copyright (c) 2010-2018, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
30 #include <cugar/linalg/vector.h>
31 
32 namespace cugar
33 {
34 
35 struct HSV
36 {
37  HSV() {}
38  HSV(float _h, float _s, float _v) : h(_h), s(_s), v(_v) {}
39  HSV(float3 _in) : h(_in.x), s(_in.y), v(_in.z) {}
40 
41  float h;
42  float s;
43  float v;
44 };
45 
46 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
47 float3 hsv_to_rgb(const HSV in)
48 {
49  float3 out;
50 
51  if (in.s <= 0.0)
52  {
53  out.x = in.v;
54  out.y = in.v;
55  out.z = in.v;
56  return out;
57  }
58  float hh = in.h;
59 
60  if (hh >= 360.0f)
61  hh = 0.0;
62 
63  hh /= 60.0f;
64 
65  long i = (long)hh;
66 
67  float ff = hh - i;
68  float p = in.v * (1.0f - in.s);
69  float q = in.v * (1.0f - (in.s * ff));
70  float t = in.v * (1.0f - (in.s * (1.0f - ff)));
71 
72  switch(i) {
73  case 0:
74  out.x = in.v;
75  out.y = t;
76  out.z = p;
77  break;
78  case 1:
79  out.x = q;
80  out.y = in.v;
81  out.z = p;
82  break;
83  case 2:
84  out.x = p;
85  out.y = in.v;
86  out.z = t;
87  break;
88  case 3:
89  out.x = p;
90  out.y = q;
91  out.z = in.v;
92  break;
93  case 4:
94  out.x = t;
95  out.y = p;
96  out.z = in.v;
97  break;
98  case 5:
99  default:
100  out.x = in.v;
101  out.y = p;
102  out.z = q;
103  break;
104  }
105  return out;
106 }
107 
108 } // namespace cugar
Definition: hsv.h:35
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38