Path: blob/master/3rdparty/openexr/Imath/ImathColor.h
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////33343536#ifndef INCLUDED_IMATHCOLOR_H37#define INCLUDED_IMATHCOLOR_H3839//----------------------------------------------------40//41// A three and four component color class template.42//43//----------------------------------------------------4445#include "ImathVec.h"46#include "half.h"4748namespace Imath {495051template <class T>52class Color3: public Vec3 <T>53{54public:5556//-------------57// Constructors58//-------------5960Color3 (); // no initialization61explicit Color3 (T a); // (a a a)62Color3 (T a, T b, T c); // (a b c)636465//---------------------------------66// Copy constructors and assignment67//---------------------------------6869Color3 (const Color3 &c);70template <class S> Color3 (const Vec3<S> &v);7172const Color3 & operator = (const Color3 &c);737475//------------------------76// Component-wise addition77//------------------------7879const Color3 & operator += (const Color3 &c);80Color3 operator + (const Color3 &c) const;818283//---------------------------84// Component-wise subtraction85//---------------------------8687const Color3 & operator -= (const Color3 &c);88Color3 operator - (const Color3 &c) const;899091//------------------------------------92// Component-wise multiplication by -193//------------------------------------9495Color3 operator - () const;96const Color3 & negate ();979899//------------------------------100// Component-wise multiplication101//------------------------------102103const Color3 & operator *= (const Color3 &c);104const Color3 & operator *= (T a);105Color3 operator * (const Color3 &c) const;106Color3 operator * (T a) const;107108109//------------------------110// Component-wise division111//------------------------112113const Color3 & operator /= (const Color3 &c);114const Color3 & operator /= (T a);115Color3 operator / (const Color3 &c) const;116Color3 operator / (T a) const;117};118119template <class T> class Color4120{121public:122123//-------------------124// Access to elements125//-------------------126127T r, g, b, a;128129T & operator [] (int i);130const T & operator [] (int i) const;131132133//-------------134// Constructors135//-------------136137Color4 (); // no initialization138explicit Color4 (T a); // (a a a a)139Color4 (T a, T b, T c, T d); // (a b c d)140141142//---------------------------------143// Copy constructors and assignment144//---------------------------------145146Color4 (const Color4 &v);147template <class S> Color4 (const Color4<S> &v);148149const Color4 & operator = (const Color4 &v);150151152//----------------------153// Compatibility with Sb154//----------------------155156template <class S>157void setValue (S a, S b, S c, S d);158159template <class S>160void setValue (const Color4<S> &v);161162template <class S>163void getValue (S &a, S &b, S &c, S &d) const;164165template <class S>166void getValue (Color4<S> &v) const;167168T * getValue();169const T * getValue() const;170171172//---------173// Equality174//---------175176template <class S>177bool operator == (const Color4<S> &v) const;178179template <class S>180bool operator != (const Color4<S> &v) const;181182183//------------------------184// Component-wise addition185//------------------------186187const Color4 & operator += (const Color4 &v);188Color4 operator + (const Color4 &v) const;189190191//---------------------------192// Component-wise subtraction193//---------------------------194195const Color4 & operator -= (const Color4 &v);196Color4 operator - (const Color4 &v) const;197198199//------------------------------------200// Component-wise multiplication by -1201//------------------------------------202203Color4 operator - () const;204const Color4 & negate ();205206207//------------------------------208// Component-wise multiplication209//------------------------------210211const Color4 & operator *= (const Color4 &v);212const Color4 & operator *= (T a);213Color4 operator * (const Color4 &v) const;214Color4 operator * (T a) const;215216217//------------------------218// Component-wise division219//------------------------220221const Color4 & operator /= (const Color4 &v);222const Color4 & operator /= (T a);223Color4 operator / (const Color4 &v) const;224Color4 operator / (T a) const;225226227//----------------------------------------------------------228// Number of dimensions, i.e. number of elements in a Color4229//----------------------------------------------------------230231static unsigned int dimensions() {return 4;}232233234//-------------------------------------------------235// Limitations of type T (see also class limits<T>)236//-------------------------------------------------237238static T baseTypeMin() {return limits<T>::min();}239static T baseTypeMax() {return limits<T>::max();}240static T baseTypeSmallest() {return limits<T>::smallest();}241static T baseTypeEpsilon() {return limits<T>::epsilon();}242243244//--------------------------------------------------------------245// Base type -- in templates, which accept a parameter, V, which246// could be a Color4<T>, you can refer to T as247// V::BaseType248//--------------------------------------------------------------249250typedef T BaseType;251};252253//--------------254// Stream output255//--------------256257template <class T>258std::ostream & operator << (std::ostream &s, const Color4<T> &v);259260//----------------------------------------------------261// Reverse multiplication: S * Color4<T>262//----------------------------------------------------263264template <class S, class T> Color4<T> operator * (S a, const Color4<T> &v);265266//-------------------------267// Typedefs for convenience268//-------------------------269270typedef Color3<float> Color3f;271typedef Color3<half> Color3h;272typedef Color3<unsigned char> Color3c;273typedef Color3<half> C3h;274typedef Color3<float> C3f;275typedef Color3<unsigned char> C3c;276typedef Color4<float> Color4f;277typedef Color4<half> Color4h;278typedef Color4<unsigned char> Color4c;279typedef Color4<float> C4f;280typedef Color4<half> C4h;281typedef Color4<unsigned char> C4c;282typedef unsigned int PackedColor;283284285//-------------------------286// Implementation of Color3287//-------------------------288289template <class T>290inline291Color3<T>::Color3 (): Vec3 <T> ()292{293// empty294}295296template <class T>297inline298Color3<T>::Color3 (T a): Vec3 <T> (a)299{300// empty301}302303template <class T>304inline305Color3<T>::Color3 (T a, T b, T c): Vec3 <T> (a, b, c)306{307// empty308}309310template <class T>311inline312Color3<T>::Color3 (const Color3 &c): Vec3 <T> (c)313{314// empty315}316317template <class T>318template <class S>319inline320Color3<T>::Color3 (const Vec3<S> &v): Vec3 <T> (v)321{322//empty323}324325template <class T>326inline const Color3<T> &327Color3<T>::operator = (const Color3 &c)328{329*((Vec3<T> *) this) = c;330return *this;331}332333template <class T>334inline const Color3<T> &335Color3<T>::operator += (const Color3 &c)336{337*((Vec3<T> *) this) += c;338return *this;339}340341template <class T>342inline Color3<T>343Color3<T>::operator + (const Color3 &c) const344{345return Color3 (*(Vec3<T> *)this + (const Vec3<T> &)c);346}347348template <class T>349inline const Color3<T> &350Color3<T>::operator -= (const Color3 &c)351{352*((Vec3<T> *) this) -= c;353return *this;354}355356template <class T>357inline Color3<T>358Color3<T>::operator - (const Color3 &c) const359{360return Color3 (*(Vec3<T> *)this - (const Vec3<T> &)c);361}362363template <class T>364inline Color3<T>365Color3<T>::operator - () const366{367return Color3 (-(*(Vec3<T> *)this));368}369370template <class T>371inline const Color3<T> &372Color3<T>::negate ()373{374((Vec3<T> *) this)->negate();375return *this;376}377378template <class T>379inline const Color3<T> &380Color3<T>::operator *= (const Color3 &c)381{382*((Vec3<T> *) this) *= c;383return *this;384}385386template <class T>387inline const Color3<T> &388Color3<T>::operator *= (T a)389{390*((Vec3<T> *) this) *= a;391return *this;392}393394template <class T>395inline Color3<T>396Color3<T>::operator * (const Color3 &c) const397{398return Color3 (*(Vec3<T> *)this * (const Vec3<T> &)c);399}400401template <class T>402inline Color3<T>403Color3<T>::operator * (T a) const404{405return Color3 (*(Vec3<T> *)this * a);406}407408template <class T>409inline const Color3<T> &410Color3<T>::operator /= (const Color3 &c)411{412*((Vec3<T> *) this) /= c;413return *this;414}415416template <class T>417inline const Color3<T> &418Color3<T>::operator /= (T a)419{420*((Vec3<T> *) this) /= a;421return *this;422}423424template <class T>425inline Color3<T>426Color3<T>::operator / (const Color3 &c) const427{428return Color3 (*(Vec3<T> *)this / (const Vec3<T> &)c);429}430431template <class T>432inline Color3<T>433Color3<T>::operator / (T a) const434{435return Color3 (*(Vec3<T> *)this / a);436}437438//-----------------------439// Implementation of Color4440//-----------------------441442template <class T>443inline T &444Color4<T>::operator [] (int i)445{446return (&r)[i];447}448449template <class T>450inline const T &451Color4<T>::operator [] (int i) const452{453return (&r)[i];454}455456template <class T>457inline458Color4<T>::Color4 ()459{460// empty461}462463template <class T>464inline465Color4<T>::Color4 (T x)466{467r = g = b = a = x;468}469470template <class T>471inline472Color4<T>::Color4 (T x, T y, T z, T w)473{474r = x;475g = y;476b = z;477a = w;478}479480template <class T>481inline482Color4<T>::Color4 (const Color4 &v)483{484r = v.r;485g = v.g;486b = v.b;487a = v.a;488}489490template <class T>491template <class S>492inline493Color4<T>::Color4 (const Color4<S> &v)494{495r = T (v.r);496g = T (v.g);497b = T (v.b);498a = T (v.a);499}500501template <class T>502inline const Color4<T> &503Color4<T>::operator = (const Color4 &v)504{505r = v.r;506g = v.g;507b = v.b;508a = v.a;509return *this;510}511512template <class T>513template <class S>514inline void515Color4<T>::setValue (S x, S y, S z, S w)516{517r = T (x);518g = T (y);519b = T (z);520a = T (w);521}522523template <class T>524template <class S>525inline void526Color4<T>::setValue (const Color4<S> &v)527{528r = T (v.r);529g = T (v.g);530b = T (v.b);531a = T (v.a);532}533534template <class T>535template <class S>536inline void537Color4<T>::getValue (S &x, S &y, S &z, S &w) const538{539x = S (r);540y = S (g);541z = S (b);542w = S (a);543}544545template <class T>546template <class S>547inline void548Color4<T>::getValue (Color4<S> &v) const549{550v.r = S (r);551v.g = S (g);552v.b = S (b);553v.a = S (a);554}555556template <class T>557inline T *558Color4<T>::getValue()559{560return (T *) &r;561}562563template <class T>564inline const T *565Color4<T>::getValue() const566{567return (const T *) &r;568}569570template <class T>571template <class S>572inline bool573Color4<T>::operator == (const Color4<S> &v) const574{575return r == v.r && g == v.g && b == v.b && a == v.a;576}577578template <class T>579template <class S>580inline bool581Color4<T>::operator != (const Color4<S> &v) const582{583return r != v.r || g != v.g || b != v.b || a != v.a;584}585586template <class T>587inline const Color4<T> &588Color4<T>::operator += (const Color4 &v)589{590r += v.r;591g += v.g;592b += v.b;593a += v.a;594return *this;595}596597template <class T>598inline Color4<T>599Color4<T>::operator + (const Color4 &v) const600{601return Color4 (r + v.r, g + v.g, b + v.b, a + v.a);602}603604template <class T>605inline const Color4<T> &606Color4<T>::operator -= (const Color4 &v)607{608r -= v.r;609g -= v.g;610b -= v.b;611a -= v.a;612return *this;613}614615template <class T>616inline Color4<T>617Color4<T>::operator - (const Color4 &v) const618{619return Color4 (r - v.r, g - v.g, b - v.b, a - v.a);620}621622template <class T>623inline Color4<T>624Color4<T>::operator - () const625{626return Color4 (-r, -g, -b, -a);627}628629template <class T>630inline const Color4<T> &631Color4<T>::negate ()632{633r = -r;634g = -g;635b = -b;636a = -a;637return *this;638}639640template <class T>641inline const Color4<T> &642Color4<T>::operator *= (const Color4 &v)643{644r *= v.r;645g *= v.g;646b *= v.b;647a *= v.a;648return *this;649}650651template <class T>652inline const Color4<T> &653Color4<T>::operator *= (T x)654{655r *= x;656g *= x;657b *= x;658a *= x;659return *this;660}661662template <class T>663inline Color4<T>664Color4<T>::operator * (const Color4 &v) const665{666return Color4 (r * v.r, g * v.g, b * v.b, a * v.a);667}668669template <class T>670inline Color4<T>671Color4<T>::operator * (T x) const672{673return Color4 (r * x, g * x, b * x, a * x);674}675676template <class T>677inline const Color4<T> &678Color4<T>::operator /= (const Color4 &v)679{680r /= v.r;681g /= v.g;682b /= v.b;683a /= v.a;684return *this;685}686687template <class T>688inline const Color4<T> &689Color4<T>::operator /= (T x)690{691r /= x;692g /= x;693b /= x;694a /= x;695return *this;696}697698template <class T>699inline Color4<T>700Color4<T>::operator / (const Color4 &v) const701{702return Color4 (r / v.r, g / v.g, b / v.b, a / v.a);703}704705template <class T>706inline Color4<T>707Color4<T>::operator / (T x) const708{709return Color4 (r / x, g / x, b / x, a / x);710}711712713template <class T>714std::ostream &715operator << (std::ostream &s, const Color4<T> &v)716{717return s << '(' << v.r << ' ' << v.g << ' ' << v.b << ' ' << v.a << ')';718}719720//-----------------------------------------721// Implementation of reverse multiplication722//-----------------------------------------723724template <class S, class T>725inline Color4<T>726operator * (S x, const Color4<T> &v)727{728return Color4<T> (x * v.r, x * v.g, x * v.b, x * v.a);729}730731} // namespace Imath732733#endif734735736