HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathColorAlgo.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5 
6 //
7 // Color conversion functions and general color algorithms
8 //
9 
10 #ifndef INCLUDED_IMATHCOLORALGO_H
11 #define INCLUDED_IMATHCOLORALGO_H
12 
13 #include "ImathExport.h"
14 #include "ImathNamespace.h"
15 
16 #include "ImathColor.h"
17 #include "ImathMath.h"
18 
19 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
20 
21 //
22 // Non-templated helper routines for color conversion.
23 // These routines eliminate type warnings under g++.
24 //
25 
26 ///
27 /// Convert 3-channel hsv to rgb. Non-templated helper routine.
29 
30 ///
31 /// Convert 4-channel hsv to rgb (with alpha). Non-templated helper routine.
34 
35 ///
36 /// Convert 3-channel rgb to hsv. Non-templated helper routine.
38 
39 ///
40 /// Convert 4-channel rgb to hsv. Non-templated helper routine.
43 
44 ///
45 /// Convert 3-channel hsv to rgb.
46 ///
47 
48 template <class T>
49 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3<T>
51 {
53  {
55  hsv.x / double (std::numeric_limits<T>::max ()),
56  hsv.y / double (std::numeric_limits<T>::max ()),
57  hsv.z / double (std::numeric_limits<T>::max ()));
58  Vec3<double> c = hsv2rgb_d (v);
59  return Vec3<T> (
60  (T) (c.x * std::numeric_limits<T>::max ()),
61  (T) (c.y * std::numeric_limits<T>::max ()),
62  (T) (c.z * std::numeric_limits<T>::max ()));
63  }
64  else
65  {
66  Vec3<double> v = Vec3<double> (hsv.x, hsv.y, hsv.z);
67  Vec3<double> c = hsv2rgb_d (v);
68  return Vec3<T> ((T) c.x, (T) c.y, (T) c.z);
69  }
70 }
71 
72 ///
73 /// Convert 4-channel hsv to rgb (with alpha).
74 ///
75 
76 template <class T>
77 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Color4<T>
79 {
81  {
86  hsv.a / float (std::numeric_limits<T>::max ()));
88  return Color4<T> (
89  (T) (c.r * std::numeric_limits<T>::max ()),
90  (T) (c.g * std::numeric_limits<T>::max ()),
91  (T) (c.b * std::numeric_limits<T>::max ()),
92  (T) (c.a * std::numeric_limits<T>::max ()));
93  }
94  else
95  {
96  Color4<double> v = Color4<double> (hsv.r, hsv.g, hsv.b, hsv.a);
98  return Color4<T> ((T) c.r, (T) c.g, (T) c.b, (T) c.a);
99  }
100 }
101 
102 ///
103 /// Convert 3-channel rgb to hsv.
104 ///
105 
106 template <class T>
107 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3<T>
109 {
111  {
113  rgb.x / double (std::numeric_limits<T>::max ()),
114  rgb.y / double (std::numeric_limits<T>::max ()),
115  rgb.z / double (std::numeric_limits<T>::max ()));
116  Vec3<double> c = rgb2hsv_d (v);
117  return Vec3<T> (
118  (T) (c.x * std::numeric_limits<T>::max ()),
119  (T) (c.y * std::numeric_limits<T>::max ()),
120  (T) (c.z * std::numeric_limits<T>::max ()));
121  }
122  else
123  {
124  Vec3<double> v = Vec3<double> (rgb.x, rgb.y, rgb.z);
125  Vec3<double> c = rgb2hsv_d (v);
126  return Vec3<T> ((T) c.x, (T) c.y, (T) c.z);
127  }
128 }
129 
130 ///
131 /// Convert 4-channel rgb to hsv (with alpha).
132 ///
133 
134 template <class T>
135 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Color4<T>
137 {
139  {
141  rgb.r / float (std::numeric_limits<T>::max ()),
142  rgb.g / float (std::numeric_limits<T>::max ()),
143  rgb.b / float (std::numeric_limits<T>::max ()),
144  rgb.a / float (std::numeric_limits<T>::max ()));
145  Color4<double> c = rgb2hsv_d (v);
146  return Color4<T> (
147  (T) (c.r * std::numeric_limits<T>::max ()),
148  (T) (c.g * std::numeric_limits<T>::max ()),
149  (T) (c.b * std::numeric_limits<T>::max ()),
150  (T) (c.a * std::numeric_limits<T>::max ()));
151  }
152  else
153  {
154  Color4<double> v = Color4<double> (rgb.r, rgb.g, rgb.b, rgb.a);
155  Color4<double> c = rgb2hsv_d (v);
156  return Color4<T> ((T) c.r, (T) c.g, (T) c.b, (T) c.a);
157  }
158 }
159 
160 ///
161 /// Convert 3-channel rgb to PackedColor
162 ///
163 
164 template <class T>
165 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 PackedColor
167 {
169  {
170  float x = c.x / float (std::numeric_limits<T>::max ());
171  float y = c.y / float (std::numeric_limits<T>::max ());
172  float z = c.z / float (std::numeric_limits<T>::max ());
173  return rgb2packed (V3f (x, y, z));
174  }
175  else
176  {
177  // clang-format off
178  return ( (PackedColor) (c.x * 255) |
179  (((PackedColor) (c.y * 255)) << 8) |
180  (((PackedColor) (c.z * 255)) << 16) | 0xFF000000 );
181  // clang-format on
182  }
183 }
184 
185 ///
186 /// Convert 4-channel rgb to PackedColor (with alpha)
187 ///
188 
189 template <class T>
190 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 PackedColor
192 {
194  {
195  float r = c.r / float (std::numeric_limits<T>::max ());
196  float g = c.g / float (std::numeric_limits<T>::max ());
197  float b = c.b / float (std::numeric_limits<T>::max ());
198  float a = c.a / float (std::numeric_limits<T>::max ());
199  return rgb2packed (C4f (r, g, b, a));
200  }
201  else
202  {
203  // clang-format off
204  return ( (PackedColor) (c.r * 255) |
205  (((PackedColor) (c.g * 255)) << 8) |
206  (((PackedColor) (c.b * 255)) << 16) |
207  (((PackedColor) (c.a * 255)) << 24));
208  // clang-format on
209  }
210 }
211 
212 ///
213 /// Convert PackedColor to 3-channel rgb. Return the result in the
214 /// `out` parameter.
215 ///
216 
217 template <class T>
218 IMATH_HOSTDEVICE void
220 {
222  {
223  T f = std::numeric_limits<T>::max () / ((PackedColor) 0xFF);
224  out.x = (packed & 0xFF) * f;
225  out.y = ((packed & 0xFF00) >> 8) * f;
226  out.z = ((packed & 0xFF0000) >> 16) * f;
227  }
228  else
229  {
230  T f = T (1) / T (255);
231  out.x = (packed & 0xFF) * f;
232  out.y = ((packed & 0xFF00) >> 8) * f;
233  out.z = ((packed & 0xFF0000) >> 16) * f;
234  }
235 }
236 
237 ///
238 /// Convert PackedColor to 4-channel rgb (with alpha). Return the
239 /// result in the `out` parameter.
240 ///
241 
242 template <class T>
243 IMATH_HOSTDEVICE void
245 {
247  {
248  T f = std::numeric_limits<T>::max () / ((PackedColor) 0xFF);
249  out.r = (packed & 0xFF) * f;
250  out.g = ((packed & 0xFF00) >> 8) * f;
251  out.b = ((packed & 0xFF0000) >> 16) * f;
252  out.a = ((packed & 0xFF000000) >> 24) * f;
253  }
254  else
255  {
256  T f = T (1) / T (255);
257  out.r = (packed & 0xFF) * f;
258  out.g = ((packed & 0xFF00) >> 8) * f;
259  out.b = ((packed & 0xFF0000) >> 16) * f;
260  out.a = ((packed & 0xFF000000) >> 24) * f;
261  }
262 }
263 
264 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
265 
266 #endif // INCLUDED_IMATHCOLORALGO_H
bool_constant< is_integral< T >::value &&!std::is_same< T, bool >::value &&!std::is_same< T, char >::value &&!std::is_same< T, wchar_t >::value > is_integer
Definition: format.h:824
T z
Definition: ImathVec.h:368
#define IMATH_NOEXCEPT
Definition: ImathConfig.h:78
Definition: ImathVec.h:40
const GLdouble * v
Definition: glcorearb.h:837
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
GLboolean GLboolean g
Definition: glcorearb.h:1222
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
unsigned int PackedColor
Packed 32-bit integer.
Definition: ImathColor.h:354
GLint y
Definition: glcorearb.h:103
#define IMATH_HOSTDEVICE
Definition: ImathConfig.h:108
IMATH_HOSTDEVICE void packed2rgb(PackedColor packed, Vec3< T > &out) IMATH_NOEXCEPT
GLfloat f
Definition: glcorearb.h:1926
T x
Definition: ImathVec.h:368
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER IMATH_EXPORT Vec3< double > hsv2rgb_d(const Vec3< double > &hsv) IMATH_NOEXCEPT
Convert 3-channel hsv to rgb. Non-templated helper routine.
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR_14 uint8_t packed(VULKAN_HPP_NAMESPACE::Format format)
IMATH_EXPORT Vec3< double > rgb2hsv_d(const Vec3< double > &rgb) IMATH_NOEXCEPT
Convert 3-channel rgb to hsv. Non-templated helper routine.
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLint GLenum GLint x
Definition: glcorearb.h:409
#define IMATH_EXPORT
Definition: ImathExport.h:47
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3< T > rgb2hsv(const Vec3< T > &rgb) IMATH_NOEXCEPT
IMATH_NAMESPACE::V2f IMATH_NAMESPACE::Box2i std::string this attribute is obsolete as of OpenEXR v3 float
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 PackedColor rgb2packed(const Vec3< T > &c) IMATH_NOEXCEPT
T y
Definition: ImathVec.h:368
GLboolean r
Definition: glcorearb.h:1222
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3< T > hsv2rgb(const Vec3< T > &hsv) IMATH_NOEXCEPT