[5789] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
| 8 | Also see acknowledgements in Readme.html |
---|
| 9 | |
---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
| 13 | version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
| 18 | |
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
| 23 | |
---|
| 24 | You may alternatively use this source under the terms of a specific version of |
---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
| 26 | Torus Knot Software Ltd. |
---|
| 27 | ----------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | #ifndef _COLOURVALUE_H__ |
---|
| 30 | #define _COLOURVALUE_H__ |
---|
| 31 | |
---|
| 32 | #include "OgrePrerequisites.h" |
---|
| 33 | |
---|
| 34 | #include <cassert> |
---|
| 35 | #include <ostream> |
---|
| 36 | |
---|
| 37 | namespace Ogre { |
---|
| 38 | |
---|
| 39 | typedef uint32 RGBA; |
---|
| 40 | typedef uint32 ARGB; |
---|
| 41 | typedef uint32 ABGR; |
---|
| 42 | typedef uint32 BGRA; |
---|
| 43 | |
---|
| 44 | /** Class representing colour. |
---|
| 45 | @remarks |
---|
| 46 | Colour is represented as 4 components, each of which is a |
---|
| 47 | floating-point value from 0.0 to 1.0. |
---|
| 48 | @par |
---|
| 49 | The 3 'normal' colour components are red, green and blue, a higher |
---|
| 50 | number indicating greater amounts of that component in the colour. |
---|
| 51 | The forth component is the 'alpha' value, which represents |
---|
| 52 | transparency. In this case, 0.0 is completely transparent and 1.0 is |
---|
| 53 | fully opaque. |
---|
| 54 | */ |
---|
| 55 | class _OgreExport ColourValue |
---|
| 56 | { |
---|
| 57 | public: |
---|
| 58 | static const ColourValue ZERO; |
---|
| 59 | static const ColourValue Black; |
---|
| 60 | static const ColourValue White; |
---|
| 61 | static const ColourValue Red; |
---|
| 62 | static const ColourValue Green; |
---|
| 63 | static const ColourValue Blue; |
---|
| 64 | |
---|
| 65 | explicit ColourValue( float red = 1.0f, |
---|
| 66 | float green = 1.0f, |
---|
| 67 | float blue = 1.0f, |
---|
| 68 | float alpha = 1.0f ) : r(red), g(green), b(blue), a(alpha) |
---|
| 69 | { } |
---|
| 70 | |
---|
| 71 | bool operator==(const ColourValue& rhs) const; |
---|
| 72 | bool operator!=(const ColourValue& rhs) const; |
---|
| 73 | |
---|
| 74 | float r,g,b,a; |
---|
| 75 | |
---|
| 76 | /** Retrieves colour as RGBA. |
---|
| 77 | */ |
---|
| 78 | RGBA getAsRGBA(void) const; |
---|
| 79 | |
---|
| 80 | /** Retrieves colour as ARGB. |
---|
| 81 | */ |
---|
| 82 | ARGB getAsARGB(void) const; |
---|
| 83 | |
---|
| 84 | /** Retrieves colour as BGRA. |
---|
| 85 | */ |
---|
| 86 | BGRA getAsBGRA(void) const; |
---|
| 87 | |
---|
| 88 | /** Retrieves colours as ABGR */ |
---|
| 89 | ABGR getAsABGR(void) const; |
---|
| 90 | |
---|
| 91 | /** Sets colour as RGBA. |
---|
| 92 | */ |
---|
| 93 | void setAsRGBA(const RGBA val); |
---|
| 94 | |
---|
| 95 | /** Sets colour as ARGB. |
---|
| 96 | */ |
---|
| 97 | void setAsARGB(const ARGB val); |
---|
| 98 | |
---|
| 99 | /** Sets colour as BGRA. |
---|
| 100 | */ |
---|
| 101 | void setAsBGRA(const BGRA val); |
---|
| 102 | |
---|
| 103 | /** Sets colour as ABGR. |
---|
| 104 | */ |
---|
| 105 | void setAsABGR(const ABGR val); |
---|
| 106 | |
---|
| 107 | /** Clamps colour value to the range [0, 1]. |
---|
| 108 | */ |
---|
| 109 | void saturate(void) |
---|
| 110 | { |
---|
| 111 | if (r < 0) |
---|
| 112 | r = 0; |
---|
| 113 | else if (r > 1) |
---|
| 114 | r = 1; |
---|
| 115 | |
---|
| 116 | if (g < 0) |
---|
| 117 | g = 0; |
---|
| 118 | else if (g > 1) |
---|
| 119 | g = 1; |
---|
| 120 | |
---|
| 121 | if (b < 0) |
---|
| 122 | b = 0; |
---|
| 123 | else if (b > 1) |
---|
| 124 | b = 1; |
---|
| 125 | |
---|
| 126 | if (a < 0) |
---|
| 127 | a = 0; |
---|
| 128 | else if (a > 1) |
---|
| 129 | a = 1; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /** As saturate, except that this colour value is unaffected and |
---|
| 133 | the saturated colour value is returned as a copy. */ |
---|
| 134 | ColourValue saturateCopy(void) const |
---|
| 135 | { |
---|
| 136 | ColourValue ret = *this; |
---|
| 137 | ret.saturate(); |
---|
| 138 | return ret; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /// Array accessor operator |
---|
| 142 | inline float operator [] ( const size_t i ) const |
---|
| 143 | { |
---|
| 144 | assert( i < 4 ); |
---|
| 145 | |
---|
| 146 | return *(&r+i); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /// Array accessor operator |
---|
| 150 | inline float& operator [] ( const size_t i ) |
---|
| 151 | { |
---|
| 152 | assert( i < 4 ); |
---|
| 153 | |
---|
| 154 | return *(&r+i); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /// Pointer accessor for direct copying |
---|
| 158 | inline float* ptr() |
---|
| 159 | { |
---|
| 160 | return &r; |
---|
| 161 | } |
---|
| 162 | /// Pointer accessor for direct copying |
---|
| 163 | inline const float* ptr() const |
---|
| 164 | { |
---|
| 165 | return &r; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | // arithmetic operations |
---|
| 170 | inline ColourValue operator + ( const ColourValue& rkVector ) const |
---|
| 171 | { |
---|
| 172 | ColourValue kSum; |
---|
| 173 | |
---|
| 174 | kSum.r = r + rkVector.r; |
---|
| 175 | kSum.g = g + rkVector.g; |
---|
| 176 | kSum.b = b + rkVector.b; |
---|
| 177 | kSum.a = a + rkVector.a; |
---|
| 178 | |
---|
| 179 | return kSum; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | inline ColourValue operator - ( const ColourValue& rkVector ) const |
---|
| 183 | { |
---|
| 184 | ColourValue kDiff; |
---|
| 185 | |
---|
| 186 | kDiff.r = r - rkVector.r; |
---|
| 187 | kDiff.g = g - rkVector.g; |
---|
| 188 | kDiff.b = b - rkVector.b; |
---|
| 189 | kDiff.a = a - rkVector.a; |
---|
| 190 | |
---|
| 191 | return kDiff; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | inline ColourValue operator * (const float fScalar ) const |
---|
| 195 | { |
---|
| 196 | ColourValue kProd; |
---|
| 197 | |
---|
| 198 | kProd.r = fScalar*r; |
---|
| 199 | kProd.g = fScalar*g; |
---|
| 200 | kProd.b = fScalar*b; |
---|
| 201 | kProd.a = fScalar*a; |
---|
| 202 | |
---|
| 203 | return kProd; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | inline ColourValue operator * ( const ColourValue& rhs) const |
---|
| 207 | { |
---|
| 208 | ColourValue kProd; |
---|
| 209 | |
---|
| 210 | kProd.r = rhs.r * r; |
---|
| 211 | kProd.g = rhs.g * g; |
---|
| 212 | kProd.b = rhs.b * b; |
---|
| 213 | kProd.a = rhs.a * a; |
---|
| 214 | |
---|
| 215 | return kProd; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | inline ColourValue operator / ( const ColourValue& rhs) const |
---|
| 219 | { |
---|
| 220 | ColourValue kProd; |
---|
| 221 | |
---|
| 222 | kProd.r = rhs.r / r; |
---|
| 223 | kProd.g = rhs.g / g; |
---|
| 224 | kProd.b = rhs.b / b; |
---|
| 225 | kProd.a = rhs.a / a; |
---|
| 226 | |
---|
| 227 | return kProd; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | inline ColourValue operator / (const float fScalar ) const |
---|
| 231 | { |
---|
| 232 | assert( fScalar != 0.0 ); |
---|
| 233 | |
---|
| 234 | ColourValue kDiv; |
---|
| 235 | |
---|
| 236 | float fInv = 1.0 / fScalar; |
---|
| 237 | kDiv.r = r * fInv; |
---|
| 238 | kDiv.g = g * fInv; |
---|
| 239 | kDiv.b = b * fInv; |
---|
| 240 | kDiv.a = a * fInv; |
---|
| 241 | |
---|
| 242 | return kDiv; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | inline friend ColourValue operator * (const float fScalar, const ColourValue& rkVector ) |
---|
| 246 | { |
---|
| 247 | ColourValue kProd; |
---|
| 248 | |
---|
| 249 | kProd.r = fScalar * rkVector.r; |
---|
| 250 | kProd.g = fScalar * rkVector.g; |
---|
| 251 | kProd.b = fScalar * rkVector.b; |
---|
| 252 | kProd.a = fScalar * rkVector.a; |
---|
| 253 | |
---|
| 254 | return kProd; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | // arithmetic updates |
---|
| 258 | inline ColourValue& operator += ( const ColourValue& rkVector ) |
---|
| 259 | { |
---|
| 260 | r += rkVector.r; |
---|
| 261 | g += rkVector.g; |
---|
| 262 | b += rkVector.b; |
---|
| 263 | a += rkVector.a; |
---|
| 264 | |
---|
| 265 | return *this; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | inline ColourValue& operator -= ( const ColourValue& rkVector ) |
---|
| 269 | { |
---|
| 270 | r -= rkVector.r; |
---|
| 271 | g -= rkVector.g; |
---|
| 272 | b -= rkVector.b; |
---|
| 273 | a -= rkVector.a; |
---|
| 274 | |
---|
| 275 | return *this; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | inline ColourValue& operator *= (const float fScalar ) |
---|
| 279 | { |
---|
| 280 | r *= fScalar; |
---|
| 281 | g *= fScalar; |
---|
| 282 | b *= fScalar; |
---|
| 283 | a *= fScalar; |
---|
| 284 | return *this; |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | inline ColourValue& operator /= (const float fScalar ) |
---|
| 288 | { |
---|
| 289 | assert( fScalar != 0.0 ); |
---|
| 290 | |
---|
| 291 | float fInv = 1.0 / fScalar; |
---|
| 292 | |
---|
| 293 | r *= fInv; |
---|
| 294 | g *= fInv; |
---|
| 295 | b *= fInv; |
---|
| 296 | a *= fInv; |
---|
| 297 | |
---|
| 298 | return *this; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | /** Set a colour value from Hue, Saturation and Brightness. |
---|
| 302 | @param hue Hue value, scaled to the [0,1] range as opposed to the 0-360 |
---|
| 303 | @param saturation Saturation level, [0,1] |
---|
| 304 | @param brightness Brightness level, [0,1] |
---|
| 305 | */ |
---|
| 306 | void setHSB(Real hue, Real saturation, Real brightness); |
---|
| 307 | |
---|
| 308 | /** Convert the current colour to Hue, Saturation and Brightness values. |
---|
| 309 | @param hue Output hue value, scaled to the [0,1] range as opposed to the 0-360 |
---|
| 310 | @param saturation Output saturation level, [0,1] |
---|
| 311 | @param brightness Output brightness level, [0,1] |
---|
| 312 | */ |
---|
| 313 | void getHSB(Real* hue, Real* saturation, Real* brightness) const; |
---|
| 314 | |
---|
| 315 | |
---|
| 316 | |
---|
| 317 | /** Function for writing to a stream. |
---|
| 318 | */ |
---|
| 319 | inline _OgreExport friend std::ostream& operator << |
---|
| 320 | ( std::ostream& o, const ColourValue& c ) |
---|
| 321 | { |
---|
| 322 | o << "ColourValue(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")"; |
---|
| 323 | return o; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | }; |
---|
| 327 | |
---|
| 328 | } // namespace |
---|
| 329 | |
---|
| 330 | #endif |
---|