source: GTP/trunk/App/Demos/Geom/OgreStuff/include/OgreColourValue.h @ 1812

Revision 1812, 7.7 KB checked in by gumbau, 18 years ago (diff)
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#ifndef _COLOURVALUE_H__
26#define _COLOURVALUE_H__
27
28#include "OgrePrerequisites.h"
29
30namespace Ogre {
31
32    typedef uint32 RGBA;
33    typedef uint32 ARGB;
34    typedef uint32 ABGR;
35        typedef uint32 BGRA;
36
37    /** Class representing colour.
38            @remarks
39                    Colour is represented as 4 components, each of which is a
40                    floating-point value from 0.0 to 1.0.
41            @par
42                    The 3 'normal' colour components are red, green and blue, a higher
43                    number indicating greater amounts of that component in the colour.
44                    The forth component is the 'alpha' value, which represents
45                    transparency. In this case, 0.0 is completely transparent and 1.0 is
46                    fully opaque.
47    */
48    class _OgreExport ColourValue
49    {
50    public:
51        static const ColourValue ZERO;
52        static const ColourValue Black;
53        static const ColourValue White;
54        static const ColourValue Red;
55        static const ColourValue Green;
56        static const ColourValue Blue;
57
58            explicit ColourValue( float red = 1.0f,
59                                    float green = 1.0f,
60                                    float blue = 1.0f,
61                                    float alpha = 1.0f ) : r(red), g(green), b(blue), a(alpha)
62        { }
63
64            bool operator==(const ColourValue& rhs) const;
65            bool operator!=(const ColourValue& rhs) const;
66
67        union {
68            struct {
69                    float r,g,b,a;
70            };
71            float val[4];
72        };
73
74            /** Retrieves colour as RGBA.
75            */
76            RGBA getAsRGBA(void) const;
77
78            /** Retrieves colour as ARGB.
79            */
80            ARGB getAsARGB(void) const;
81
82                /** Retrieves colour as BGRA.
83                */
84                BGRA getAsBGRA(void) const;
85
86                /** Retrieves colours as ABGR */
87            ABGR getAsABGR(void) const;
88
89            /** Sets colour as RGBA.
90            */
91        void setAsRGBA(const RGBA val);
92
93            /** Sets colour as ARGB.
94            */
95        void setAsARGB(const ARGB val);
96
97                /** Sets colour as BGRA.
98                */
99                void setAsBGRA(const BGRA val);
100
101            /** Sets colour as ABGR.
102            */
103        void setAsABGR(const ABGR val);
104
105        /** Clamps colour value to the range [0, 1].
106        */
107        void saturate(void)
108        {
109            if (r < 0)
110                r = 0;
111            else if (r > 1)
112                r = 1;
113
114            if (g < 0)
115                g = 0;
116            else if (g > 1)
117                g = 1;
118
119            if (b < 0)
120                b = 0;
121            else if (b > 1)
122                b = 1;
123
124            if (a < 0)
125                a = 0;
126            else if (a > 1)
127                a = 1;
128        }
129
130        /** As saturate, except that this colour value is unaffected and
131            the saturated colour value is returned as a copy. */
132        ColourValue saturateCopy(void) const
133        {
134            ColourValue ret = *this;
135            ret.saturate();
136            return ret;
137        }
138
139        // arithmetic operations
140        inline ColourValue operator + ( const ColourValue& rkVector ) const
141        {
142            ColourValue kSum;
143
144            kSum.r = r + rkVector.r;
145            kSum.g = g + rkVector.g;
146            kSum.b = b + rkVector.b;
147            kSum.a = a + rkVector.a;
148
149            return kSum;
150        }
151
152        inline ColourValue operator - ( const ColourValue& rkVector ) const
153        {
154            ColourValue kDiff;
155
156            kDiff.r = r - rkVector.r;
157            kDiff.g = g - rkVector.g;
158            kDiff.b = b - rkVector.b;
159            kDiff.a = a - rkVector.a;
160
161            return kDiff;
162        }
163
164        inline ColourValue operator * (const float fScalar ) const
165        {
166            ColourValue kProd;
167
168            kProd.r = fScalar*r;
169            kProd.g = fScalar*g;
170            kProd.b = fScalar*b;
171            kProd.a = fScalar*a;
172
173            return kProd;
174        }
175
176        inline ColourValue operator * ( const ColourValue& rhs) const
177        {
178            ColourValue kProd;
179
180            kProd.r = rhs.r * r;
181            kProd.g = rhs.g * g;
182            kProd.b = rhs.b * b;
183            kProd.a = rhs.a * a;
184
185            return kProd;
186        }
187
188        inline ColourValue operator / ( const ColourValue& rhs) const
189        {
190            ColourValue kProd;
191
192            kProd.r = rhs.r / r;
193            kProd.g = rhs.g / g;
194            kProd.b = rhs.b / b;
195            kProd.a = rhs.a / a;
196
197            return kProd;
198        }
199
200        inline ColourValue operator / (const float fScalar ) const
201        {
202            assert( fScalar != 0.0 );
203
204            ColourValue kDiv;
205
206            float fInv = 1.0 / fScalar;
207            kDiv.r = r * fInv;
208            kDiv.g = g * fInv;
209            kDiv.b = b * fInv;
210            kDiv.a = a * fInv;
211
212            return kDiv;
213        }
214
215        inline friend ColourValue operator * (const float fScalar, const ColourValue& rkVector )
216        {
217            ColourValue kProd;
218
219            kProd.r = fScalar * rkVector.r;
220            kProd.g = fScalar * rkVector.g;
221            kProd.b = fScalar * rkVector.b;
222            kProd.a = fScalar * rkVector.a;
223
224            return kProd;
225        }
226
227        // arithmetic updates
228        inline ColourValue& operator += ( const ColourValue& rkVector )
229        {
230            r += rkVector.r;
231            g += rkVector.g;
232            b += rkVector.b;
233            a += rkVector.a;
234
235            return *this;
236        }
237
238        inline ColourValue& operator -= ( const ColourValue& rkVector )
239        {
240            r -= rkVector.r;
241            g -= rkVector.g;
242            b -= rkVector.b;
243            a -= rkVector.a;
244
245            return *this;
246        }
247
248        inline ColourValue& operator *= (const float fScalar )
249        {
250            r *= fScalar;
251            g *= fScalar;
252            b *= fScalar;
253            a *= fScalar;
254            return *this;
255        }
256
257        inline ColourValue& operator /= (const float fScalar )
258        {
259            assert( fScalar != 0.0 );
260
261            float fInv = 1.0 / fScalar;
262
263            r *= fInv;
264            g *= fInv;
265            b *= fInv;
266            a *= fInv;
267
268            return *this;
269        }
270
271                /** Set a colour value from Hue, Saturation and Brightness.
272                @param hue Hue value, scaled to the [0,1] range as opposed to the 0-360
273                @param saturation Saturation level, [0,1]
274                @param brightness Brightness level, [0,1]
275                */
276                void setHSB(Real hue, Real saturation, Real brightness);
277
278
279                /** Function for writing to a stream.
280                */
281                inline _OgreExport friend std::ostream& operator <<
282                        ( std::ostream& o, const ColourValue& c )
283                {
284                        o << "ColourValue(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
285                        return o;
286                }
287
288    };
289
290} // namespace
291
292#endif
Note: See TracBrowser for help on using the repository browser.