[1812] | 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 | Crazy Eddie's GUI System (http://www.cegui.org.uk)
|
---|
| 11 | Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
|
---|
| 12 |
|
---|
| 13 | This library is free software; you can redistribute it and/or
|
---|
| 14 | modify it under the terms of the GNU Lesser General Public
|
---|
| 15 | License as published by the Free Software Foundation; either
|
---|
| 16 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 17 |
|
---|
| 18 | This library is distributed in the hope that it will be useful,
|
---|
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 21 | Lesser General Public License for more details.
|
---|
| 22 |
|
---|
| 23 | You should have received a copy of the GNU Lesser General Public
|
---|
| 24 | License along with this library; if not, write to the Free Software
|
---|
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 26 | *************************************************************************/
|
---|
| 27 | #ifndef _CEGUIcolour_h_
|
---|
| 28 | #define _CEGUIcolour_h_
|
---|
| 29 |
|
---|
| 30 | #include "CEGUIBase.h"
|
---|
| 31 |
|
---|
| 32 | // Start of CEGUI namespace section
|
---|
| 33 | namespace CEGUI
|
---|
| 34 | {
|
---|
| 35 | typedef uint32 argb_t; //!< 32 bit ARGB representation of a colour.
|
---|
| 36 |
|
---|
| 37 | /*!
|
---|
| 38 | \brief
|
---|
| 39 | Class representing colour values within the system.
|
---|
| 40 | */
|
---|
| 41 | class CEGUIEXPORT colour
|
---|
| 42 | {
|
---|
| 43 | public:
|
---|
| 44 | /*************************************************************************
|
---|
| 45 | Construction & Destruction
|
---|
| 46 | *************************************************************************/
|
---|
| 47 | colour(void);
|
---|
| 48 | colour(const colour& val);
|
---|
| 49 | colour(float red, float green, float blue, float alpha = 1.0f);
|
---|
| 50 | colour(argb_t argb);
|
---|
| 51 |
|
---|
| 52 | /*************************************************************************
|
---|
| 53 | Accessors
|
---|
| 54 | *************************************************************************/
|
---|
| 55 | argb_t getARGB(void) const
|
---|
| 56 | {
|
---|
| 57 | if (!d_argbValid)
|
---|
| 58 | {
|
---|
| 59 | d_argb = calculateARGB();
|
---|
| 60 | d_argbValid = true;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | return d_argb;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | float getAlpha(void) const {return d_alpha;}
|
---|
| 67 | float getRed(void) const {return d_red;}
|
---|
| 68 | float getGreen(void) const {return d_green;}
|
---|
| 69 | float getBlue(void) const {return d_blue;}
|
---|
| 70 |
|
---|
| 71 | float getHue(void) const;
|
---|
| 72 | float getSaturation(void) const;
|
---|
| 73 | float getLumination(void) const;
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | /*************************************************************************
|
---|
| 77 | Manipulators
|
---|
| 78 | *************************************************************************/
|
---|
| 79 | void setARGB(argb_t argb);
|
---|
| 80 | inline void setAlpha(float alpha)
|
---|
| 81 | {
|
---|
| 82 | d_argbValid = false;
|
---|
| 83 | d_alpha = alpha;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | inline void setRed(float red)
|
---|
| 87 | {
|
---|
| 88 | d_argbValid = false;
|
---|
| 89 | d_red = red;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | inline void setGreen(float green)
|
---|
| 93 | {
|
---|
| 94 | d_argbValid = false;
|
---|
| 95 | d_green = green;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | inline void setBlue(float blue)
|
---|
| 99 | {
|
---|
| 100 | d_argbValid = false;
|
---|
| 101 | d_blue = blue;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | inline void set(float red, float green, float blue, float alpha = 1.0f)
|
---|
| 105 | {
|
---|
| 106 | d_argbValid = false;
|
---|
| 107 | d_alpha = alpha;
|
---|
| 108 | d_red = red;
|
---|
| 109 | d_green = green;
|
---|
| 110 | d_blue = blue;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | inline void setRGB(float red, float green, float blue)
|
---|
| 114 | {
|
---|
| 115 | d_argbValid = false;
|
---|
| 116 | d_red = red;
|
---|
| 117 | d_green = green;
|
---|
| 118 | d_blue = blue;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | inline void setRGB(const colour& val)
|
---|
| 122 | {
|
---|
| 123 | d_red = val.d_red;
|
---|
| 124 | d_green = val.d_green;
|
---|
| 125 | d_blue = val.d_blue;
|
---|
| 126 | if (d_argbValid)
|
---|
| 127 | {
|
---|
| 128 | d_argbValid = val.d_argbValid;
|
---|
| 129 | if (d_argbValid)
|
---|
| 130 | d_argb = (d_argb & 0xFF000000) | (val.d_argb & 0x00FFFFFF);
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | void setHSL(float hue, float saturation, float luminance, float alpha = 1.0f);
|
---|
| 135 |
|
---|
| 136 | void invertColour(void);
|
---|
| 137 | void invertColourWithAlpha(void);
|
---|
| 138 |
|
---|
| 139 | /*************************************************************************
|
---|
| 140 | Operators
|
---|
| 141 | *************************************************************************/
|
---|
| 142 | inline colour& operator=(argb_t val)
|
---|
| 143 | {
|
---|
| 144 | setARGB(val);
|
---|
| 145 | return *this;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | inline colour& operator=(const colour& val)
|
---|
| 149 | {
|
---|
| 150 | d_alpha = val.d_alpha;
|
---|
| 151 | d_red = val.d_red;
|
---|
| 152 | d_green = val.d_green;
|
---|
| 153 | d_blue = val.d_blue;
|
---|
| 154 | d_argb = val.d_argb;
|
---|
| 155 | d_argbValid = val.d_argbValid;
|
---|
| 156 |
|
---|
| 157 | return *this;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | inline colour& operator&=(argb_t val)
|
---|
| 161 | {
|
---|
| 162 | setARGB(getARGB() & val);
|
---|
| 163 | return *this;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | inline colour& operator&=(const colour& val)
|
---|
| 167 | {
|
---|
| 168 | setARGB(getARGB() & val.getARGB());
|
---|
| 169 | return *this;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | inline colour& operator|=(argb_t val)
|
---|
| 173 | {
|
---|
| 174 | setARGB(getARGB() | val);
|
---|
| 175 | return *this;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | inline colour& operator|=(const colour& val)
|
---|
| 179 | {
|
---|
| 180 | setARGB(getARGB() | val.getARGB());
|
---|
| 181 | return *this;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | inline colour& operator<<=(int val)
|
---|
| 185 | {
|
---|
| 186 | setARGB(getARGB() << val);
|
---|
| 187 | return *this;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | inline colour& operator>>=(int val)
|
---|
| 191 | {
|
---|
| 192 | setARGB(getARGB() >> val);
|
---|
| 193 | return *this;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | inline colour operator+(const colour& val) const
|
---|
| 197 | {
|
---|
| 198 | return colour(
|
---|
| 199 | d_red + val.d_red,
|
---|
| 200 | d_green + val.d_green,
|
---|
| 201 | d_blue + val.d_blue,
|
---|
| 202 | d_alpha + val.d_alpha
|
---|
| 203 | );
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | inline colour operator-(const colour& val) const
|
---|
| 207 | {
|
---|
| 208 | return colour(
|
---|
| 209 | d_red - val.d_red,
|
---|
| 210 | d_green - val.d_green,
|
---|
| 211 | d_blue - val.d_blue,
|
---|
| 212 | d_alpha - val.d_alpha
|
---|
| 213 | );
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | inline colour operator*(const float val) const
|
---|
| 217 | {
|
---|
| 218 | return colour(
|
---|
| 219 | d_red * val,
|
---|
| 220 | d_green * val,
|
---|
| 221 | d_blue * val,
|
---|
| 222 | d_alpha * val
|
---|
| 223 | );
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | inline colour& operator*=(const colour& val)
|
---|
| 227 | {
|
---|
| 228 | d_red *= val.d_red;
|
---|
| 229 | d_blue *= val.d_blue;
|
---|
| 230 | d_green *= val.d_green;
|
---|
| 231 | d_alpha *= val.d_alpha;
|
---|
| 232 |
|
---|
| 233 | d_argbValid = false;
|
---|
| 234 |
|
---|
| 235 | return *this;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | /*************************************************************************
|
---|
| 239 | Compare operators
|
---|
| 240 | *************************************************************************/
|
---|
| 241 | inline bool operator==(const colour& rhs) const
|
---|
| 242 | {
|
---|
| 243 | return d_red == rhs.d_red &&
|
---|
| 244 | d_green == rhs.d_green &&
|
---|
| 245 | d_blue == rhs.d_blue &&
|
---|
| 246 | d_alpha == rhs.d_alpha;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | inline bool operator!=(const colour& rhs) const
|
---|
| 250 | {
|
---|
| 251 | return !(*this == rhs);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | //
|
---|
| 255 | // Conversion operators
|
---|
| 256 | //
|
---|
| 257 | operator argb_t() const {return getARGB();}
|
---|
| 258 |
|
---|
| 259 | private:
|
---|
| 260 | /*************************************************************************
|
---|
| 261 | Implementation Methods
|
---|
| 262 | *************************************************************************/
|
---|
| 263 | /*!
|
---|
| 264 | \brief
|
---|
| 265 | calculate and return the ARGB value based on the current colour component values.
|
---|
| 266 | */
|
---|
| 267 | argb_t calculateARGB(void) const;
|
---|
| 268 |
|
---|
| 269 | /*************************************************************************
|
---|
| 270 | Implementation Data
|
---|
| 271 | *************************************************************************/
|
---|
| 272 | float d_alpha, d_red, d_green, d_blue; //!< Colour components.
|
---|
| 273 | mutable argb_t d_argb; //!< Colour as ARGB value.
|
---|
| 274 | mutable bool d_argbValid; //!< True if argb value is valid.
|
---|
| 275 | };
|
---|
| 276 |
|
---|
| 277 | } // End of CEGUI namespace section
|
---|
| 278 |
|
---|
| 279 |
|
---|
| 280 | #endif // end of guard _CEGUIcolour_h_
|
---|