/src/FreeImage/Source/OpenEXR/Imath/ImathColor.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 734 lines · 470 code · 140 blank · 124 comment · 18 complexity · e98e3101f6bee7d96869844eb148de16 MD5 · raw file

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