# OgreColourValue.h

Go to the documentation of this file.

    00001 /*
    00002 -----------------------------------------------------------------------------
    00003 This source file is part of OGRE
    00004     (Object-oriented Graphics Rendering Engine)
    00005 For the latest info, see http://www.ogre3d.org/
    00006
    00007 Copyright (c) 2000-2009 Torus Knot Software Ltd
    00008
    00009 Permission is hereby granted, free of charge, to any person obtaining a copy
    00010 of this software and associated documentation files (the "Software"), to deal
    00011 in the Software without restriction, including without limitation the rights
    00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00013 copies of the Software, and to permit persons to whom the Software is
    00014 furnished to do so, subject to the following conditions:
    00015
    00016 The above copyright notice and this permission notice shall be included in
    00017 all copies or substantial portions of the Software.
    00018
    00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00025 THE SOFTWARE.
    00026 -----------------------------------------------------------------------------
    00027 */
    00028 #ifndef _COLOURVALUE_H__
    00029 #define _COLOURVALUE_H__
    00030
    00031 #include "OgrePrerequisites.h"
    00032
    00033 namespace Ogre {
    00041     typedef uint32 RGBA;
    00042     typedef uint32 ARGB;
    00043     typedef uint32 ABGR;
    00044     typedef uint32 BGRA;
    00045
    00057     class _OgreExport ColourValue
    00058     {
    00059     public:
    00060         static const ColourValue ZERO;
    00061         static const ColourValue Black;
    00062         static const ColourValue White;
    00063         static const ColourValue Red;
    00064         static const ColourValue Green;
    00065         static const ColourValue Blue;
    00066
    00067         explicit ColourValue( float red = 1.0f,
    00068                     float green = 1.0f,
    00069                     float blue = 1.0f,
    00070                     float alpha = 1.0f ) : r(red), g(green), b(blue), a(alpha)
    00071         { }
    00072
    00073         bool operator==(const ColourValue& rhs) const;
    00074         bool operator!=(const ColourValue& rhs) const;
    00075
    00076         float r,g,b,a;
    00077
    00080         RGBA getAsRGBA(void) const;
    00081
    00084         ARGB getAsARGB(void) const;
    00085
    00088         BGRA getAsBGRA(void) const;
    00089
    00091         ABGR getAsABGR(void) const;
    00092
    00095         void setAsRGBA(const RGBA val);
    00096
    00099         void setAsARGB(const ARGB val);
    00100
    00103         void setAsBGRA(const BGRA val);
    00104
    00107         void setAsABGR(const ABGR val);
    00108
    00111         void saturate(void)
    00112         {
    00113             if (r < 0)
    00114                 r = 0;
    00115             else if (r > 1)
    00116                 r = 1;
    00117
    00118             if (g < 0)
    00119                 g = 0;
    00120             else if (g > 1)
    00121                 g = 1;
    00122
    00123             if (b < 0)
    00124                 b = 0;
    00125             else if (b > 1)
    00126                 b = 1;
    00127
    00128             if (a < 0)
    00129                 a = 0;
    00130             else if (a > 1)
    00131                 a = 1;
    00132         }
    00133
    00136         ColourValue saturateCopy(void) const
    00137         {
    00138             ColourValue ret = *this;
    00139             ret.saturate();
    00140             return ret;
    00141         }
    00142
    00144         inline float operator [] ( const size_t i ) const
    00145         {
    00146             assert( i < 4 );
    00147
    00148             return *(&r+i);
    00149         }
    00150
    00152         inline float& operator [] ( const size_t i )
    00153         {
    00154             assert( i < 4 );
    00155
    00156             return *(&r+i);
    00157         }
    00158
    00160         inline float* ptr()
    00161         {
    00162             return &r;
    00163         }
    00165         inline const float* ptr() const
    00166         {
    00167             return &r;
    00168         }
    00169
    00170
    00171         // arithmetic operations
    00172         inline ColourValue operator + ( const ColourValue& rkVector ) const
    00173         {
    00174             ColourValue kSum;
    00175
    00176             kSum.r = r + rkVector.r;
    00177             kSum.g = g + rkVector.g;
    00178             kSum.b = b + rkVector.b;
    00179             kSum.a = a + rkVector.a;
    00180
    00181             return kSum;
    00182         }
    00183
    00184         inline ColourValue operator - ( const ColourValue& rkVector ) const
    00185         {
    00186             ColourValue kDiff;
    00187
    00188             kDiff.r = r - rkVector.r;
    00189             kDiff.g = g - rkVector.g;
    00190             kDiff.b = b - rkVector.b;
    00191             kDiff.a = a - rkVector.a;
    00192
    00193             return kDiff;
    00194         }
    00195
    00196         inline ColourValue operator * (const float fScalar ) const
    00197         {
    00198             ColourValue kProd;
    00199
    00200             kProd.r = fScalar*r;
    00201             kProd.g = fScalar*g;
    00202             kProd.b = fScalar*b;
    00203             kProd.a = fScalar*a;
    00204
    00205             return kProd;
    00206         }
    00207
    00208         inline ColourValue operator * ( const ColourValue& rhs) const
    00209         {
    00210             ColourValue kProd;
    00211
    00212             kProd.r = rhs.r * r;
    00213             kProd.g = rhs.g * g;
    00214             kProd.b = rhs.b * b;
    00215             kProd.a = rhs.a * a;
    00216
    00217             return kProd;
    00218         }
    00219
    00220         inline ColourValue operator / ( const ColourValue& rhs) const
    00221         {
    00222             ColourValue kProd;
    00223
    00224             kProd.r = rhs.r / r;
    00225             kProd.g = rhs.g / g;
    00226             kProd.b = rhs.b / b;
    00227             kProd.a = rhs.a / a;
    00228
    00229             return kProd;
    00230         }
    00231
    00232         inline ColourValue operator / (const float fScalar ) const
    00233         {
    00234             assert( fScalar != 0.0 );
    00235
    00236             ColourValue kDiv;
    00237
    00238             float fInv = 1.0f / fScalar;
    00239             kDiv.r = r * fInv;
    00240             kDiv.g = g * fInv;
    00241             kDiv.b = b * fInv;
    00242             kDiv.a = a * fInv;
    00243
    00244             return kDiv;
    00245         }
    00246
    00247         inline friend ColourValue operator * (const float fScalar, const ColourValue& rkVector )
    00248         {
    00249             ColourValue kProd;
    00250
    00251             kProd.r = fScalar * rkVector.r;
    00252             kProd.g = fScalar * rkVector.g;
    00253             kProd.b = fScalar * rkVector.b;
    00254             kProd.a = fScalar * rkVector.a;
    00255
    00256             return kProd;
    00257         }
    00258
    00259         // arithmetic updates
    00260         inline ColourValue& operator += ( const ColourValue& rkVector )
    00261         {
    00262             r += rkVector.r;
    00263             g += rkVector.g;
    00264             b += rkVector.b;
    00265             a += rkVector.a;
    00266
    00267             return *this;
    00268         }
    00269
    00270         inline ColourValue& operator -= ( const ColourValue& rkVector )
    00271         {
    00272             r -= rkVector.r;
    00273             g -= rkVector.g;
    00274             b -= rkVector.b;
    00275             a -= rkVector.a;
    00276
    00277             return *this;
    00278         }
    00279
    00280         inline ColourValue& operator *= (const float fScalar )
    00281         {
    00282             r *= fScalar;
    00283             g *= fScalar;
    00284             b *= fScalar;
    00285             a *= fScalar;
    00286             return *this;
    00287         }
    00288
    00289         inline ColourValue& operator /= (const float fScalar )
    00290         {
    00291             assert( fScalar != 0.0 );
    00292
    00293             float fInv = 1.0f / fScalar;
    00294
    00295             r *= fInv;
    00296             g *= fInv;
    00297             b *= fInv;
    00298             a *= fInv;
    00299
    00300             return *this;
    00301         }
    00302
    00308         void setHSB(Real hue, Real saturation, Real brightness);
    00309
    00315         void getHSB(Real* hue, Real* saturation, Real* brightness) const;
    00316
    00317
    00318
    00321         inline _OgreExport friend std::ostream& operator <<
    00322             ( std::ostream& o, const ColourValue& c )
    00323         {
    00324             o << "ColourValue(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
    00325             return o;
    00326         }
    00327
    00328     };
    00332 } // namespace
    00333
    00334 #endif

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun May 8 2011 20:28:08