Crazy Eddie's GUI System  0.8.7
Colour.h
1 /***********************************************************************
2  created: 20/8/2004
3  author: Paul D Turner (with code from Jeff Leigh)
4 
5  purpose: Defines interface to the colour class used to represent
6  colour values within the system
7 *************************************************************************/
8 /***************************************************************************
9  * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining
12  * a copy of this software and associated documentation files (the
13  * "Software"), to deal in the Software without restriction, including
14  * without limitation the rights to use, copy, modify, merge, publish,
15  * distribute, sublicense, and/or sell copies of the Software, and to
16  * permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  ***************************************************************************/
30 #ifndef _CEGUIColour_h_
31 #define _CEGUIColour_h_
32 
33 #include "CEGUI/Base.h"
34 
35 // Start of CEGUI namespace section
36 namespace CEGUI
37 {
38 typedef uint32 argb_t;
39 
44 class CEGUIEXPORT Colour :
45  public AllocatedObject<Colour>
46 {
47 public:
48  /*************************************************************************
49  Construction & Destruction
50  *************************************************************************/
51  Colour(void);
52  Colour(const Colour& val);
53  Colour(float red, float green, float blue, float alpha = 1.0f);
54  Colour(argb_t argb);
55 
56  /*************************************************************************
57  Accessors
58  *************************************************************************/
59  argb_t getARGB(void) const
60  {
61  if (!d_argbValid)
62  {
63  d_argb = calculateARGB();
64  d_argbValid = true;
65  }
66 
67  return d_argb;
68  }
69 
70  float getAlpha(void) const {return d_alpha;}
71  float getRed(void) const {return d_red;}
72  float getGreen(void) const {return d_green;}
73  float getBlue(void) const {return d_blue;}
74 
82  float getHue(void) const;
83 
91  float getSaturation(void) const;
92 
100  float getLumination(void) const;
101 
102 
103  /*************************************************************************
104  Manipulators
105  *************************************************************************/
106  void setARGB(argb_t argb);
107  inline void setAlpha(float alpha)
108  {
109  d_argbValid = false;
110  d_alpha = alpha;
111  }
112 
113  inline void setRed(float red)
114  {
115  d_argbValid = false;
116  d_red = red;
117  }
118 
119  inline void setGreen(float green)
120  {
121  d_argbValid = false;
122  d_green = green;
123  }
124 
125  inline void setBlue(float blue)
126  {
127  d_argbValid = false;
128  d_blue = blue;
129  }
130 
131  inline void set(float red, float green, float blue, float alpha = 1.0f)
132  {
133  d_argbValid = false;
134  d_alpha = alpha;
135  d_red = red;
136  d_green = green;
137  d_blue = blue;
138  }
139 
140  inline void setRGB(float red, float green, float blue)
141  {
142  d_argbValid = false;
143  d_red = red;
144  d_green = green;
145  d_blue = blue;
146  }
147 
148  inline void setRGB(const Colour& val)
149  {
150  d_red = val.d_red;
151  d_green = val.d_green;
152  d_blue = val.d_blue;
153  if (d_argbValid)
154  {
155  d_argbValid = val.d_argbValid;
156  if (d_argbValid)
157  d_argb = (d_argb & 0xFF000000) | (val.d_argb & 0x00FFFFFF);
158  }
159  }
160 
161  void setHSL(float hue, float saturation, float luminance, float alpha = 1.0f);
162 
163  void invertColour(void);
164  void invertColourWithAlpha(void);
165 
166  /*************************************************************************
167  Operators
168  *************************************************************************/
169  inline Colour& operator=(argb_t val)
170  {
171  setARGB(val);
172  return *this;
173  }
174 
175  inline Colour& operator=(const Colour& val)
176  {
177  d_alpha = val.d_alpha;
178  d_red = val.d_red;
179  d_green = val.d_green;
180  d_blue = val.d_blue;
181  d_argb = val.d_argb;
182  d_argbValid = val.d_argbValid;
183 
184  return *this;
185  }
186 
187  inline Colour& operator&=(argb_t val)
188  {
189  setARGB(getARGB() & val);
190  return *this;
191  }
192 
193  inline Colour& operator&=(const Colour& val)
194  {
195  setARGB(getARGB() & val.getARGB());
196  return *this;
197  }
198 
199  inline Colour& operator|=(argb_t val)
200  {
201  setARGB(getARGB() | val);
202  return *this;
203  }
204 
205  inline Colour& operator|=(const Colour& val)
206  {
207  setARGB(getARGB() | val.getARGB());
208  return *this;
209  }
210 
211  inline Colour& operator<<=(int val)
212  {
213  setARGB(getARGB() << val);
214  return *this;
215  }
216 
217  inline Colour& operator>>=(int val)
218  {
219  setARGB(getARGB() >> val);
220  return *this;
221  }
222 
223  inline Colour operator+(const Colour& val) const
224  {
225  return Colour(
226  d_red + val.d_red,
227  d_green + val.d_green,
228  d_blue + val.d_blue,
229  d_alpha + val.d_alpha
230  );
231  }
232 
233  inline Colour operator-(const Colour& val) const
234  {
235  return Colour(
236  d_red - val.d_red,
237  d_green - val.d_green,
238  d_blue - val.d_blue,
239  d_alpha - val.d_alpha
240  );
241  }
242 
243  inline Colour operator*(const float val) const
244  {
245  return Colour(
246  d_red * val,
247  d_green * val,
248  d_blue * val,
249  d_alpha * val
250  );
251  }
252 
253  inline Colour& operator*=(const Colour& val)
254  {
255  d_red *= val.d_red;
256  d_blue *= val.d_blue;
257  d_green *= val.d_green;
258  d_alpha *= val.d_alpha;
259 
260  d_argbValid = false;
261 
262  return *this;
263  }
264 
265  /*************************************************************************
266  Compare operators
267  *************************************************************************/
268  inline bool operator==(const Colour& rhs) const
269  {
270  return d_red == rhs.d_red &&
271  d_green == rhs.d_green &&
272  d_blue == rhs.d_blue &&
273  d_alpha == rhs.d_alpha;
274  }
275 
276  inline bool operator!=(const Colour& rhs) const
277  {
278  return !(*this == rhs);
279  }
280 
281  //
282  // Conversion operators
283  //
284  operator argb_t() const {return getARGB();}
285 
286 private:
287  /*************************************************************************
288  Implementation Methods
289  *************************************************************************/
294  argb_t calculateARGB(void) const;
295 
296  /*************************************************************************
297  Implementation Data
298  *************************************************************************/
299  float d_alpha, d_red, d_green, d_blue;
300  mutable argb_t d_argb;
301  mutable bool d_argbValid;
302 };
303 
304 } // End of CEGUI namespace section
305 
306 
307 #endif // end of guard _CEGUIColour_h_
Definition: MemoryAllocatedObject.h:109
bool CEGUIEXPORT operator==(const String &str1, const String &str2)
Return true if String str1 is equal to String str2.
Main namespace for Crazy Eddie's GUI Library.
Definition: arch_overview.dox:1
uint32 argb_t
32 bit ARGB representation of a colour.
Definition: Colour.h:38
String CEGUIEXPORT operator+(const String &str1, const String &str2)
Return String object that is the concatenation of the given inputs.
Class representing colour values within the system.
Definition: Colour.h:44
bool CEGUIEXPORT operator!=(const String &str1, const String &str2)
Return true if String str1 is not equal to String str2.