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