Crazy Eddie's GUI System  0.8.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
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 
75  float getHue(void) const;
76  float getSaturation(void) const;
77  float getLumination(void) const;
78 
79 
80  /*************************************************************************
81  Manipulators
82  *************************************************************************/
83  void setARGB(argb_t argb);
84  inline void setAlpha(float alpha)
85  {
86  d_argbValid = false;
87  d_alpha = alpha;
88  }
89 
90  inline void setRed(float red)
91  {
92  d_argbValid = false;
93  d_red = red;
94  }
95 
96  inline void setGreen(float green)
97  {
98  d_argbValid = false;
99  d_green = green;
100  }
101 
102  inline void setBlue(float blue)
103  {
104  d_argbValid = false;
105  d_blue = blue;
106  }
107 
108  inline void set(float red, float green, float blue, float alpha = 1.0f)
109  {
110  d_argbValid = false;
111  d_alpha = alpha;
112  d_red = red;
113  d_green = green;
114  d_blue = blue;
115  }
116 
117  inline void setRGB(float red, float green, float blue)
118  {
119  d_argbValid = false;
120  d_red = red;
121  d_green = green;
122  d_blue = blue;
123  }
124 
125  inline void setRGB(const Colour& val)
126  {
127  d_red = val.d_red;
128  d_green = val.d_green;
129  d_blue = val.d_blue;
130  if (d_argbValid)
131  {
132  d_argbValid = val.d_argbValid;
133  if (d_argbValid)
134  d_argb = (d_argb & 0xFF000000) | (val.d_argb & 0x00FFFFFF);
135  }
136  }
137 
138  void setHSL(float hue, float saturation, float luminance, float alpha = 1.0f);
139 
140  void invertColour(void);
141  void invertColourWithAlpha(void);
142 
143  /*************************************************************************
144  Operators
145  *************************************************************************/
146  inline Colour& operator=(argb_t val)
147  {
148  setARGB(val);
149  return *this;
150  }
151 
152  inline Colour& operator=(const Colour& val)
153  {
154  d_alpha = val.d_alpha;
155  d_red = val.d_red;
156  d_green = val.d_green;
157  d_blue = val.d_blue;
158  d_argb = val.d_argb;
159  d_argbValid = val.d_argbValid;
160 
161  return *this;
162  }
163 
164  inline Colour& operator&=(argb_t val)
165  {
166  setARGB(getARGB() & val);
167  return *this;
168  }
169 
170  inline Colour& operator&=(const Colour& val)
171  {
172  setARGB(getARGB() & val.getARGB());
173  return *this;
174  }
175 
176  inline Colour& operator|=(argb_t val)
177  {
178  setARGB(getARGB() | val);
179  return *this;
180  }
181 
182  inline Colour& operator|=(const Colour& val)
183  {
184  setARGB(getARGB() | val.getARGB());
185  return *this;
186  }
187 
188  inline Colour& operator<<=(int val)
189  {
190  setARGB(getARGB() << val);
191  return *this;
192  }
193 
194  inline Colour& operator>>=(int val)
195  {
196  setARGB(getARGB() >> val);
197  return *this;
198  }
199 
200  inline Colour operator+(const Colour& val) const
201  {
202  return Colour(
203  d_red + val.d_red,
204  d_green + val.d_green,
205  d_blue + val.d_blue,
206  d_alpha + val.d_alpha
207  );
208  }
209 
210  inline Colour operator-(const Colour& val) const
211  {
212  return Colour(
213  d_red - val.d_red,
214  d_green - val.d_green,
215  d_blue - val.d_blue,
216  d_alpha - val.d_alpha
217  );
218  }
219 
220  inline Colour operator*(const float val) const
221  {
222  return Colour(
223  d_red * val,
224  d_green * val,
225  d_blue * val,
226  d_alpha * val
227  );
228  }
229 
230  inline Colour& operator*=(const Colour& val)
231  {
232  d_red *= val.d_red;
233  d_blue *= val.d_blue;
234  d_green *= val.d_green;
235  d_alpha *= val.d_alpha;
236 
237  d_argbValid = false;
238 
239  return *this;
240  }
241 
242  /*************************************************************************
243  Compare operators
244  *************************************************************************/
245  inline bool operator==(const Colour& rhs) const
246  {
247  return d_red == rhs.d_red &&
248  d_green == rhs.d_green &&
249  d_blue == rhs.d_blue &&
250  d_alpha == rhs.d_alpha;
251  }
252 
253  inline bool operator!=(const Colour& rhs) const
254  {
255  return !(*this == rhs);
256  }
257 
258  //
259  // Conversion operators
260  //
261  operator argb_t() const {return getARGB();}
262 
263 private:
264  /*************************************************************************
265  Implementation Methods
266  *************************************************************************/
271  argb_t calculateARGB(void) const;
272 
273  /*************************************************************************
274  Implementation Data
275  *************************************************************************/
276  float d_alpha, d_red, d_green, d_blue;
277  mutable argb_t d_argb;
278  mutable bool d_argbValid;
279 };
280 
281 } // End of CEGUI namespace section
282 
283 
284 #endif // end of guard _CEGUIColour_h_