OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreColourValue.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4  (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef _COLOURVALUE_H__
29 #define _COLOURVALUE_H__
30 
31 #include "OgrePrerequisites.h"
32 
33 namespace Ogre {
41  typedef uint32 RGBA;
42  typedef uint32 ARGB;
43  typedef uint32 ABGR;
44  typedef uint32 BGRA;
45 
58  {
59  public:
60  static const ColourValue ZERO;
61  static const ColourValue Black;
62  static const ColourValue White;
63  static const ColourValue Red;
64  static const ColourValue Green;
65  static const ColourValue Blue;
66 
67  explicit ColourValue( float red = 1.0f,
68  float green = 1.0f,
69  float blue = 1.0f,
70  float alpha = 1.0f ) : r(red), g(green), b(blue), a(alpha)
71  { }
72 
73  bool operator==(const ColourValue& rhs) const;
74  bool operator!=(const ColourValue& rhs) const;
75 
76  float r,g,b,a;
77 
80  RGBA getAsRGBA(void) const;
81 
84  ARGB getAsARGB(void) const;
85 
88  BGRA getAsBGRA(void) const;
89 
91  ABGR getAsABGR(void) const;
92 
95  void setAsRGBA(const RGBA val);
96 
99  void setAsARGB(const ARGB val);
100 
103  void setAsBGRA(const BGRA val);
104 
107  void setAsABGR(const ABGR val);
108 
111  void saturate(void)
112  {
113  if (r < 0)
114  r = 0;
115  else if (r > 1)
116  r = 1;
117 
118  if (g < 0)
119  g = 0;
120  else if (g > 1)
121  g = 1;
122 
123  if (b < 0)
124  b = 0;
125  else if (b > 1)
126  b = 1;
127 
128  if (a < 0)
129  a = 0;
130  else if (a > 1)
131  a = 1;
132  }
133 
137  {
138  ColourValue ret = *this;
139  ret.saturate();
140  return ret;
141  }
142 
144  inline float operator [] ( const size_t i ) const
145  {
146  assert( i < 4 );
147 
148  return *(&r+i);
149  }
150 
152  inline float& operator [] ( const size_t i )
153  {
154  assert( i < 4 );
155 
156  return *(&r+i);
157  }
158 
160  inline float* ptr()
161  {
162  return &r;
163  }
165  inline const float* ptr() const
166  {
167  return &r;
168  }
169 
170 
171  // arithmetic operations
172  inline ColourValue operator + ( const ColourValue& rkVector ) const
173  {
174  ColourValue kSum;
175 
176  kSum.r = r + rkVector.r;
177  kSum.g = g + rkVector.g;
178  kSum.b = b + rkVector.b;
179  kSum.a = a + rkVector.a;
180 
181  return kSum;
182  }
183 
184  inline ColourValue operator - ( const ColourValue& rkVector ) const
185  {
186  ColourValue kDiff;
187 
188  kDiff.r = r - rkVector.r;
189  kDiff.g = g - rkVector.g;
190  kDiff.b = b - rkVector.b;
191  kDiff.a = a - rkVector.a;
192 
193  return kDiff;
194  }
195 
196  inline ColourValue operator * (const float fScalar ) const
197  {
198  ColourValue kProd;
199 
200  kProd.r = fScalar*r;
201  kProd.g = fScalar*g;
202  kProd.b = fScalar*b;
203  kProd.a = fScalar*a;
204 
205  return kProd;
206  }
207 
208  inline ColourValue operator * ( const ColourValue& rhs) const
209  {
210  ColourValue kProd;
211 
212  kProd.r = rhs.r * r;
213  kProd.g = rhs.g * g;
214  kProd.b = rhs.b * b;
215  kProd.a = rhs.a * a;
216 
217  return kProd;
218  }
219 
220  inline ColourValue operator / ( const ColourValue& rhs) const
221  {
222  ColourValue kProd;
223 
224  kProd.r = r / rhs.r;
225  kProd.g = g / rhs.g;
226  kProd.b = b / rhs.b;
227  kProd.a = a / rhs.a;
228 
229  return kProd;
230  }
231 
232  inline ColourValue operator / (const float fScalar ) const
233  {
234  assert( fScalar != 0.0 );
235 
236  ColourValue kDiv;
237 
238  float fInv = 1.0f / fScalar;
239  kDiv.r = r * fInv;
240  kDiv.g = g * fInv;
241  kDiv.b = b * fInv;
242  kDiv.a = a * fInv;
243 
244  return kDiv;
245  }
246 
247  inline friend ColourValue operator * (const float fScalar, const ColourValue& rkVector )
248  {
249  ColourValue kProd;
250 
251  kProd.r = fScalar * rkVector.r;
252  kProd.g = fScalar * rkVector.g;
253  kProd.b = fScalar * rkVector.b;
254  kProd.a = fScalar * rkVector.a;
255 
256  return kProd;
257  }
258 
259  // arithmetic updates
260  inline ColourValue& operator += ( const ColourValue& rkVector )
261  {
262  r += rkVector.r;
263  g += rkVector.g;
264  b += rkVector.b;
265  a += rkVector.a;
266 
267  return *this;
268  }
269 
270  inline ColourValue& operator -= ( const ColourValue& rkVector )
271  {
272  r -= rkVector.r;
273  g -= rkVector.g;
274  b -= rkVector.b;
275  a -= rkVector.a;
276 
277  return *this;
278  }
279 
280  inline ColourValue& operator *= (const float fScalar )
281  {
282  r *= fScalar;
283  g *= fScalar;
284  b *= fScalar;
285  a *= fScalar;
286  return *this;
287  }
288 
289  inline ColourValue& operator /= (const float fScalar )
290  {
291  assert( fScalar != 0.0 );
292 
293  float fInv = 1.0f / fScalar;
294 
295  r *= fInv;
296  g *= fInv;
297  b *= fInv;
298  a *= fInv;
299 
300  return *this;
301  }
302 
308  void setHSB(Real hue, Real saturation, Real brightness);
309 
315  void getHSB(Real* hue, Real* saturation, Real* brightness) const;
316 
317 
318 
321  inline _OgreExport friend std::ostream& operator <<
322  ( std::ostream& o, const ColourValue& c )
323  {
324  o << "ColourValue(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
325  return o;
326  }
327 
328  };
332 } // namespace
333 
334 #endif
uint32 ABGR
float Real
Software floating point type.
uint32 ARGB
unsigned int uint32
Definition: OgrePlatform.h:420
#define _OgreExport
Definition: OgrePlatform.h:255
static const ColourValue Green
Class representing colour.
static const ColourValue White
uint32 BGRA
static const ColourValue ZERO
Radian operator*(Real a, const Radian &b)
Definition: OgreMath.h:782
Radian operator/(Real a, const Radian &b)
Definition: OgreMath.h:787
float * ptr()
Pointer accessor for direct copying.
void saturate(void)
Clamps colour value to the range [0, 1].
uint32 RGBA
ColourValue(float red=1.0f, float green=1.0f, float blue=1.0f, float alpha=1.0f)
ColourValue saturateCopy(void) const
As saturate, except that this colour value is unaffected and the saturated colour value is returned a...
static const ColourValue Blue
static const ColourValue Black
static const ColourValue Red
const float * ptr() const
Pointer accessor for direct copying.
bool operator==(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)
bool operator!=(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)