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 //
3 // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // * Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 
36 
37 #ifndef INCLUDED_IMATHCOLORALGO_H
38 #define INCLUDED_IMATHCOLORALGO_H
39 
40 
41 #include "ImathColor.h"
42 #include "ImathExport.h"
43 #include "ImathMath.h"
44 #include "ImathLimits.h"
45 #include "ImathNamespace.h"
46 
48 
49 
50 //
51 // Non-templated helper routines for color conversion.
52 // These routines eliminate type warnings under g++.
53 //
54 
56 
58 
60 
62 
63 
64 //
65 // Color conversion functions and general color algorithms
66 //
67 // hsv2rgb(), rgb2hsv(), rgb2packed(), packed2rgb()
68 // see each funtion definition for details.
69 //
70 
71 template<class T>
72 Vec3<T>
73 hsv2rgb(const Vec3<T> &hsv)
74 {
75  if ( limits<T>::isIntegral() )
76  {
77  Vec3<double> v = Vec3<double>(hsv.x / double(limits<T>::max()),
78  hsv.y / double(limits<T>::max()),
79  hsv.z / double(limits<T>::max()));
80  Vec3<double> c = hsv2rgb_d(v);
81  return Vec3<T>((T) (c.x * limits<T>::max()),
82  (T) (c.y * limits<T>::max()),
83  (T) (c.z * limits<T>::max()));
84  }
85  else
86  {
87  Vec3<double> v = Vec3<double>(hsv.x, hsv.y, hsv.z);
88  Vec3<double> c = hsv2rgb_d(v);
89  return Vec3<T>((T) c.x, (T) c.y, (T) c.z);
90  }
91 }
92 
93 
94 template<class T>
95 Color4<T>
96 hsv2rgb(const Color4<T> &hsv)
97 {
98  if ( limits<T>::isIntegral() )
99  {
101  hsv.g / float(limits<T>::max()),
102  hsv.b / float(limits<T>::max()),
103  hsv.a / float(limits<T>::max()));
104  Color4<double> c = hsv2rgb_d(v);
105  return Color4<T>((T) (c.r * limits<T>::max()),
106  (T) (c.g * limits<T>::max()),
107  (T) (c.b * limits<T>::max()),
108  (T) (c.a * limits<T>::max()));
109  }
110  else
111  {
112  Color4<double> v = Color4<double>(hsv.r, hsv.g, hsv.b, hsv.a);
113  Color4<double> c = hsv2rgb_d(v);
114  return Color4<T>((T) c.r, (T) c.g, (T) c.b, (T) c.a);
115  }
116 }
117 
118 
119 template<class T>
120 Vec3<T>
121 rgb2hsv(const Vec3<T> &rgb)
122 {
123  if ( limits<T>::isIntegral() )
124  {
125  Vec3<double> v = Vec3<double>(rgb.x / double(limits<T>::max()),
126  rgb.y / double(limits<T>::max()),
127  rgb.z / double(limits<T>::max()));
128  Vec3<double> c = rgb2hsv_d(v);
129  return Vec3<T>((T) (c.x * limits<T>::max()),
130  (T) (c.y * limits<T>::max()),
131  (T) (c.z * limits<T>::max()));
132  }
133  else
134  {
135  Vec3<double> v = Vec3<double>(rgb.x, rgb.y, rgb.z);
136  Vec3<double> c = rgb2hsv_d(v);
137  return Vec3<T>((T) c.x, (T) c.y, (T) c.z);
138  }
139 }
140 
141 
142 template<class T>
143 Color4<T>
144 rgb2hsv(const Color4<T> &rgb)
145 {
146  if ( limits<T>::isIntegral() )
147  {
149  rgb.g / float(limits<T>::max()),
150  rgb.b / float(limits<T>::max()),
151  rgb.a / float(limits<T>::max()));
152  Color4<double> c = rgb2hsv_d(v);
153  return Color4<T>((T) (c.r * limits<T>::max()),
154  (T) (c.g * limits<T>::max()),
155  (T) (c.b * limits<T>::max()),
156  (T) (c.a * limits<T>::max()));
157  }
158  else
159  {
160  Color4<double> v = Color4<double>(rgb.r, rgb.g, rgb.b, rgb.a);
161  Color4<double> c = rgb2hsv_d(v);
162  return Color4<T>((T) c.r, (T) c.g, (T) c.b, (T) c.a);
163  }
164 }
165 
166 template <class T>
169 {
170  if ( limits<T>::isIntegral() )
171  {
172  float x = c.x / float(limits<T>::max());
173  float y = c.y / float(limits<T>::max());
174  float z = c.z / float(limits<T>::max());
175  return rgb2packed( V3f(x,y,z) );
176  }
177  else
178  {
179  return ( (PackedColor) (c.x * 255) |
180  (((PackedColor) (c.y * 255)) << 8) |
181  (((PackedColor) (c.z * 255)) << 16) | 0xFF000000 );
182  }
183 }
184 
185 template <class T>
188 {
189  if ( limits<T>::isIntegral() )
190  {
191  float r = c.r / float(limits<T>::max());
192  float g = c.g / float(limits<T>::max());
193  float b = c.b / float(limits<T>::max());
194  float a = c.a / float(limits<T>::max());
195  return rgb2packed( C4f(r,g,b,a) );
196  }
197  else
198  {
199  return ( (PackedColor) (c.r * 255) |
200  (((PackedColor) (c.g * 255)) << 8) |
201  (((PackedColor) (c.b * 255)) << 16) |
202  (((PackedColor) (c.a * 255)) << 24));
203  }
204 }
205 
206 //
207 // This guy can't return the result because the template
208 // parameter would not be in the function signiture. So instead,
209 // its passed in as an argument.
210 //
211 
212 template <class T>
213 void
215 {
216  if ( limits<T>::isIntegral() )
217  {
218  T f = limits<T>::max() / ((PackedColor)0xFF);
219  out.x = (packed & 0xFF) * f;
220  out.y = ((packed & 0xFF00) >> 8) * f;
221  out.z = ((packed & 0xFF0000) >> 16) * f;
222  }
223  else
224  {
225  T f = T(1) / T(255);
226  out.x = (packed & 0xFF) * f;
227  out.y = ((packed & 0xFF00) >> 8) * f;
228  out.z = ((packed & 0xFF0000) >> 16) * f;
229  }
230 }
231 
232 template <class T>
233 void
235 {
236  if ( limits<T>::isIntegral() )
237  {
238  T f = limits<T>::max() / ((PackedColor)0xFF);
239  out.r = (packed & 0xFF) * f;
240  out.g = ((packed & 0xFF00) >> 8) * f;
241  out.b = ((packed & 0xFF0000) >> 16) * f;
242  out.a = ((packed & 0xFF000000) >> 24) * f;
243  }
244  else
245  {
246  T f = T(1) / T(255);
247  out.r = (packed & 0xFF) * f;
248  out.g = ((packed & 0xFF00) >> 8) * f;
249  out.b = ((packed & 0xFF0000) >> 16) * f;
250  out.a = ((packed & 0xFF000000) >> 24) * f;
251  }
252 }
253 
254 
256 
257 #endif // INCLUDED_IMATHCOLORALGO_H
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER IMATH_EXPORT Vec3< double > hsv2rgb_d(const Vec3< double > &hsv)
#define IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
Vec3< T > rgb2hsv(const Vec3< T > &rgb)
T z
Definition: ImathVec.h:275
#define IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
const GLdouble * v
Definition: glcorearb.h:836
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:847
GLboolean GLboolean g
Definition: glcorearb.h:1221
Color4< float > C4f
Definition: ImathColor.h:281
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
unsigned int PackedColor
Definition: ImathColor.h:284
GLint y
Definition: glcorearb.h:102
static T max()
GLfloat f
Definition: glcorearb.h:1925
T x
Definition: ImathVec.h:275
void packed2rgb(PackedColor packed, Vec3< T > &out)
#define IMATH_EXPORT
Definition: ImathExport.h:60
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
IMATH_EXPORT Vec3< double > rgb2hsv_d(const Vec3< double > &rgb)
Vec3< float > V3f
Definition: ImathVec.h:693
GLint GLenum GLint x
Definition: glcorearb.h:408
T y
Definition: ImathVec.h:275
Vec3< T > hsv2rgb(const Vec3< T > &hsv)
GLboolean r
Definition: glcorearb.h:1221
PackedColor rgb2packed(const Vec3< T > &c)