ImathColor.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. #ifndef INCLUDED_IMATHCOLOR_H
  35. #define INCLUDED_IMATHCOLOR_H
  36. //----------------------------------------------------
  37. //
  38. // A three and four component color class template.
  39. //
  40. //----------------------------------------------------
  41. #include "ImathVec.h"
  42. #include "ImathNamespace.h"
  43. #include "half.h"
  44. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  45. template <class T>
  46. class Color3: public Vec3 <T>
  47. {
  48. public:
  49. //-------------
  50. // Constructors
  51. //-------------
  52. Color3 (); // no initialization
  53. explicit Color3 (T a); // (a a a)
  54. Color3 (T a, T b, T c); // (a b c)
  55. //---------------------------------
  56. // Copy constructors and assignment
  57. //---------------------------------
  58. Color3 (const Color3 &c);
  59. template <class S> Color3 (const Vec3<S> &v);
  60. const Color3 & operator = (const Color3 &c);
  61. //------------------------
  62. // Component-wise addition
  63. //------------------------
  64. const Color3 & operator += (const Color3 &c);
  65. Color3 operator + (const Color3 &c) const;
  66. //---------------------------
  67. // Component-wise subtraction
  68. //---------------------------
  69. const Color3 & operator -= (const Color3 &c);
  70. Color3 operator - (const Color3 &c) const;
  71. //------------------------------------
  72. // Component-wise multiplication by -1
  73. //------------------------------------
  74. Color3 operator - () const;
  75. const Color3 & negate ();
  76. //------------------------------
  77. // Component-wise multiplication
  78. //------------------------------
  79. const Color3 & operator *= (const Color3 &c);
  80. const Color3 & operator *= (T a);
  81. Color3 operator * (const Color3 &c) const;
  82. Color3 operator * (T a) const;
  83. //------------------------
  84. // Component-wise division
  85. //------------------------
  86. const Color3 & operator /= (const Color3 &c);
  87. const Color3 & operator /= (T a);
  88. Color3 operator / (const Color3 &c) const;
  89. Color3 operator / (T a) const;
  90. };
  91. template <class T> class Color4
  92. {
  93. public:
  94. //-------------------
  95. // Access to elements
  96. //-------------------
  97. T r, g, b, a;
  98. T & operator [] (int i);
  99. const T & operator [] (int i) const;
  100. //-------------
  101. // Constructors
  102. //-------------
  103. Color4 (); // no initialization
  104. explicit Color4 (T a); // (a a a a)
  105. Color4 (T a, T b, T c, T d); // (a b c d)
  106. //---------------------------------
  107. // Copy constructors and assignment
  108. //---------------------------------
  109. Color4 (const Color4 &v);
  110. template <class S> Color4 (const Color4<S> &v);
  111. const Color4 & operator = (const Color4 &v);
  112. //----------------------
  113. // Compatibility with Sb
  114. //----------------------
  115. template <class S>
  116. void setValue (S a, S b, S c, S d);
  117. template <class S>
  118. void setValue (const Color4<S> &v);
  119. template <class S>
  120. void getValue (S &a, S &b, S &c, S &d) const;
  121. template <class S>
  122. void getValue (Color4<S> &v) const;
  123. T * getValue();
  124. const T * getValue() const;
  125. //---------
  126. // Equality
  127. //---------
  128. template <class S>
  129. bool operator == (const Color4<S> &v) const;
  130. template <class S>
  131. bool operator != (const Color4<S> &v) const;
  132. //------------------------
  133. // Component-wise addition
  134. //------------------------
  135. const Color4 & operator += (const Color4 &v);
  136. Color4 operator + (const Color4 &v) const;
  137. //---------------------------
  138. // Component-wise subtraction
  139. //---------------------------
  140. const Color4 & operator -= (const Color4 &v);
  141. Color4 operator - (const Color4 &v) const;
  142. //------------------------------------
  143. // Component-wise multiplication by -1
  144. //------------------------------------
  145. Color4 operator - () const;
  146. const Color4 & negate ();
  147. //------------------------------
  148. // Component-wise multiplication
  149. //------------------------------
  150. const Color4 & operator *= (const Color4 &v);
  151. const Color4 & operator *= (T a);
  152. Color4 operator * (const Color4 &v) const;
  153. Color4 operator * (T a) const;
  154. //------------------------
  155. // Component-wise division
  156. //------------------------
  157. const Color4 & operator /= (const Color4 &v);
  158. const Color4 & operator /= (T a);
  159. Color4 operator / (const Color4 &v) const;
  160. Color4 operator / (T a) const;
  161. //----------------------------------------------------------
  162. // Number of dimensions, i.e. number of elements in a Color4
  163. //----------------------------------------------------------
  164. static unsigned int dimensions() {return 4;}
  165. //-------------------------------------------------
  166. // Limitations of type T (see also class limits<T>)
  167. //-------------------------------------------------
  168. static T baseTypeMin() {return limits<T>::min();}
  169. static T baseTypeMax() {return limits<T>::max();}
  170. static T baseTypeSmallest() {return limits<T>::smallest();}
  171. static T baseTypeEpsilon() {return limits<T>::epsilon();}
  172. //--------------------------------------------------------------
  173. // Base type -- in templates, which accept a parameter, V, which
  174. // could be a Color4<T>, you can refer to T as
  175. // V::BaseType
  176. //--------------------------------------------------------------
  177. typedef T BaseType;
  178. };
  179. //--------------
  180. // Stream output
  181. //--------------
  182. template <class T>
  183. std::ostream & operator << (std::ostream &s, const Color4<T> &v);
  184. //----------------------------------------------------
  185. // Reverse multiplication: S * Color4<T>
  186. //----------------------------------------------------
  187. template <class S, class T> Color4<T> operator * (S a, const Color4<T> &v);
  188. //-------------------------
  189. // Typedefs for convenience
  190. //-------------------------
  191. typedef Color3<float> Color3f;
  192. typedef Color3<half> Color3h;
  193. typedef Color3<unsigned char> Color3c;
  194. typedef Color3<half> C3h;
  195. typedef Color3<float> C3f;
  196. typedef Color3<unsigned char> C3c;
  197. typedef Color4<float> Color4f;
  198. typedef Color4<half> Color4h;
  199. typedef Color4<unsigned char> Color4c;
  200. typedef Color4<float> C4f;
  201. typedef Color4<half> C4h;
  202. typedef Color4<unsigned char> C4c;
  203. typedef unsigned int PackedColor;
  204. //-------------------------
  205. // Implementation of Color3
  206. //-------------------------
  207. template <class T>
  208. inline
  209. Color3<T>::Color3 (): Vec3 <T> ()
  210. {
  211. // empty
  212. }
  213. template <class T>
  214. inline
  215. Color3<T>::Color3 (T a): Vec3 <T> (a)
  216. {
  217. // empty
  218. }
  219. template <class T>
  220. inline
  221. Color3<T>::Color3 (T a, T b, T c): Vec3 <T> (a, b, c)
  222. {
  223. // empty
  224. }
  225. template <class T>
  226. inline
  227. Color3<T>::Color3 (const Color3 &c): Vec3 <T> (c)
  228. {
  229. // empty
  230. }
  231. template <class T>
  232. template <class S>
  233. inline
  234. Color3<T>::Color3 (const Vec3<S> &v): Vec3 <T> (v)
  235. {
  236. //empty
  237. }
  238. template <class T>
  239. inline const Color3<T> &
  240. Color3<T>::operator = (const Color3 &c)
  241. {
  242. *((Vec3<T> *) this) = c;
  243. return *this;
  244. }
  245. template <class T>
  246. inline const Color3<T> &
  247. Color3<T>::operator += (const Color3 &c)
  248. {
  249. *((Vec3<T> *) this) += c;
  250. return *this;
  251. }
  252. template <class T>
  253. inline Color3<T>
  254. Color3<T>::operator + (const Color3 &c) const
  255. {
  256. return Color3 (*(Vec3<T> *)this + (const Vec3<T> &)c);
  257. }
  258. template <class T>
  259. inline const Color3<T> &
  260. Color3<T>::operator -= (const Color3 &c)
  261. {
  262. *((Vec3<T> *) this) -= c;
  263. return *this;
  264. }
  265. template <class T>
  266. inline Color3<T>
  267. Color3<T>::operator - (const Color3 &c) const
  268. {
  269. return Color3 (*(Vec3<T> *)this - (const Vec3<T> &)c);
  270. }
  271. template <class T>
  272. inline Color3<T>
  273. Color3<T>::operator - () const
  274. {
  275. return Color3 (-(*(Vec3<T> *)this));
  276. }
  277. template <class T>
  278. inline const Color3<T> &
  279. Color3<T>::negate ()
  280. {
  281. ((Vec3<T> *) this)->negate();
  282. return *this;
  283. }
  284. template <class T>
  285. inline const Color3<T> &
  286. Color3<T>::operator *= (const Color3 &c)
  287. {
  288. *((Vec3<T> *) this) *= c;
  289. return *this;
  290. }
  291. template <class T>
  292. inline const Color3<T> &
  293. Color3<T>::operator *= (T a)
  294. {
  295. *((Vec3<T> *) this) *= a;
  296. return *this;
  297. }
  298. template <class T>
  299. inline Color3<T>
  300. Color3<T>::operator * (const Color3 &c) const
  301. {
  302. return Color3 (*(Vec3<T> *)this * (const Vec3<T> &)c);
  303. }
  304. template <class T>
  305. inline Color3<T>
  306. Color3<T>::operator * (T a) const
  307. {
  308. return Color3 (*(Vec3<T> *)this * a);
  309. }
  310. template <class T>
  311. inline const Color3<T> &
  312. Color3<T>::operator /= (const Color3 &c)
  313. {
  314. *((Vec3<T> *) this) /= c;
  315. return *this;
  316. }
  317. template <class T>
  318. inline const Color3<T> &
  319. Color3<T>::operator /= (T a)
  320. {
  321. *((Vec3<T> *) this) /= a;
  322. return *this;
  323. }
  324. template <class T>
  325. inline Color3<T>
  326. Color3<T>::operator / (const Color3 &c) const
  327. {
  328. return Color3 (*(Vec3<T> *)this / (const Vec3<T> &)c);
  329. }
  330. template <class T>
  331. inline Color3<T>
  332. Color3<T>::operator / (T a) const
  333. {
  334. return Color3 (*(Vec3<T> *)this / a);
  335. }
  336. //-----------------------
  337. // Implementation of Color4
  338. //-----------------------
  339. template <class T>
  340. inline T &
  341. Color4<T>::operator [] (int i)
  342. {
  343. return (&r)[i];
  344. }
  345. template <class T>
  346. inline const T &
  347. Color4<T>::operator [] (int i) const
  348. {
  349. return (&r)[i];
  350. }
  351. template <class T>
  352. inline
  353. Color4<T>::Color4 ()
  354. {
  355. // empty
  356. }
  357. template <class T>
  358. inline
  359. Color4<T>::Color4 (T x)
  360. {
  361. r = g = b = a = x;
  362. }
  363. template <class T>
  364. inline
  365. Color4<T>::Color4 (T x, T y, T z, T w)
  366. {
  367. r = x;
  368. g = y;
  369. b = z;
  370. a = w;
  371. }
  372. template <class T>
  373. inline
  374. Color4<T>::Color4 (const Color4 &v)
  375. {
  376. r = v.r;
  377. g = v.g;
  378. b = v.b;
  379. a = v.a;
  380. }
  381. template <class T>
  382. template <class S>
  383. inline
  384. Color4<T>::Color4 (const Color4<S> &v)
  385. {
  386. r = T (v.r);
  387. g = T (v.g);
  388. b = T (v.b);
  389. a = T (v.a);
  390. }
  391. template <class T>
  392. inline const Color4<T> &
  393. Color4<T>::operator = (const Color4 &v)
  394. {
  395. r = v.r;
  396. g = v.g;
  397. b = v.b;
  398. a = v.a;
  399. return *this;
  400. }
  401. template <class T>
  402. template <class S>
  403. inline void
  404. Color4<T>::setValue (S x, S y, S z, S w)
  405. {
  406. r = T (x);
  407. g = T (y);
  408. b = T (z);
  409. a = T (w);
  410. }
  411. template <class T>
  412. template <class S>
  413. inline void
  414. Color4<T>::setValue (const Color4<S> &v)
  415. {
  416. r = T (v.r);
  417. g = T (v.g);
  418. b = T (v.b);
  419. a = T (v.a);
  420. }
  421. template <class T>
  422. template <class S>
  423. inline void
  424. Color4<T>::getValue (S &x, S &y, S &z, S &w) const
  425. {
  426. x = S (r);
  427. y = S (g);
  428. z = S (b);
  429. w = S (a);
  430. }
  431. template <class T>
  432. template <class S>
  433. inline void
  434. Color4<T>::getValue (Color4<S> &v) const
  435. {
  436. v.r = S (r);
  437. v.g = S (g);
  438. v.b = S (b);
  439. v.a = S (a);
  440. }
  441. template <class T>
  442. inline T *
  443. Color4<T>::getValue()
  444. {
  445. return (T *) &r;
  446. }
  447. template <class T>
  448. inline const T *
  449. Color4<T>::getValue() const
  450. {
  451. return (const T *) &r;
  452. }
  453. template <class T>
  454. template <class S>
  455. inline bool
  456. Color4<T>::operator == (const Color4<S> &v) const
  457. {
  458. return r == v.r && g == v.g && b == v.b && a == v.a;
  459. }
  460. template <class T>
  461. template <class S>
  462. inline bool
  463. Color4<T>::operator != (const Color4<S> &v) const
  464. {
  465. return r != v.r || g != v.g || b != v.b || a != v.a;
  466. }
  467. template <class T>
  468. inline const Color4<T> &
  469. Color4<T>::operator += (const Color4 &v)
  470. {
  471. r += v.r;
  472. g += v.g;
  473. b += v.b;
  474. a += v.a;
  475. return *this;
  476. }
  477. template <class T>
  478. inline Color4<T>
  479. Color4<T>::operator + (const Color4 &v) const
  480. {
  481. return Color4 (r + v.r, g + v.g, b + v.b, a + v.a);
  482. }
  483. template <class T>
  484. inline const Color4<T> &
  485. Color4<T>::operator -= (const Color4 &v)
  486. {
  487. r -= v.r;
  488. g -= v.g;
  489. b -= v.b;
  490. a -= v.a;
  491. return *this;
  492. }
  493. template <class T>
  494. inline Color4<T>
  495. Color4<T>::operator - (const Color4 &v) const
  496. {
  497. return Color4 (r - v.r, g - v.g, b - v.b, a - v.a);
  498. }
  499. template <class T>
  500. inline Color4<T>
  501. Color4<T>::operator - () const
  502. {
  503. return Color4 (-r, -g, -b, -a);
  504. }
  505. template <class T>
  506. inline const Color4<T> &
  507. Color4<T>::negate ()
  508. {
  509. r = -r;
  510. g = -g;
  511. b = -b;
  512. a = -a;
  513. return *this;
  514. }
  515. template <class T>
  516. inline const Color4<T> &
  517. Color4<T>::operator *= (const Color4 &v)
  518. {
  519. r *= v.r;
  520. g *= v.g;
  521. b *= v.b;
  522. a *= v.a;
  523. return *this;
  524. }
  525. template <class T>
  526. inline const Color4<T> &
  527. Color4<T>::operator *= (T x)
  528. {
  529. r *= x;
  530. g *= x;
  531. b *= x;
  532. a *= x;
  533. return *this;
  534. }
  535. template <class T>
  536. inline Color4<T>
  537. Color4<T>::operator * (const Color4 &v) const
  538. {
  539. return Color4 (r * v.r, g * v.g, b * v.b, a * v.a);
  540. }
  541. template <class T>
  542. inline Color4<T>
  543. Color4<T>::operator * (T x) const
  544. {
  545. return Color4 (r * x, g * x, b * x, a * x);
  546. }
  547. template <class T>
  548. inline const Color4<T> &
  549. Color4<T>::operator /= (const Color4 &v)
  550. {
  551. r /= v.r;
  552. g /= v.g;
  553. b /= v.b;
  554. a /= v.a;
  555. return *this;
  556. }
  557. template <class T>
  558. inline const Color4<T> &
  559. Color4<T>::operator /= (T x)
  560. {
  561. r /= x;
  562. g /= x;
  563. b /= x;
  564. a /= x;
  565. return *this;
  566. }
  567. template <class T>
  568. inline Color4<T>
  569. Color4<T>::operator / (const Color4 &v) const
  570. {
  571. return Color4 (r / v.r, g / v.g, b / v.b, a / v.a);
  572. }
  573. template <class T>
  574. inline Color4<T>
  575. Color4<T>::operator / (T x) const
  576. {
  577. return Color4 (r / x, g / x, b / x, a / x);
  578. }
  579. template <class T>
  580. std::ostream &
  581. operator << (std::ostream &s, const Color4<T> &v)
  582. {
  583. return s << '(' << v.r << ' ' << v.g << ' ' << v.b << ' ' << v.a << ')';
  584. }
  585. //-----------------------------------------
  586. // Implementation of reverse multiplication
  587. //-----------------------------------------
  588. template <class S, class T>
  589. inline Color4<T>
  590. operator * (S x, const Color4<T> &v)
  591. {
  592. return Color4<T> (x * v.r, x * v.g, x * v.b, x * v.a);
  593. }
  594. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  595. #endif // INCLUDED_IMATHCOLOR_H