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