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