PBRT
/home/felix/UBC/projects/AdaptiveLightfieldSampling/pbrt_v2/src/3rdparty/ilmbase-1.0.2/ImathColor.h
00001 
00002 //
00003 // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
00004 // Digital Ltd. LLC
00005 // 
00006 // All rights reserved.
00007 // 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are
00010 // met:
00011 // *       Redistributions of source code must retain the above copyright
00012 // notice, this list of conditions and the following disclaimer.
00013 // *       Redistributions in binary form must reproduce the above
00014 // copyright notice, this list of conditions and the following disclaimer
00015 // in the documentation and/or other materials provided with the
00016 // distribution.
00017 // *       Neither the name of Industrial Light & Magic nor the names of
00018 // its contributors may be used to endorse or promote products derived
00019 // from this software without specific prior written permission. 
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00024 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00025 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00027 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00028 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00029 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00030 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00031 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 //
00034 
00035 
00036 
00037 #ifndef INCLUDED_IMATHCOLOR_H
00038 #define INCLUDED_IMATHCOLOR_H
00039 
00040 //----------------------------------------------------
00041 //
00042 //      A three and four component color class template.
00043 //
00044 //----------------------------------------------------
00045 
00046 #include "ImathVec.h"
00047 #include "half.h"
00048 
00049 namespace Imath {
00050 
00051 
00052 template <class T>
00053 class Color3: public Vec3 <T>
00054 {
00055   public:
00056 
00057     //-------------
00058     // Constructors
00059     //-------------
00060 
00061     Color3 ();                  // no initialization
00062     explicit Color3 (T a);      // (a a a)
00063     Color3 (T a, T b, T c);     // (a b c)
00064 
00065 
00066     //---------------------------------
00067     // Copy constructors and assignment
00068     //---------------------------------
00069 
00070     Color3 (const Color3 &c);
00071     template <class S> Color3 (const Vec3<S> &v);
00072 
00073     const Color3 &      operator = (const Color3 &c);
00074 
00075 
00076     //------------------------
00077     // Component-wise addition
00078     //------------------------
00079 
00080     const Color3 &      operator += (const Color3 &c);
00081     Color3              operator + (const Color3 &c) const;
00082 
00083 
00084     //---------------------------
00085     // Component-wise subtraction
00086     //---------------------------
00087 
00088     const Color3 &      operator -= (const Color3 &c);
00089     Color3              operator - (const Color3 &c) const;
00090 
00091 
00092     //------------------------------------
00093     // Component-wise multiplication by -1
00094     //------------------------------------
00095 
00096     Color3              operator - () const;
00097     const Color3 &      negate ();
00098 
00099 
00100     //------------------------------
00101     // Component-wise multiplication
00102     //------------------------------
00103 
00104     const Color3 &      operator *= (const Color3 &c);
00105     const Color3 &      operator *= (T a);
00106     Color3              operator * (const Color3 &c) const;
00107     Color3              operator * (T a) const;
00108 
00109 
00110     //------------------------
00111     // Component-wise division
00112     //------------------------
00113 
00114     const Color3 &      operator /= (const Color3 &c);
00115     const Color3 &      operator /= (T a);
00116     Color3              operator / (const Color3 &c) const;
00117     Color3              operator / (T a) const;
00118 };
00119 
00120 template <class T> class Color4
00121 {
00122   public:
00123 
00124     //-------------------
00125     // Access to elements
00126     //-------------------
00127 
00128     T                   r, g, b, a;
00129 
00130     T &                 operator [] (int i);
00131     const T &           operator [] (int i) const;
00132 
00133 
00134     //-------------
00135     // Constructors
00136     //-------------
00137 
00138     Color4 ();                          // no initialization
00139     explicit Color4 (T a);              // (a a a a)
00140     Color4 (T a, T b, T c, T d);        // (a b c d)
00141 
00142 
00143     //---------------------------------
00144     // Copy constructors and assignment
00145     //---------------------------------
00146 
00147     Color4 (const Color4 &v);
00148     template <class S> Color4 (const Color4<S> &v);
00149 
00150     const Color4 &      operator = (const Color4 &v);
00151 
00152 
00153     //----------------------
00154     // Compatibility with Sb
00155     //----------------------
00156 
00157     template <class S>
00158     void                setValue (S a, S b, S c, S d);
00159 
00160     template <class S>
00161     void                setValue (const Color4<S> &v);
00162 
00163     template <class S>
00164     void                getValue (S &a, S &b, S &c, S &d) const;
00165 
00166     template <class S>
00167     void                getValue (Color4<S> &v) const;
00168 
00169     T *                 getValue();
00170     const T *           getValue() const;
00171 
00172 
00173     //---------
00174     // Equality
00175     //---------
00176 
00177     template <class S>
00178     bool                operator == (const Color4<S> &v) const;
00179 
00180     template <class S>
00181     bool                operator != (const Color4<S> &v) const;
00182 
00183 
00184     //------------------------
00185     // Component-wise addition
00186     //------------------------
00187 
00188     const Color4 &      operator += (const Color4 &v);
00189     Color4              operator + (const Color4 &v) const;
00190 
00191 
00192     //---------------------------
00193     // Component-wise subtraction
00194     //---------------------------
00195 
00196     const Color4 &      operator -= (const Color4 &v);
00197     Color4              operator - (const Color4 &v) const;
00198 
00199 
00200     //------------------------------------
00201     // Component-wise multiplication by -1
00202     //------------------------------------
00203 
00204     Color4              operator - () const;
00205     const Color4 &      negate ();
00206 
00207 
00208     //------------------------------
00209     // Component-wise multiplication
00210     //------------------------------
00211 
00212     const Color4 &      operator *= (const Color4 &v);
00213     const Color4 &      operator *= (T a);
00214     Color4              operator * (const Color4 &v) const;
00215     Color4              operator * (T a) const;
00216 
00217 
00218     //------------------------
00219     // Component-wise division
00220     //------------------------
00221 
00222     const Color4 &      operator /= (const Color4 &v);
00223     const Color4 &      operator /= (T a);
00224     Color4              operator / (const Color4 &v) const;
00225     Color4              operator / (T a) const;
00226 
00227 
00228     //----------------------------------------------------------
00229     // Number of dimensions, i.e. number of elements in a Color4
00230     //----------------------------------------------------------
00231 
00232     static unsigned int dimensions() {return 4;}
00233 
00234 
00235     //-------------------------------------------------
00236     // Limitations of type T (see also class limits<T>)
00237     //-------------------------------------------------
00238 
00239     static T            baseTypeMin()           {return limits<T>::min();}
00240     static T            baseTypeMax()           {return limits<T>::max();}
00241     static T            baseTypeSmallest()      {return limits<T>::smallest();}
00242     static T            baseTypeEpsilon()       {return limits<T>::epsilon();}
00243 
00244 
00245     //--------------------------------------------------------------
00246     // Base type -- in templates, which accept a parameter, V, which
00247     // could be a Color4<T>, you can refer to T as
00248     // V::BaseType
00249     //--------------------------------------------------------------
00250 
00251     typedef T           BaseType;
00252 };
00253 
00254 //--------------
00255 // Stream output
00256 //--------------
00257 
00258 template <class T>
00259 std::ostream &  operator << (std::ostream &s, const Color4<T> &v);
00260 
00261 //----------------------------------------------------
00262 // Reverse multiplication: S * Color4<T>
00263 //----------------------------------------------------
00264 
00265 template <class S, class T> Color4<T>   operator * (S a, const Color4<T> &v);
00266 
00267 //-------------------------
00268 // Typedefs for convenience
00269 //-------------------------
00270 
00271 typedef Color3<float>           Color3f;
00272 typedef Color3<half>            Color3h;
00273 typedef Color3<unsigned char>   Color3c;
00274 typedef Color3<half>            C3h;
00275 typedef Color3<float>           C3f;
00276 typedef Color3<unsigned char>   C3c;
00277 typedef Color4<float>           Color4f;
00278 typedef Color4<half>            Color4h;
00279 typedef Color4<unsigned char>   Color4c;
00280 typedef Color4<float>           C4f;
00281 typedef Color4<half>            C4h;
00282 typedef Color4<unsigned char>   C4c;
00283 typedef unsigned int            PackedColor;
00284 
00285 
00286 //-------------------------
00287 // Implementation of Color3
00288 //-------------------------
00289 
00290 template <class T>
00291 inline
00292 Color3<T>::Color3 (): Vec3 <T> ()
00293 {
00294     // empty
00295 }
00296 
00297 template <class T>
00298 inline
00299 Color3<T>::Color3 (T a): Vec3 <T> (a)
00300 {
00301     // empty
00302 }
00303 
00304 template <class T>
00305 inline
00306 Color3<T>::Color3 (T a, T b, T c): Vec3 <T> (a, b, c)
00307 {
00308     // empty
00309 }
00310 
00311 template <class T>
00312 inline
00313 Color3<T>::Color3 (const Color3 &c): Vec3 <T> (c)
00314 {
00315     // empty
00316 }
00317 
00318 template <class T>
00319 template <class S>
00320 inline
00321 Color3<T>::Color3 (const Vec3<S> &v): Vec3 <T> (v)
00322 {
00323     //empty
00324 }
00325 
00326 template <class T>
00327 inline const Color3<T> &
00328 Color3<T>::operator = (const Color3 &c)
00329 {
00330     *((Vec3<T> *) this) = c;
00331     return *this;
00332 }
00333 
00334 template <class T>
00335 inline const Color3<T> &
00336 Color3<T>::operator += (const Color3 &c)
00337 {
00338     *((Vec3<T> *) this) += c;
00339     return *this;
00340 }
00341 
00342 template <class T>
00343 inline Color3<T>        
00344 Color3<T>::operator + (const Color3 &c) const
00345 {
00346     return Color3 (*(Vec3<T> *)this + (const Vec3<T> &)c);
00347 }
00348 
00349 template <class T>
00350 inline const Color3<T> &
00351 Color3<T>::operator -= (const Color3 &c)
00352 {
00353     *((Vec3<T> *) this) -= c;
00354     return *this;
00355 }
00356 
00357 template <class T>
00358 inline Color3<T>        
00359 Color3<T>::operator - (const Color3 &c) const
00360 {
00361     return Color3 (*(Vec3<T> *)this - (const Vec3<T> &)c);
00362 }
00363 
00364 template <class T>
00365 inline Color3<T>        
00366 Color3<T>::operator - () const
00367 {
00368     return Color3 (-(*(Vec3<T> *)this));
00369 }
00370 
00371 template <class T>
00372 inline const Color3<T> &
00373 Color3<T>::negate ()
00374 {
00375     ((Vec3<T> *) this)->negate();
00376     return *this;
00377 }
00378 
00379 template <class T>
00380 inline const Color3<T> &
00381 Color3<T>::operator *= (const Color3 &c)
00382 {
00383     *((Vec3<T> *) this) *= c;
00384     return *this;
00385 }
00386 
00387 template <class T>
00388 inline const Color3<T> &
00389 Color3<T>::operator *= (T a)
00390 {
00391     *((Vec3<T> *) this) *= a;
00392     return *this;
00393 }
00394 
00395 template <class T>
00396 inline Color3<T>        
00397 Color3<T>::operator * (const Color3 &c) const
00398 {
00399     return Color3 (*(Vec3<T> *)this * (const Vec3<T> &)c);
00400 }
00401 
00402 template <class T>
00403 inline Color3<T>        
00404 Color3<T>::operator * (T a) const
00405 {
00406     return Color3 (*(Vec3<T> *)this * a);
00407 }
00408 
00409 template <class T>
00410 inline const Color3<T> &
00411 Color3<T>::operator /= (const Color3 &c)
00412 {
00413     *((Vec3<T> *) this) /= c;
00414     return *this;
00415 }
00416 
00417 template <class T>
00418 inline const Color3<T> &
00419 Color3<T>::operator /= (T a)
00420 {
00421     *((Vec3<T> *) this) /= a;
00422     return *this;
00423 }
00424 
00425 template <class T>
00426 inline Color3<T>        
00427 Color3<T>::operator / (const Color3 &c) const
00428 {
00429     return Color3 (*(Vec3<T> *)this / (const Vec3<T> &)c);
00430 }
00431 
00432 template <class T>
00433 inline Color3<T>        
00434 Color3<T>::operator / (T a) const
00435 {
00436     return Color3 (*(Vec3<T> *)this / a);
00437 }
00438 
00439 //-----------------------
00440 // Implementation of Color4
00441 //-----------------------
00442 
00443 template <class T>
00444 inline T &
00445 Color4<T>::operator [] (int i)
00446 {
00447     return (&r)[i];
00448 }
00449 
00450 template <class T>
00451 inline const T &
00452 Color4<T>::operator [] (int i) const
00453 {
00454     return (&r)[i];
00455 }
00456 
00457 template <class T>
00458 inline
00459 Color4<T>::Color4 ()
00460 {
00461     // empty
00462 }
00463 
00464 template <class T>
00465 inline
00466 Color4<T>::Color4 (T x)
00467 {
00468     r = g = b = a = x;
00469 }
00470 
00471 template <class T>
00472 inline
00473 Color4<T>::Color4 (T x, T y, T z, T w)
00474 {
00475     r = x;
00476     g = y;
00477     b = z;
00478     a = w;
00479 }
00480 
00481 template <class T>
00482 inline
00483 Color4<T>::Color4 (const Color4 &v)
00484 {
00485     r = v.r;
00486     g = v.g;
00487     b = v.b;
00488     a = v.a;
00489 }
00490 
00491 template <class T>
00492 template <class S>
00493 inline
00494 Color4<T>::Color4 (const Color4<S> &v)
00495 {
00496     r = T (v.r);
00497     g = T (v.g);
00498     b = T (v.b);
00499     a = T (v.a);
00500 }
00501 
00502 template <class T>
00503 inline const Color4<T> &
00504 Color4<T>::operator = (const Color4 &v)
00505 {
00506     r = v.r;
00507     g = v.g;
00508     b = v.b;
00509     a = v.a;
00510     return *this;
00511 }
00512 
00513 template <class T>
00514 template <class S>
00515 inline void
00516 Color4<T>::setValue (S x, S y, S z, S w)
00517 {
00518     r = T (x);
00519     g = T (y);
00520     b = T (z);
00521     a = T (w);
00522 }
00523 
00524 template <class T>
00525 template <class S>
00526 inline void
00527 Color4<T>::setValue (const Color4<S> &v)
00528 {
00529     r = T (v.r);
00530     g = T (v.g);
00531     b = T (v.b);
00532     a = T (v.a);
00533 }
00534 
00535 template <class T>
00536 template <class S>
00537 inline void
00538 Color4<T>::getValue (S &x, S &y, S &z, S &w) const
00539 {
00540     x = S (r);
00541     y = S (g);
00542     z = S (b);
00543     w = S (a);
00544 }
00545 
00546 template <class T>
00547 template <class S>
00548 inline void
00549 Color4<T>::getValue (Color4<S> &v) const
00550 {
00551     v.r = S (r);
00552     v.g = S (g);
00553     v.b = S (b);
00554     v.a = S (a);
00555 }
00556 
00557 template <class T>
00558 inline T *
00559 Color4<T>::getValue()
00560 {
00561     return (T *) &r;
00562 }
00563 
00564 template <class T>
00565 inline const T *
00566 Color4<T>::getValue() const
00567 {
00568     return (const T *) &r;
00569 }
00570 
00571 template <class T>
00572 template <class S>
00573 inline bool
00574 Color4<T>::operator == (const Color4<S> &v) const
00575 {
00576     return r == v.r && g == v.g && b == v.b && a == v.a;
00577 }
00578 
00579 template <class T>
00580 template <class S>
00581 inline bool
00582 Color4<T>::operator != (const Color4<S> &v) const
00583 {
00584     return r != v.r || g != v.g || b != v.b || a != v.a;
00585 }
00586 
00587 template <class T>
00588 inline const Color4<T> &
00589 Color4<T>::operator += (const Color4 &v)
00590 {
00591     r += v.r;
00592     g += v.g;
00593     b += v.b;
00594     a += v.a;
00595     return *this;
00596 }
00597 
00598 template <class T>
00599 inline Color4<T>
00600 Color4<T>::operator + (const Color4 &v) const
00601 {
00602     return Color4 (r + v.r, g + v.g, b + v.b, a + v.a);
00603 }
00604 
00605 template <class T>
00606 inline const Color4<T> &
00607 Color4<T>::operator -= (const Color4 &v)
00608 {
00609     r -= v.r;
00610     g -= v.g;
00611     b -= v.b;
00612     a -= v.a;
00613     return *this;
00614 }
00615 
00616 template <class T>
00617 inline Color4<T>
00618 Color4<T>::operator - (const Color4 &v) const
00619 {
00620     return Color4 (r - v.r, g - v.g, b - v.b, a - v.a);
00621 }
00622 
00623 template <class T>
00624 inline Color4<T>
00625 Color4<T>::operator - () const
00626 {
00627     return Color4 (-r, -g, -b, -a);
00628 }
00629 
00630 template <class T>
00631 inline const Color4<T> &
00632 Color4<T>::negate ()
00633 {
00634     r = -r;
00635     g = -g;
00636     b = -b;
00637     a = -a;
00638     return *this;
00639 }
00640 
00641 template <class T>
00642 inline const Color4<T> &
00643 Color4<T>::operator *= (const Color4 &v)
00644 {
00645     r *= v.r;
00646     g *= v.g;
00647     b *= v.b;
00648     a *= v.a;
00649     return *this;
00650 }
00651 
00652 template <class T>
00653 inline const Color4<T> &
00654 Color4<T>::operator *= (T x)
00655 {
00656     r *= x;
00657     g *= x;
00658     b *= x;
00659     a *= x;
00660     return *this;
00661 }
00662 
00663 template <class T>
00664 inline Color4<T>
00665 Color4<T>::operator * (const Color4 &v) const
00666 {
00667     return Color4 (r * v.r, g * v.g, b * v.b, a * v.a);
00668 }
00669 
00670 template <class T>
00671 inline Color4<T>
00672 Color4<T>::operator * (T x) const
00673 {
00674     return Color4 (r * x, g * x, b * x, a * x);
00675 }
00676 
00677 template <class T>
00678 inline const Color4<T> &
00679 Color4<T>::operator /= (const Color4 &v)
00680 {
00681     r /= v.r;
00682     g /= v.g;
00683     b /= v.b;
00684     a /= v.a;
00685     return *this;
00686 }
00687 
00688 template <class T>
00689 inline const Color4<T> &
00690 Color4<T>::operator /= (T x)
00691 {
00692     r /= x;
00693     g /= x;
00694     b /= x;
00695     a /= x;
00696     return *this;
00697 }
00698 
00699 template <class T>
00700 inline Color4<T>
00701 Color4<T>::operator / (const Color4 &v) const
00702 {
00703     return Color4 (r / v.r, g / v.g, b / v.b, a / v.a);
00704 }
00705 
00706 template <class T>
00707 inline Color4<T>
00708 Color4<T>::operator / (T x) const
00709 {
00710     return Color4 (r / x, g / x, b / x, a / x);
00711 }
00712 
00713 
00714 template <class T>
00715 std::ostream &
00716 operator << (std::ostream &s, const Color4<T> &v)
00717 {
00718     return s << '(' << v.r << ' ' << v.g << ' ' << v.b << ' ' << v.a << ')';
00719 }
00720 
00721 //-----------------------------------------
00722 // Implementation of reverse multiplication
00723 //-----------------------------------------
00724 
00725 template <class S, class T>
00726 inline Color4<T>
00727 operator * (S x, const Color4<T> &v)
00728 {
00729     return Color4<T> (x * v.r, x * v.g, x * v.b, x * v.a);
00730 }
00731 
00732 } // namespace Imath
00733 
00734 #endif