HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ImathColor.h
Go to the documentation of this file.
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 
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 
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();}
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 
284 typedef unsigned int PackedColor;
285 
286 
287 //-------------------------
288 // Implementation of Color3
289 //-------------------------
290 
291 template <class T>
292 inline
294 {
295  // empty
296 }
297 
298 template <class T>
299 inline
301 {
302  // empty
303 }
304 
305 template <class T>
306 inline
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
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
323 {
324  //empty
325 }
326 
327 template <class T>
328 inline const Color3<T> &
330 {
331  *((Vec3<T> *) this) = c;
332  return *this;
333 }
334 
335 template <class T>
336 inline const Color3<T> &
338 {
339  *((Vec3<T> *) this) += c;
340  return *this;
341 }
342 
343 template <class T>
344 inline Color3<T>
346 {
347  return Color3 (*(Vec3<T> *)this + (const Vec3<T> &)c);
348 }
349 
350 template <class T>
351 inline const Color3<T> &
353 {
354  *((Vec3<T> *) this) -= c;
355  return *this;
356 }
357 
358 template <class T>
359 inline Color3<T>
361 {
362  return Color3 (*(Vec3<T> *)this - (const Vec3<T> &)c);
363 }
364 
365 template <class T>
366 inline Color3<T>
368 {
369  return Color3 (-(*(Vec3<T> *)this));
370 }
371 
372 template <class T>
373 inline const Color3<T> &
375 {
376  ((Vec3<T> *) this)->negate();
377  return *this;
378 }
379 
380 template <class T>
381 inline const Color3<T> &
383 {
384  *((Vec3<T> *) this) *= c;
385  return *this;
386 }
387 
388 template <class T>
389 inline const Color3<T> &
391 {
392  *((Vec3<T> *) this) *= a;
393  return *this;
394 }
395 
396 template <class T>
397 inline Color3<T>
399 {
400  return Color3 (*(Vec3<T> *)this * (const Vec3<T> &)c);
401 }
402 
403 template <class T>
404 inline Color3<T>
406 {
407  return Color3 (*(Vec3<T> *)this * a);
408 }
409 
410 template <class T>
411 inline const Color3<T> &
413 {
414  *((Vec3<T> *) this) /= c;
415  return *this;
416 }
417 
418 template <class T>
419 inline const Color3<T> &
421 {
422  *((Vec3<T> *) this) /= a;
423  return *this;
424 }
425 
426 template <class T>
427 inline Color3<T>
429 {
430  return Color3 (*(Vec3<T> *)this / (const Vec3<T> &)c);
431 }
432 
433 template <class T>
434 inline Color3<T>
436 {
437  return Color3 (*(Vec3<T> *)this / a);
438 }
439 
440 //-----------------------
441 // Implementation of Color4
442 //-----------------------
443 
444 template <class T>
445 inline T &
447 {
448  return (&r)[i];
449 }
450 
451 template <class T>
452 inline const T &
454 {
455  return (&r)[i];
456 }
457 
458 template <class T>
459 inline
461 {
462  // empty
463 }
464 
465 template <class T>
466 inline
468 {
469  r = g = b = a = x;
470 }
471 
472 template <class T>
473 inline
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
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
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> &
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
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
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
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
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 *
561 {
562  return (T *) &r;
563 }
564 
565 template <class T>
566 inline const T *
568 {
569  return (const T *) &r;
570 }
571 
572 template <class T>
573 template <class S>
574 inline bool
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
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> &
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>
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> &
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>
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>
627 {
628  return Color4 (-r, -g, -b, -a);
629 }
630 
631 template <class T>
632 inline const Color4<T> &
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> &
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> &
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>
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>
674 {
675  return Color4 (r * x, g * x, b * x, a * x);
676 }
677 
678 template <class T>
679 inline const Color4<T> &
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> &
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>
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>
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>
729 {
730  return Color4<T> (x * v.r, x * v.g, x * v.b, x * v.a);
731 }
732 
733 
735 
736 #endif // INCLUDED_IMATHCOLOR_H
#define IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
Color3 operator-() const
Definition: ImathColor.h:367
Color3 operator+(const Color3 &c) const
Definition: ImathColor.h:345
const Color4 & operator-=(const Color4 &v)
Definition: ImathColor.h:608
Mat3< typename promote< S, T >::type > operator*(S scalar, const Mat3< T > &m)
Returns M, where for .
Definition: Mat3.h:615
const Color4 & operator*=(const Color4 &v)
Definition: ImathColor.h:644
Color4 operator+(const Color4 &v) const
Definition: ImathColor.h:601
Color4 operator/(const Color4 &v) const
Definition: ImathColor.h:702
#define IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
const GLdouble * v
Definition: glcorearb.h:836
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:847
GLboolean GLboolean g
Definition: glcorearb.h:1221
Color4< float > C4f
Definition: ImathColor.h:281
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
Color4 operator-() const
Definition: ImathColor.h:626
unsigned int PackedColor
Definition: ImathColor.h:284
GLint y
Definition: glcorearb.h:102
Color4< half > C4h
Definition: ImathColor.h:282
static T min()
static T max()
T & operator[](int i)
Definition: ImathColor.h:446
Color4< unsigned char > Color4c
Definition: ImathColor.h:280
png_uint_32 i
Definition: png.h:2877
Color4< half > Color4h
Definition: ImathColor.h:279
const Color3 & operator/=(const Color3 &c)
Definition: ImathColor.h:412
static T epsilon()
void setValue(S a, S b, S c, S d)
Definition: ImathColor.h:517
T * getValue()
Definition: ImathColor.h:560
const Color4 & operator+=(const Color4 &v)
Definition: ImathColor.h:590
bool operator==(const Color4< S > &v) const
Definition: ImathColor.h:575
Color3()
Definition: ImathColor.h:293
const Color3 & negate()
Definition: ImathColor.h:374
Color3 operator*(const Color3 &c) const
Definition: ImathColor.h:398
Color4< unsigned char > C4c
Definition: ImathColor.h:283
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
Color3< float > Color3f
Definition: ImathColor.h:272
bool operator!=(const Color4< S > &v) const
Definition: ImathColor.h:583
Color4< float > Color4f
Definition: ImathColor.h:278
const Color3 & operator=(const Color3 &c)
Definition: ImathColor.h:329
const Color3 & operator+=(const Color3 &c)
Definition: ImathColor.h:337
T BaseType
Definition: ImathColor.h:252
const Color3 & operator*=(const Color3 &c)
Definition: ImathColor.h:382
Color3< half > C3h
Definition: ImathColor.h:275
const Color3 & operator-=(const Color3 &c)
Definition: ImathColor.h:352
Color3 operator/(const Color3 &c) const
Definition: ImathColor.h:428
Color4()
Definition: ImathColor.h:460
Color3< half > Color3h
Definition: ImathColor.h:273
GLint GLenum GLint x
Definition: glcorearb.h:408
Color4 operator*(const Color4 &v) const
Definition: ImathColor.h:666
Color3< unsigned char > Color3c
Definition: ImathColor.h:274
static T smallest()
static T baseTypeMin()
Definition: ImathColor.h:240
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
const Color4 & operator=(const Color4 &v)
Definition: ImathColor.h:505
GLboolean r
Definition: glcorearb.h:1221
Color3< float > C3f
Definition: ImathColor.h:276
const Color4 & negate()
Definition: ImathColor.h:633
static T baseTypeMax()
Definition: ImathColor.h:241
static unsigned int dimensions()
Definition: ImathColor.h:233
const Color4 & operator/=(const Color4 &v)
Definition: ImathColor.h:680
static T baseTypeEpsilon()
Definition: ImathColor.h:243
Color3< unsigned char > C3c
Definition: ImathColor.h:277
static T baseTypeSmallest()
Definition: ImathColor.h:242