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 
50 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
51 
52 
53 template <class T>
54 class 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 
121 template <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 
dimensions()233     static unsigned int	dimensions() {return 4;}
234 
235 
236     //-------------------------------------------------
237     // Limitations of type T (see also class limits<T>)
238     //-------------------------------------------------
239 
baseTypeMin()240     static T		baseTypeMin()		{return limits<T>::min();}
baseTypeMax()241     static T		baseTypeMax()		{return limits<T>::max();}
baseTypeSmallest()242     static T		baseTypeSmallest()	{return limits<T>::smallest();}
baseTypeEpsilon()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 
259 template <class T>
260 std::ostream &	operator << (std::ostream &s, const Color4<T> &v);
261 
262 //----------------------------------------------------
263 // Reverse multiplication: S * Color4<T>
264 //----------------------------------------------------
265 
266 template <class S, class T> Color4<T>	operator * (S a, const Color4<T> &v);
267 
268 //-------------------------
269 // Typedefs for convenience
270 //-------------------------
271 
272 typedef Color3<float>		Color3f;
273 typedef Color3<half>		Color3h;
274 typedef Color3<unsigned char>	Color3c;
275 typedef Color3<half>		C3h;
276 typedef Color3<float>		C3f;
277 typedef Color3<unsigned char>	C3c;
278 typedef Color4<float>		Color4f;
279 typedef Color4<half>		Color4h;
280 typedef Color4<unsigned char>	Color4c;
281 typedef Color4<float>		C4f;
282 typedef Color4<half>		C4h;
283 typedef Color4<unsigned char>	C4c;
284 typedef unsigned int		PackedColor;
285 
286 
287 //-------------------------
288 // Implementation of Color3
289 //-------------------------
290 
291 template <class T>
292 inline
Color3()293 Color3<T>::Color3 (): Vec3 <T> ()
294 {
295     // empty
296 }
297 
298 template <class T>
299 inline
Color3(T a)300 Color3<T>::Color3 (T a): Vec3 <T> (a)
301 {
302     // empty
303 }
304 
305 template <class T>
306 inline
Color3(T a,T b,T c)307 Color3<T>::Color3 (T a, T b, T c): Vec3 <T> (a, b, c)
308 {
309     // empty
310 }
311 
312 template <class T>
313 inline
Color3(const Color3 & c)314 Color3<T>::Color3 (const Color3 &c): Vec3 <T> (c)
315 {
316     // empty
317 }
318 
319 template <class T>
320 template <class S>
321 inline
Color3(const Vec3<S> & v)322 Color3<T>::Color3 (const Vec3<S> &v): Vec3 <T> (v)
323 {
324     //empty
325 }
326 
327 template <class T>
328 inline const Color3<T> &
329 Color3<T>::operator = (const Color3 &c)
330 {
331     *((Vec3<T> *) this) = c;
332     return *this;
333 }
334 
335 template <class T>
336 inline const Color3<T> &
337 Color3<T>::operator += (const Color3 &c)
338 {
339     *((Vec3<T> *) this) += c;
340     return *this;
341 }
342 
343 template <class T>
344 inline Color3<T>
345 Color3<T>::operator + (const Color3 &c) const
346 {
347     return Color3 (*(Vec3<T> *)this + (const Vec3<T> &)c);
348 }
349 
350 template <class T>
351 inline const Color3<T> &
352 Color3<T>::operator -= (const Color3 &c)
353 {
354     *((Vec3<T> *) this) -= c;
355     return *this;
356 }
357 
358 template <class T>
359 inline Color3<T>
360 Color3<T>::operator - (const Color3 &c) const
361 {
362     return Color3 (*(Vec3<T> *)this - (const Vec3<T> &)c);
363 }
364 
365 template <class T>
366 inline Color3<T>
367 Color3<T>::operator - () const
368 {
369     return Color3 (-(*(Vec3<T> *)this));
370 }
371 
372 template <class T>
373 inline const Color3<T> &
negate()374 Color3<T>::negate ()
375 {
376     ((Vec3<T> *) this)->negate();
377     return *this;
378 }
379 
380 template <class T>
381 inline const Color3<T> &
382 Color3<T>::operator *= (const Color3 &c)
383 {
384     *((Vec3<T> *) this) *= c;
385     return *this;
386 }
387 
388 template <class T>
389 inline const Color3<T> &
390 Color3<T>::operator *= (T a)
391 {
392     *((Vec3<T> *) this) *= a;
393     return *this;
394 }
395 
396 template <class T>
397 inline Color3<T>
398 Color3<T>::operator * (const Color3 &c) const
399 {
400     return Color3 (*(Vec3<T> *)this * (const Vec3<T> &)c);
401 }
402 
403 template <class T>
404 inline Color3<T>
405 Color3<T>::operator * (T a) const
406 {
407     return Color3 (*(Vec3<T> *)this * a);
408 }
409 
410 template <class T>
411 inline const Color3<T> &
412 Color3<T>::operator /= (const Color3 &c)
413 {
414     *((Vec3<T> *) this) /= c;
415     return *this;
416 }
417 
418 template <class T>
419 inline const Color3<T> &
420 Color3<T>::operator /= (T a)
421 {
422     *((Vec3<T> *) this) /= a;
423     return *this;
424 }
425 
426 template <class T>
427 inline Color3<T>
428 Color3<T>::operator / (const Color3 &c) const
429 {
430     return Color3 (*(Vec3<T> *)this / (const Vec3<T> &)c);
431 }
432 
433 template <class T>
434 inline Color3<T>
435 Color3<T>::operator / (T a) const
436 {
437     return Color3 (*(Vec3<T> *)this / a);
438 }
439 
440 //-----------------------
441 // Implementation of Color4
442 //-----------------------
443 
444 template <class T>
445 inline T &
446 Color4<T>::operator [] (int i)
447 {
448     return (&r)[i];
449 }
450 
451 template <class T>
452 inline const T &
453 Color4<T>::operator [] (int i) const
454 {
455     return (&r)[i];
456 }
457 
458 template <class T>
459 inline
Color4()460 Color4<T>::Color4 ()
461 {
462     // empty
463 }
464 
465 template <class T>
466 inline
Color4(T x)467 Color4<T>::Color4 (T x)
468 {
469     r = g = b = a = x;
470 }
471 
472 template <class T>
473 inline
Color4(T x,T y,T z,T w)474 Color4<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 
482 template <class T>
483 inline
Color4(const Color4 & v)484 Color4<T>::Color4 (const Color4 &v)
485 {
486     r = v.r;
487     g = v.g;
488     b = v.b;
489     a = v.a;
490 }
491 
492 template <class T>
493 template <class S>
494 inline
Color4(const Color4<S> & v)495 Color4<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 
503 template <class T>
504 inline const Color4<T> &
505 Color4<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 
514 template <class T>
515 template <class S>
516 inline void
setValue(S x,S y,S z,S w)517 Color4<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 
525 template <class T>
526 template <class S>
527 inline void
setValue(const Color4<S> & v)528 Color4<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 
536 template <class T>
537 template <class S>
538 inline void
getValue(S & x,S & y,S & z,S & w)539 Color4<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 
547 template <class T>
548 template <class S>
549 inline void
getValue(Color4<S> & v)550 Color4<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 
558 template <class T>
559 inline T *
getValue()560 Color4<T>::getValue()
561 {
562     return (T *) &r;
563 }
564 
565 template <class T>
566 inline const T *
getValue()567 Color4<T>::getValue() const
568 {
569     return (const T *) &r;
570 }
571 
572 template <class T>
573 template <class S>
574 inline bool
575 Color4<T>::operator == (const Color4<S> &v) const
576 {
577     return r == v.r && g == v.g && b == v.b && a == v.a;
578 }
579 
580 template <class T>
581 template <class S>
582 inline bool
583 Color4<T>::operator != (const Color4<S> &v) const
584 {
585     return r != v.r || g != v.g || b != v.b || a != v.a;
586 }
587 
588 template <class T>
589 inline const Color4<T> &
590 Color4<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 
599 template <class T>
600 inline Color4<T>
601 Color4<T>::operator + (const Color4 &v) const
602 {
603     return Color4 (r + v.r, g + v.g, b + v.b, a + v.a);
604 }
605 
606 template <class T>
607 inline const Color4<T> &
608 Color4<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 
617 template <class T>
618 inline Color4<T>
619 Color4<T>::operator - (const Color4 &v) const
620 {
621     return Color4 (r - v.r, g - v.g, b - v.b, a - v.a);
622 }
623 
624 template <class T>
625 inline Color4<T>
626 Color4<T>::operator - () const
627 {
628     return Color4 (-r, -g, -b, -a);
629 }
630 
631 template <class T>
632 inline const Color4<T> &
negate()633 Color4<T>::negate ()
634 {
635     r = -r;
636     g = -g;
637     b = -b;
638     a = -a;
639     return *this;
640 }
641 
642 template <class T>
643 inline const Color4<T> &
644 Color4<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 
653 template <class T>
654 inline const Color4<T> &
655 Color4<T>::operator *= (T x)
656 {
657     r *= x;
658     g *= x;
659     b *= x;
660     a *= x;
661     return *this;
662 }
663 
664 template <class T>
665 inline Color4<T>
666 Color4<T>::operator * (const Color4 &v) const
667 {
668     return Color4 (r * v.r, g * v.g, b * v.b, a * v.a);
669 }
670 
671 template <class T>
672 inline Color4<T>
673 Color4<T>::operator * (T x) const
674 {
675     return Color4 (r * x, g * x, b * x, a * x);
676 }
677 
678 template <class T>
679 inline const Color4<T> &
680 Color4<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 
689 template <class T>
690 inline const Color4<T> &
691 Color4<T>::operator /= (T x)
692 {
693     r /= x;
694     g /= x;
695     b /= x;
696     a /= x;
697     return *this;
698 }
699 
700 template <class T>
701 inline Color4<T>
702 Color4<T>::operator / (const Color4 &v) const
703 {
704     return Color4 (r / v.r, g / v.g, b / v.b, a / v.a);
705 }
706 
707 template <class T>
708 inline Color4<T>
709 Color4<T>::operator / (T x) const
710 {
711     return Color4 (r / x, g / x, b / x, a / x);
712 }
713 
714 
715 template <class T>
716 std::ostream &
717 operator << (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 
726 template <class S, class T>
727 inline Color4<T>
728 operator * (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 
734 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
735 
736 #endif // INCLUDED_IMATHCOLOR_H
737