Crazy Eddies GUI System  0.7.0
CEGUIcolour.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 "CEGUIBase.h"
35 
36 // Start of CEGUI namespace section
37 namespace CEGUI
38 {
39 typedef uint32 argb_t;
40 
45 class CEGUIEXPORT 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_