1 ///////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2004, 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_IMATHCOLOR_H
38 #define INCLUDED_IMATHCOLOR_H
40 //----------------------------------------------------
42 // A three and four component color class template.
44 //----------------------------------------------------
53 class Color3: public Vec3 <T>
61 Color3 (); // no initialization
62 explicit Color3 (T a); // (a a a)
63 Color3 (T a, T b, T c); // (a b c)
66 //---------------------------------
67 // Copy constructors and assignment
68 //---------------------------------
70 Color3 (const Color3 &c);
71 template <class S> Color3 (const Vec3<S> &v);
73 const Color3 & operator = (const Color3 &c);
76 //------------------------
77 // Component-wise addition
78 //------------------------
80 const Color3 & operator += (const Color3 &c);
81 Color3 operator + (const Color3 &c) const;
84 //---------------------------
85 // Component-wise subtraction
86 //---------------------------
88 const Color3 & operator -= (const Color3 &c);
89 Color3 operator - (const Color3 &c) const;
92 //------------------------------------
93 // Component-wise multiplication by -1
94 //------------------------------------
96 Color3 operator - () const;
97 const Color3 & negate ();
100 //------------------------------
101 // Component-wise multiplication
102 //------------------------------
104 const Color3 & operator *= (const Color3 &c);
105 const Color3 & operator *= (T a);
106 Color3 operator * (const Color3 &c) const;
107 Color3 operator * (T a) const;
110 //------------------------
111 // Component-wise division
112 //------------------------
114 const Color3 & operator /= (const Color3 &c);
115 const Color3 & operator /= (T a);
116 Color3 operator / (const Color3 &c) const;
117 Color3 operator / (T a) const;
120 template <class T> class Color4
124 //-------------------
125 // Access to elements
126 //-------------------
130 T & operator [] (int i);
131 const T & operator [] (int i) const;
138 Color4 (); // no initialization
139 explicit Color4 (T a); // (a a a a)
140 Color4 (T a, T b, T c, T d); // (a b c d)
143 //---------------------------------
144 // Copy constructors and assignment
145 //---------------------------------
147 Color4 (const Color4 &v);
148 template <class S> Color4 (const Color4<S> &v);
150 const Color4 & operator = (const Color4 &v);
153 //----------------------
154 // Compatibility with Sb
155 //----------------------
158 void setValue (S a, S b, S c, S d);
161 void setValue (const Color4<S> &v);
164 void getValue (S &a, S &b, S &c, S &d) const;
167 void getValue (Color4<S> &v) const;
170 const T * getValue() const;
178 bool operator == (const Color4<S> &v) const;
181 bool operator != (const Color4<S> &v) const;
184 //------------------------
185 // Component-wise addition
186 //------------------------
188 const Color4 & operator += (const Color4 &v);
189 Color4 operator + (const Color4 &v) const;
192 //---------------------------
193 // Component-wise subtraction
194 //---------------------------
196 const Color4 & operator -= (const Color4 &v);
197 Color4 operator - (const Color4 &v) const;
200 //------------------------------------
201 // Component-wise multiplication by -1
202 //------------------------------------
204 Color4 operator - () const;
205 const Color4 & negate ();
208 //------------------------------
209 // Component-wise multiplication
210 //------------------------------
212 const Color4 & operator *= (const Color4 &v);
213 const Color4 & operator *= (T a);
214 Color4 operator * (const Color4 &v) const;
215 Color4 operator * (T a) const;
218 //------------------------
219 // Component-wise division
220 //------------------------
222 const Color4 & operator /= (const Color4 &v);
223 const Color4 & operator /= (T a);
224 Color4 operator / (const Color4 &v) const;
225 Color4 operator / (T a) const;
228 //----------------------------------------------------------
229 // Number of dimensions, i.e. number of elements in a Color4
230 //----------------------------------------------------------
232 static unsigned int dimensions() {return 4;}
235 //-------------------------------------------------
236 // Limitations of type T (see also class limits<T>)
237 //-------------------------------------------------
239 static T baseTypeMin() {return limits<T>::min();}
240 static T baseTypeMax() {return limits<T>::max();}
241 static T baseTypeSmallest() {return limits<T>::smallest();}
242 static T baseTypeEpsilon() {return limits<T>::epsilon();}
245 //--------------------------------------------------------------
246 // Base type -- in templates, which accept a parameter, V, which
247 // could be a Color4<T>, you can refer to T as
249 //--------------------------------------------------------------
259 std::ostream & operator << (std::ostream &s, const Color4<T> &v);
261 //----------------------------------------------------
262 // Reverse multiplication: S * Color4<T>
263 //----------------------------------------------------
265 template <class S, class T> Color4<T> operator * (S a, const Color4<T> &v);
267 //-------------------------
268 // Typedefs for convenience
269 //-------------------------
271 typedef Color3<float> Color3f;
272 typedef Color3<half> Color3h;
273 typedef Color3<unsigned char> Color3c;
274 typedef Color3<half> C3h;
275 typedef Color3<float> C3f;
276 typedef Color3<unsigned char> C3c;
277 typedef Color4<float> Color4f;
278 typedef Color4<half> Color4h;
279 typedef Color4<unsigned char> Color4c;
280 typedef Color4<float> C4f;
281 typedef Color4<half> C4h;
282 typedef Color4<unsigned char> C4c;
283 typedef unsigned int PackedColor;
286 //-------------------------
287 // Implementation of Color3
288 //-------------------------
292 Color3<T>::Color3 (): Vec3 <T> ()
299 Color3<T>::Color3 (T a): Vec3 <T> (a)
306 Color3<T>::Color3 (T a, T b, T c): Vec3 <T> (a, b, c)
313 Color3<T>::Color3 (const Color3 &c): Vec3 <T> (c)
321 Color3<T>::Color3 (const Vec3<S> &v): Vec3 <T> (v)
327 inline const Color3<T> &
328 Color3<T>::operator = (const Color3 &c)
330 *((Vec3<T> *) this) = c;
335 inline const Color3<T> &
336 Color3<T>::operator += (const Color3 &c)
338 *((Vec3<T> *) this) += c;
344 Color3<T>::operator + (const Color3 &c) const
346 return Color3 (*(Vec3<T> *)this + (const Vec3<T> &)c);
350 inline const Color3<T> &
351 Color3<T>::operator -= (const Color3 &c)
353 *((Vec3<T> *) this) -= c;
359 Color3<T>::operator - (const Color3 &c) const
361 return Color3 (*(Vec3<T> *)this - (const Vec3<T> &)c);
366 Color3<T>::operator - () const
368 return Color3 (-(*(Vec3<T> *)this));
372 inline const Color3<T> &
375 ((Vec3<T> *) this)->negate();
380 inline const Color3<T> &
381 Color3<T>::operator *= (const Color3 &c)
383 *((Vec3<T> *) this) *= c;
388 inline const Color3<T> &
389 Color3<T>::operator *= (T a)
391 *((Vec3<T> *) this) *= a;
397 Color3<T>::operator * (const Color3 &c) const
399 return Color3 (*(Vec3<T> *)this * (const Vec3<T> &)c);
404 Color3<T>::operator * (T a) const
406 return Color3 (*(Vec3<T> *)this * a);
410 inline const Color3<T> &
411 Color3<T>::operator /= (const Color3 &c)
413 *((Vec3<T> *) this) /= c;
418 inline const Color3<T> &
419 Color3<T>::operator /= (T a)
421 *((Vec3<T> *) this) /= a;
427 Color3<T>::operator / (const Color3 &c) const
429 return Color3 (*(Vec3<T> *)this / (const Vec3<T> &)c);
434 Color3<T>::operator / (T a) const
436 return Color3 (*(Vec3<T> *)this / a);
439 //-----------------------
440 // Implementation of Color4
441 //-----------------------
445 Color4<T>::operator [] (int i)
452 Color4<T>::operator [] (int i) const
466 Color4<T>::Color4 (T x)
473 Color4<T>::Color4 (T x, T y, T z, T w)
483 Color4<T>::Color4 (const Color4 &v)
494 Color4<T>::Color4 (const Color4<S> &v)
503 inline const Color4<T> &
504 Color4<T>::operator = (const Color4 &v)
516 Color4<T>::setValue (S x, S y, S z, S w)
527 Color4<T>::setValue (const Color4<S> &v)
538 Color4<T>::getValue (S &x, S &y, S &z, S &w) const
549 Color4<T>::getValue (Color4<S> &v) const
559 Color4<T>::getValue()
566 Color4<T>::getValue() const
568 return (const T *) &r;
574 Color4<T>::operator == (const Color4<S> &v) const
576 return r == v.r && g == v.g && b == v.b && a == v.a;
582 Color4<T>::operator != (const Color4<S> &v) const
584 return r != v.r || g != v.g || b != v.b || a != v.a;
588 inline const Color4<T> &
589 Color4<T>::operator += (const Color4 &v)
600 Color4<T>::operator + (const Color4 &v) const
602 return Color4 (r + v.r, g + v.g, b + v.b, a + v.a);
606 inline const Color4<T> &
607 Color4<T>::operator -= (const Color4 &v)
618 Color4<T>::operator - (const Color4 &v) const
620 return Color4 (r - v.r, g - v.g, b - v.b, a - v.a);
625 Color4<T>::operator - () const
627 return Color4 (-r, -g, -b, -a);
631 inline const Color4<T> &
642 inline const Color4<T> &
643 Color4<T>::operator *= (const Color4 &v)
653 inline const Color4<T> &
654 Color4<T>::operator *= (T x)
665 Color4<T>::operator * (const Color4 &v) const
667 return Color4 (r * v.r, g * v.g, b * v.b, a * v.a);
672 Color4<T>::operator * (T x) const
674 return Color4 (r * x, g * x, b * x, a * x);
678 inline const Color4<T> &
679 Color4<T>::operator /= (const Color4 &v)
689 inline const Color4<T> &
690 Color4<T>::operator /= (T x)
701 Color4<T>::operator / (const Color4 &v) const
703 return Color4 (r / v.r, g / v.g, b / v.b, a / v.a);
708 Color4<T>::operator / (T x) const
710 return Color4 (r / x, g / x, b / x, a / x);
716 operator << (std::ostream &s, const Color4<T> &v)
718 return s << '(' << v.r << ' ' << v.g << ' ' << v.b << ' ' << v.a << ')';
721 //-----------------------------------------
722 // Implementation of reverse multiplication
723 //-----------------------------------------
725 template <class S, class T>
727 operator * (S x, const Color4<T> &v)
729 return Color4<T> (x * v.r, x * v.g, x * v.b, x * v.a);