1///////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2004-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_IMATHCOLOR_H
38#define INCLUDED_IMATHCOLOR_H
39
40//----------------------------------------------------
41//
42// A three and four component color class template.
43//
44//----------------------------------------------------
45
46#include "ImathVec.h"
47#include "ImathNamespace.h"
48#include "half.h"
49
50IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
51
52
53template <class T>
54class Color3: public Vec3 <T>
55{
56 public:
57
58 //-------------
59 // Constructors
60 //-------------
61
62 Color3 (); // no initialization
63 explicit Color3 (T a); // (a a a)
64 Color3 (T a, T b, T c); // (a b c)
65
66
67 //---------------------------------
68 // Copy constructors and assignment
69 //---------------------------------
70
71 Color3 (const Color3 &c);
72 template <class S> Color3 (const Vec3<S> &v);
73
74 const Color3 & operator = (const Color3 &c);
75
76
77 //------------------------
78 // Component-wise addition
79 //------------------------
80
81 const Color3 & operator += (const Color3 &c);
82 Color3 operator + (const Color3 &c) const;
83
84
85 //---------------------------
86 // Component-wise subtraction
87 //---------------------------
88
89 const Color3 & operator -= (const Color3 &c);
90 Color3 operator - (const Color3 &c) const;
91
92
93 //------------------------------------
94 // Component-wise multiplication by -1
95 //------------------------------------
96
97 Color3 operator - () const;
98 const Color3 & negate ();
99
100
101 //------------------------------
102 // Component-wise multiplication
103 //------------------------------
104
105 const Color3 & operator *= (const Color3 &c);
106 const Color3 & operator *= (T a);
107 Color3 operator * (const Color3 &c) const;
108 Color3 operator * (T a) const;
109
110
111 //------------------------
112 // Component-wise division
113 //------------------------
114
115 const Color3 & operator /= (const Color3 &c);
116 const Color3 & operator /= (T a);
117 Color3 operator / (const Color3 &c) const;
118 Color3 operator / (T a) const;
119};
120
121template <class T> class Color4
122{
123 public:
124
125 //-------------------
126 // Access to elements
127 //-------------------
128
129 T r, g, b, a;
130
131 T & operator [] (int i);
132 const T & operator [] (int i) const;
133
134
135 //-------------
136 // Constructors
137 //-------------
138
139 Color4 (); // no initialization
140 explicit Color4 (T a); // (a a a a)
141 Color4 (T a, T b, T c, T d); // (a b c d)
142
143
144 //---------------------------------
145 // Copy constructors and assignment
146 //---------------------------------
147
148 Color4 (const Color4 &v);
149 template <class S> Color4 (const Color4<S> &v);
150
151 const Color4 & operator = (const Color4 &v);
152
153
154 //----------------------
155 // Compatibility with Sb
156 //----------------------
157
158 template <class S>
159 void setValue (S a, S b, S c, S d);
160
161 template <class S>
162 void setValue (const Color4<S> &v);
163
164 template <class S>
165 void getValue (S &a, S &b, S &c, S &d) const;
166
167 template <class S>
168 void getValue (Color4<S> &v) const;
169
170 T * getValue();
171 const T * getValue() const;
172
173
174 //---------
175 // Equality
176 //---------
177
178 template <class S>
179 bool operator == (const Color4<S> &v) const;
180
181 template <class S>
182 bool operator != (const Color4<S> &v) const;
183
184
185 //------------------------
186 // Component-wise addition
187 //------------------------
188
189 const Color4 & operator += (const Color4 &v);
190 Color4 operator + (const Color4 &v) const;
191
192
193 //---------------------------
194 // Component-wise subtraction
195 //---------------------------
196
197 const Color4 & operator -= (const Color4 &v);
198 Color4 operator - (const Color4 &v) const;
199
200
201 //------------------------------------
202 // Component-wise multiplication by -1
203 //------------------------------------
204
205 Color4 operator - () const;
206 const Color4 & negate ();
207
208
209 //------------------------------
210 // Component-wise multiplication
211 //------------------------------
212
213 const Color4 & operator *= (const Color4 &v);
214 const Color4 & operator *= (T a);
215 Color4 operator * (const Color4 &v) const;
216 Color4 operator * (T a) const;
217
218
219 //------------------------
220 // Component-wise division
221 //------------------------
222
223 const Color4 & operator /= (const Color4 &v);
224 const Color4 & operator /= (T a);
225 Color4 operator / (const Color4 &v) const;
226 Color4 operator / (T a) const;
227
228
229 //----------------------------------------------------------
230 // Number of dimensions, i.e. number of elements in a Color4
231 //----------------------------------------------------------
232
233 static unsigned int dimensions() {return 4;}
234
235
236 //-------------------------------------------------
237 // Limitations of type T (see also class limits<T>)
238 //-------------------------------------------------
239
240 static T baseTypeMin() {return limits<T>::min();}
241 static T baseTypeMax() {return limits<T>::max();}
242 static T baseTypeSmallest() {return limits<T>::smallest();}
243 static T baseTypeEpsilon() {return limits<T>::epsilon();}
244
245
246 //--------------------------------------------------------------
247 // Base type -- in templates, which accept a parameter, V, which
248 // could be a Color4<T>, you can refer to T as
249 // V::BaseType
250 //--------------------------------------------------------------
251
252 typedef T BaseType;
253};
254
255//--------------
256// Stream output
257//--------------
258
259template <class T>
260std::ostream & operator << (std::ostream &s, const Color4<T> &v);
261
262//----------------------------------------------------
263// Reverse multiplication: S * Color4<T>
264//----------------------------------------------------
265
266template <class S, class T> Color4<T> operator * (S a, const Color4<T> &v);
267
268//-------------------------
269// Typedefs for convenience
270//-------------------------
271
272typedef Color3<float> Color3f;
273typedef Color3<half> Color3h;
274typedef Color3<unsigned char> Color3c;
275typedef Color3<half> C3h;
276typedef Color3<float> C3f;
277typedef Color3<unsigned char> C3c;
278typedef Color4<float> Color4f;
279typedef Color4<half> Color4h;
280typedef Color4<unsigned char> Color4c;
281typedef Color4<float> C4f;
282typedef Color4<half> C4h;
283typedef Color4<unsigned char> C4c;
284typedef unsigned int PackedColor;
285
286
287//-------------------------
288// Implementation of Color3
289//-------------------------
290
291template <class T>
292inline
293Color3<T>::Color3 (): Vec3 <T> ()
294{
295 // empty
296}
297
298template <class T>
299inline
300Color3<T>::Color3 (T a): Vec3 <T> (a)
301{
302 // empty
303}
304
305template <class T>
306inline
307Color3<T>::Color3 (T a, T b, T c): Vec3 <T> (a, b, c)
308{
309 // empty
310}
311
312template <class T>
313inline
314Color3<T>::Color3 (const Color3 &c): Vec3 <T> (c)
315{
316 // empty
317}
318
319template <class T>
320template <class S>
321inline
322Color3<T>::Color3 (const Vec3<S> &v): Vec3 <T> (v)
323{
324 //empty
325}
326
327template <class T>
328inline const Color3<T> &
329Color3<T>::operator = (const Color3 &c)
330{
331 *((Vec3<T> *) this) = c;
332 return *this;
333}
334
335template <class T>
336inline const Color3<T> &
337Color3<T>::operator += (const Color3 &c)
338{
339 *((Vec3<T> *) this) += c;
340 return *this;
341}
342
343template <class T>
344inline Color3<T>
345Color3<T>::operator + (const Color3 &c) const
346{
347 return Color3 (*(Vec3<T> *)this + (const Vec3<T> &)c);
348}
349
350template <class T>
351inline const Color3<T> &
352Color3<T>::operator -= (const Color3 &c)
353{
354 *((Vec3<T> *) this) -= c;
355 return *this;
356}
357
358template <class T>
359inline Color3<T>
360Color3<T>::operator - (const Color3 &c) const
361{
362 return Color3 (*(Vec3<T> *)this - (const Vec3<T> &)c);
363}
364
365template <class T>
366inline Color3<T>
367Color3<T>::operator - () const
368{
369 return Color3 (-(*(Vec3<T> *)this));
370}
371
372template <class T>
373inline const Color3<T> &
374Color3<T>::negate ()
375{
376 ((Vec3<T> *) this)->negate();
377 return *this;
378}
379
380template <class T>
381inline const Color3<T> &
382Color3<T>::operator *= (const Color3 &c)
383{
384 *((Vec3<T> *) this) *= c;
385 return *this;
386}
387
388template <class T>
389inline const Color3<T> &
390Color3<T>::operator *= (T a)
391{
392 *((Vec3<T> *) this) *= a;
393 return *this;
394}
395
396template <class T>
397inline Color3<T>
398Color3<T>::operator * (const Color3 &c) const
399{
400 return Color3 (*(Vec3<T> *)this * (const Vec3<T> &)c);
401}
402
403template <class T>
404inline Color3<T>
405Color3<T>::operator * (T a) const
406{
407 return Color3 (*(Vec3<T> *)this * a);
408}
409
410template <class T>
411inline const Color3<T> &
412Color3<T>::operator /= (const Color3 &c)
413{
414 *((Vec3<T> *) this) /= c;
415 return *this;
416}
417
418template <class T>
419inline const Color3<T> &
420Color3<T>::operator /= (T a)
421{
422 *((Vec3<T> *) this) /= a;
423 return *this;
424}
425
426template <class T>
427inline Color3<T>
428Color3<T>::operator / (const Color3 &c) const
429{
430 return Color3 (*(Vec3<T> *)this / (const Vec3<T> &)c);
431}
432
433template <class T>
434inline Color3<T>
435Color3<T>::operator / (T a) const
436{
437 return Color3 (*(Vec3<T> *)this / a);
438}
439
440//-----------------------
441// Implementation of Color4
442//-----------------------
443
444template <class T>
445inline T &
446Color4<T>::operator [] (int i)
447{
448 return (&r)[i];
449}
450
451template <class T>
452inline const T &
453Color4<T>::operator [] (int i) const
454{
455 return (&r)[i];
456}
457
458template <class T>
459inline
460Color4<T>::Color4 ()
461{
462 // empty
463}
464
465template <class T>
466inline
467Color4<T>::Color4 (T x)
468{
469 r = g = b = a = x;
470}
471
472template <class T>
473inline
474Color4<T>::Color4 (T x, T y, T z, T w)
475{
476 r = x;
477 g = y;
478 b = z;
479 a = w;
480}
481
482template <class T>
483inline
484Color4<T>::Color4 (const Color4 &v)
485{
486 r = v.r;
487 g = v.g;
488 b = v.b;
489 a = v.a;
490}
491
492template <class T>
493template <class S>
494inline
495Color4<T>::Color4 (const Color4<S> &v)
496{
497 r = T (v.r);
498 g = T (v.g);
499 b = T (v.b);
500 a = T (v.a);
501}
502
503template <class T>
504inline const Color4<T> &
505Color4<T>::operator = (const Color4 &v)
506{
507 r = v.r;
508 g = v.g;
509 b = v.b;
510 a = v.a;
511 return *this;
512}
513
514template <class T>
515template <class S>
516inline void
517Color4<T>::setValue (S x, S y, S z, S w)
518{
519 r = T (x);
520 g = T (y);
521 b = T (z);
522 a = T (w);
523}
524
525template <class T>
526template <class S>
527inline void
528Color4<T>::setValue (const Color4<S> &v)
529{
530 r = T (v.r);
531 g = T (v.g);
532 b = T (v.b);
533 a = T (v.a);
534}
535
536template <class T>
537template <class S>
538inline void
539Color4<T>::getValue (S &x, S &y, S &z, S &w) const
540{
541 x = S (r);
542 y = S (g);
543 z = S (b);
544 w = S (a);
545}
546
547template <class T>
548template <class S>
549inline void
550Color4<T>::getValue (Color4<S> &v) const
551{
552 v.r = S (r);
553 v.g = S (g);
554 v.b = S (b);
555 v.a = S (a);
556}
557
558template <class T>
559inline T *
560Color4<T>::getValue()
561{
562 return (T *) &r;
563}
564
565template <class T>
566inline const T *
567Color4<T>::getValue() const
568{
569 return (const T *) &r;
570}
571
572template <class T>
573template <class S>
574inline bool
575Color4<T>::operator == (const Color4<S> &v) const
576{
577 return r == v.r && g == v.g && b == v.b && a == v.a;
578}
579
580template <class T>
581template <class S>
582inline bool
583Color4<T>::operator != (const Color4<S> &v) const
584{
585 return r != v.r || g != v.g || b != v.b || a != v.a;
586}
587
588template <class T>
589inline const Color4<T> &
590Color4<T>::operator += (const Color4 &v)
591{
592 r += v.r;
593 g += v.g;
594 b += v.b;
595 a += v.a;
596 return *this;
597}
598
599template <class T>
600inline Color4<T>
601Color4<T>::operator + (const Color4 &v) const
602{
603 return Color4 (r + v.r, g + v.g, b + v.b, a + v.a);
604}
605
606template <class T>
607inline const Color4<T> &
608Color4<T>::operator -= (const Color4 &v)
609{
610 r -= v.r;
611 g -= v.g;
612 b -= v.b;
613 a -= v.a;
614 return *this;
615}
616
617template <class T>
618inline Color4<T>
619Color4<T>::operator - (const Color4 &v) const
620{
621 return Color4 (r - v.r, g - v.g, b - v.b, a - v.a);
622}
623
624template <class T>
625inline Color4<T>
626Color4<T>::operator - () const
627{
628 return Color4 (-r, -g, -b, -a);
629}
630
631template <class T>
632inline const Color4<T> &
633Color4<T>::negate ()
634{
635 r = -r;
636 g = -g;
637 b = -b;
638 a = -a;
639 return *this;
640}
641
642template <class T>
643inline const Color4<T> &
644Color4<T>::operator *= (const Color4 &v)
645{
646 r *= v.r;
647 g *= v.g;
648 b *= v.b;
649 a *= v.a;
650 return *this;
651}
652
653template <class T>
654inline const Color4<T> &
655Color4<T>::operator *= (T x)
656{
657 r *= x;
658 g *= x;
659 b *= x;
660 a *= x;
661 return *this;
662}
663
664template <class T>
665inline Color4<T>
666Color4<T>::operator * (const Color4 &v) const
667{
668 return Color4 (r * v.r, g * v.g, b * v.b, a * v.a);
669}
670
671template <class T>
672inline Color4<T>
673Color4<T>::operator * (T x) const
674{
675 return Color4 (r * x, g * x, b * x, a * x);
676}
677
678template <class T>
679inline const Color4<T> &
680Color4<T>::operator /= (const Color4 &v)
681{
682 r /= v.r;
683 g /= v.g;
684 b /= v.b;
685 a /= v.a;
686 return *this;
687}
688
689template <class T>
690inline const Color4<T> &
691Color4<T>::operator /= (T x)
692{
693 r /= x;
694 g /= x;
695 b /= x;
696 a /= x;
697 return *this;
698}
699
700template <class T>
701inline Color4<T>
702Color4<T>::operator / (const Color4 &v) const
703{
704 return Color4 (r / v.r, g / v.g, b / v.b, a / v.a);
705}
706
707template <class T>
708inline Color4<T>
709Color4<T>::operator / (T x) const
710{
711 return Color4 (r / x, g / x, b / x, a / x);
712}
713
714
715template <class T>
716std::ostream &
717operator << (std::ostream &s, const Color4<T> &v)
718{
719 return s << '(' << v.r << ' ' << v.g << ' ' << v.b << ' ' << v.a << ')';
720}
721
722//-----------------------------------------
723// Implementation of reverse multiplication
724//-----------------------------------------
725
726template <class S, class T>
727inline Color4<T>
728operator * (S x, const Color4<T> &v)
729{
730 return Color4<T> (x * v.r, x * v.g, x * v.b, x * v.a);
731}
732
733
734IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
735
736#endif // INCLUDED_IMATHCOLOR_H
737