1 ///////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
6 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
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
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.
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.
33 ///////////////////////////////////////////////////////////////////////////
37 #ifndef INCLUDED_IMATHCOLORALGO_H
38 #define INCLUDED_IMATHCOLORALGO_H
41 #include "ImathColor.h"
42 #include "ImathMath.h"
43 #include "ImathLimits.h"
49 // Non-templated helper routines for color conversion.
50 // These routines eliminate type warnings under g++.
53 Vec3<double> hsv2rgb_d(const Vec3<double> &hsv);
55 Color4<double> hsv2rgb_d(const Color4<double> &hsv);
58 Vec3<double> rgb2hsv_d(const Vec3<double> &rgb);
60 Color4<double> rgb2hsv_d(const Color4<double> &rgb);
64 // Color conversion functions and general color algorithms
66 // hsv2rgb(), rgb2hsv(), rgb2packed(), packed2rgb()
67 // see each funtion definition for details.
72 hsv2rgb(const Vec3<T> &hsv)
74 if ( limits<T>::isIntegral() )
76 Vec3<double> v = Vec3<double>(hsv.x / double(limits<T>::max()),
77 hsv.y / double(limits<T>::max()),
78 hsv.z / double(limits<T>::max()));
79 Vec3<double> c = hsv2rgb_d(v);
80 return Vec3<T>((T) (c.x * limits<T>::max()),
81 (T) (c.y * limits<T>::max()),
82 (T) (c.z * limits<T>::max()));
86 Vec3<double> v = Vec3<double>(hsv.x, hsv.y, hsv.z);
87 Vec3<double> c = hsv2rgb_d(v);
88 return Vec3<T>((T) c.x, (T) c.y, (T) c.z);
95 hsv2rgb(const Color4<T> &hsv)
97 if ( limits<T>::isIntegral() )
99 Color4<double> v = Color4<double>(hsv.r / float(limits<T>::max()),
100 hsv.g / float(limits<T>::max()),
101 hsv.b / float(limits<T>::max()),
102 hsv.a / float(limits<T>::max()));
103 Color4<double> c = hsv2rgb_d(v);
104 return Color4<T>((T) (c.r * limits<T>::max()),
105 (T) (c.g * limits<T>::max()),
106 (T) (c.b * limits<T>::max()),
107 (T) (c.a * limits<T>::max()));
111 Color4<double> v = Color4<double>(hsv.r, hsv.g, hsv.g, hsv.a);
112 Color4<double> c = hsv2rgb_d(v);
113 return Color4<T>((T) c.r, (T) c.g, (T) c.b, (T) c.a);
120 rgb2hsv(const Vec3<T> &rgb)
122 if ( limits<T>::isIntegral() )
124 Vec3<double> v = Vec3<double>(rgb.x / double(limits<T>::max()),
125 rgb.y / double(limits<T>::max()),
126 rgb.z / double(limits<T>::max()));
127 Vec3<double> c = rgb2hsv_d(v);
128 return Vec3<T>((T) (c.x * limits<T>::max()),
129 (T) (c.y * limits<T>::max()),
130 (T) (c.z * limits<T>::max()));
134 Vec3<double> v = Vec3<double>(rgb.x, rgb.y, rgb.z);
135 Vec3<double> c = rgb2hsv_d(v);
136 return Vec3<T>((T) c.x, (T) c.y, (T) c.z);
143 rgb2hsv(const Color4<T> &rgb)
145 if ( limits<T>::isIntegral() )
147 Color4<double> v = Color4<double>(rgb.r / float(limits<T>::max()),
148 rgb.g / float(limits<T>::max()),
149 rgb.b / float(limits<T>::max()),
150 rgb.a / float(limits<T>::max()));
151 Color4<double> c = rgb2hsv_d(v);
152 return Color4<T>((T) (c.r * limits<T>::max()),
153 (T) (c.g * limits<T>::max()),
154 (T) (c.b * limits<T>::max()),
155 (T) (c.a * limits<T>::max()));
159 Color4<double> v = Color4<double>(rgb.r, rgb.g, rgb.g, rgb.a);
160 Color4<double> c = rgb2hsv_d(v);
161 return Color4<T>((T) c.r, (T) c.g, (T) c.b, (T) c.a);
167 rgb2packed(const Vec3<T> &c)
169 if ( limits<T>::isIntegral() )
171 float x = c.x / float(limits<T>::max());
172 float y = c.y / float(limits<T>::max());
173 float z = c.z / float(limits<T>::max());
174 return rgb2packed( V3f(x,y,z) );
178 return ( (PackedColor) (c.x * 255) |
179 (((PackedColor) (c.y * 255)) << 8) |
180 (((PackedColor) (c.z * 255)) << 16) | 0xFF000000 );
186 rgb2packed(const Color4<T> &c)
188 if ( limits<T>::isIntegral() )
190 float r = c.r / float(limits<T>::max());
191 float g = c.g / float(limits<T>::max());
192 float b = c.b / float(limits<T>::max());
193 float a = c.a / float(limits<T>::max());
194 return rgb2packed( C4f(r,g,b,a) );
198 return ( (PackedColor) (c.r * 255) |
199 (((PackedColor) (c.g * 255)) << 8) |
200 (((PackedColor) (c.b * 255)) << 16) |
201 (((PackedColor) (c.a * 255)) << 24));
206 // This guy can't return the result because the template
207 // parameter would not be in the function signiture. So instead,
208 // its passed in as an argument.
213 packed2rgb(PackedColor packed, Vec3<T> &out)
215 if ( limits<T>::isIntegral() )
217 T f = limits<T>::max() / ((PackedColor)0xFF);
218 out.x = (packed & 0xFF) * f;
219 out.y = ((packed & 0xFF00) >> 8) * f;
220 out.z = ((packed & 0xFF0000) >> 16) * f;
225 out.x = (packed & 0xFF) * f;
226 out.y = ((packed & 0xFF00) >> 8) * f;
227 out.z = ((packed & 0xFF0000) >> 16) * f;
233 packed2rgb(PackedColor packed, Color4<T> &out)
235 if ( limits<T>::isIntegral() )
237 T f = limits<T>::max() / ((PackedColor)0xFF);
238 out.r = (packed & 0xFF) * f;
239 out.g = ((packed & 0xFF00) >> 8) * f;
240 out.b = ((packed & 0xFF0000) >> 16) * f;
241 out.a = ((packed & 0xFF000000) >> 24) * f;
246 out.r = (packed & 0xFF) * f;
247 out.g = ((packed & 0xFF00) >> 8) * f;
248 out.b = ((packed & 0xFF0000) >> 16) * f;
249 out.a = ((packed & 0xFF000000) >> 24) * f;