[1] | 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 | namespace Ogre { |
---|
| 35 | |
---|
| 36 | typedef uint32 RGBA; |
---|
| 37 | typedef uint32 ARGB; |
---|
| 38 | typedef uint32 ABGR; |
---|
| 39 | typedef uint32 BGRA; |
---|
| 40 | |
---|
| 41 | /** Class representing colour. |
---|
| 42 | @remarks |
---|
| 43 | Colour is represented as 4 components, each of which is a |
---|
| 44 | floating-point value from 0.0 to 1.0. |
---|
| 45 | @par |
---|
| 46 | The 3 'normal' colour components are red, green and blue, a higher |
---|
| 47 | number indicating greater amounts of that component in the colour. |
---|
| 48 | The forth component is the 'alpha' value, which represents |
---|
| 49 | transparency. In this case, 0.0 is completely transparent and 1.0 is |
---|
| 50 | fully opaque. |
---|
| 51 | */ |
---|
| 52 | class _OgreExport ColourValue |
---|
| 53 | { |
---|
| 54 | public: |
---|
| 55 | static const ColourValue ZERO; |
---|
| 56 | static const ColourValue Black; |
---|
| 57 | static const ColourValue White; |
---|
| 58 | static const ColourValue Red; |
---|
| 59 | static const ColourValue Green; |
---|
| 60 | static const ColourValue Blue; |
---|
| 61 | |
---|
| 62 | explicit ColourValue( float red = 1.0f, |
---|
| 63 | float green = 1.0f, |
---|
| 64 | float blue = 1.0f, |
---|
| 65 | float alpha = 1.0f ) : r(red), g(green), b(blue), a(alpha) |
---|
| 66 | { } |
---|
| 67 | |
---|
| 68 | bool operator==(const ColourValue& rhs) const; |
---|
| 69 | bool operator!=(const ColourValue& rhs) const; |
---|
| 70 | |
---|
| 71 | float r,g,b,a; |
---|
| 72 | |
---|
| 73 | /** Retrieves colour as RGBA. |
---|
| 74 | */ |
---|
| 75 | RGBA getAsRGBA(void) const; |
---|
| 76 | |
---|
| 77 | /** Retrieves colour as ARGB. |
---|
| 78 | */ |
---|
| 79 | ARGB getAsARGB(void) const; |
---|
| 80 | |
---|
| 81 | /** Retrieves colour as BGRA. |
---|
| 82 | */ |
---|
| 83 | BGRA getAsBGRA(void) const; |
---|
| 84 | |
---|
| 85 | /** Retrieves colours as ABGR */ |
---|
| 86 | ABGR getAsABGR(void) const; |
---|
| 87 | |
---|
| 88 | /** Sets colour as RGBA. |
---|
| 89 | */ |
---|
| 90 | void setAsRGBA(const RGBA val); |
---|
| 91 | |
---|
| 92 | /** Sets colour as ARGB. |
---|
| 93 | */ |
---|
| 94 | void setAsARGB(const ARGB val); |
---|
| 95 | |
---|
| 96 | /** Sets colour as BGRA. |
---|
| 97 | */ |
---|
| 98 | void setAsBGRA(const BGRA val); |
---|
| 99 | |
---|
| 100 | /** Sets colour as ABGR. |
---|
| 101 | */ |
---|
| 102 | void setAsABGR(const ABGR val); |
---|
| 103 | |
---|
| 104 | /** Clamps colour value to the range [0, 1]. |
---|
| 105 | */ |
---|
| 106 | void saturate(void) |
---|
| 107 | { |
---|
| 108 | if (r < 0) |
---|
| 109 | r = 0; |
---|
| 110 | else if (r > 1) |
---|
| 111 | r = 1; |
---|
| 112 | |
---|
| 113 | if (g < 0) |
---|
| 114 | g = 0; |
---|
| 115 | else if (g > 1) |
---|
| 116 | g = 1; |
---|
| 117 | |
---|
| 118 | if (b < 0) |
---|
| 119 | b = 0; |
---|
| 120 | else if (b > 1) |
---|
| 121 | b = 1; |
---|
| 122 | |
---|
| 123 | if (a < 0) |
---|
| 124 | a = 0; |
---|
| 125 | else if (a > 1) |
---|
| 126 | a = 1; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | /** As saturate, except that this colour value is unaffected and |
---|
| 130 | the saturated colour value is returned as a copy. */ |
---|
| 131 | ColourValue saturateCopy(void) const |
---|
| 132 | { |
---|
| 133 | ColourValue ret = *this; |
---|
| 134 | ret.saturate(); |
---|
| 135 | return ret; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /// Array accessor operator |
---|
| 139 | inline float operator [] ( const size_t i ) const |
---|
| 140 | { |
---|
| 141 | assert( i < 4 ); |
---|
| 142 | |
---|
| 143 | return *(&r+i); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | /// Array accessor operator |
---|
| 147 | inline float& operator [] ( const size_t i ) |
---|
| 148 | { |
---|
| 149 | assert( i < 4 ); |
---|
| 150 | |
---|
| 151 | return *(&r+i); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /// Pointer accessor for direct copying |
---|
| 155 | inline float* ptr() |
---|
| 156 | { |
---|
| 157 | return &r; |
---|
| 158 | } |
---|
| 159 | /// Pointer accessor for direct copying |
---|
| 160 | inline const float* ptr() const |
---|
| 161 | { |
---|
| 162 | return &r; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | // arithmetic operations |
---|
| 167 | inline ColourValue operator + ( const ColourValue& rkVector ) const |
---|
| 168 | { |
---|
| 169 | ColourValue kSum; |
---|
| 170 | |
---|
| 171 | kSum.r = r + rkVector.r; |
---|
| 172 | kSum.g = g + rkVector.g; |
---|
| 173 | kSum.b = b + rkVector.b; |
---|
| 174 | kSum.a = a + rkVector.a; |
---|
| 175 | |
---|
| 176 | return kSum; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | inline ColourValue operator - ( const ColourValue& rkVector ) const |
---|
| 180 | { |
---|
| 181 | ColourValue kDiff; |
---|
| 182 | |
---|
| 183 | kDiff.r = r - rkVector.r; |
---|
| 184 | kDiff.g = g - rkVector.g; |
---|
| 185 | kDiff.b = b - rkVector.b; |
---|
| 186 | kDiff.a = a - rkVector.a; |
---|
| 187 | |
---|
| 188 | return kDiff; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | inline ColourValue operator * (const float fScalar ) const |
---|
| 192 | { |
---|
| 193 | ColourValue kProd; |
---|
| 194 | |
---|
| 195 | kProd.r = fScalar*r; |
---|
| 196 | kProd.g = fScalar*g; |
---|
| 197 | kProd.b = fScalar*b; |
---|
| 198 | kProd.a = fScalar*a; |
---|
| 199 | |
---|
| 200 | return kProd; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | inline ColourValue operator * ( const ColourValue& rhs) const |
---|
| 204 | { |
---|
| 205 | ColourValue kProd; |
---|
| 206 | |
---|
| 207 | kProd.r = rhs.r * r; |
---|
| 208 | kProd.g = rhs.g * g; |
---|
| 209 | kProd.b = rhs.b * b; |
---|
| 210 | kProd.a = rhs.a * a; |
---|
| 211 | |
---|
| 212 | return kProd; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | inline ColourValue operator / ( const ColourValue& rhs) const |
---|
| 216 | { |
---|
| 217 | ColourValue kProd; |
---|
| 218 | |
---|
| 219 | kProd.r = rhs.r / r; |
---|
| 220 | kProd.g = rhs.g / g; |
---|
| 221 | kProd.b = rhs.b / b; |
---|
| 222 | kProd.a = rhs.a / a; |
---|
| 223 | |
---|
| 224 | return kProd; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | inline ColourValue operator / (const float fScalar ) const |
---|
| 228 | { |
---|
| 229 | assert( fScalar != 0.0 ); |
---|
| 230 | |
---|
| 231 | ColourValue kDiv; |
---|
| 232 | |
---|
| 233 | float fInv = 1.0 / fScalar; |
---|
| 234 | kDiv.r = r * fInv; |
---|
| 235 | kDiv.g = g * fInv; |
---|
| 236 | kDiv.b = b * fInv; |
---|
| 237 | kDiv.a = a * fInv; |
---|
| 238 | |
---|
| 239 | return kDiv; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | inline friend ColourValue operator * (const float fScalar, const ColourValue& rkVector ) |
---|
| 243 | { |
---|
| 244 | ColourValue kProd; |
---|
| 245 | |
---|
| 246 | kProd.r = fScalar * rkVector.r; |
---|
| 247 | kProd.g = fScalar * rkVector.g; |
---|
| 248 | kProd.b = fScalar * rkVector.b; |
---|
| 249 | kProd.a = fScalar * rkVector.a; |
---|
| 250 | |
---|
| 251 | return kProd; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | // arithmetic updates |
---|
| 255 | inline ColourValue& operator += ( const ColourValue& rkVector ) |
---|
| 256 | { |
---|
| 257 | r += rkVector.r; |
---|
| 258 | g += rkVector.g; |
---|
| 259 | b += rkVector.b; |
---|
| 260 | a += rkVector.a; |
---|
| 261 | |
---|
| 262 | return *this; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | inline ColourValue& operator -= ( const ColourValue& rkVector ) |
---|
| 266 | { |
---|
| 267 | r -= rkVector.r; |
---|
| 268 | g -= rkVector.g; |
---|
| 269 | b -= rkVector.b; |
---|
| 270 | a -= rkVector.a; |
---|
| 271 | |
---|
| 272 | return *this; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | inline ColourValue& operator *= (const float fScalar ) |
---|
| 276 | { |
---|
| 277 | r *= fScalar; |
---|
| 278 | g *= fScalar; |
---|
| 279 | b *= fScalar; |
---|
| 280 | a *= fScalar; |
---|
| 281 | return *this; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | inline ColourValue& operator /= (const float fScalar ) |
---|
| 285 | { |
---|
| 286 | assert( fScalar != 0.0 ); |
---|
| 287 | |
---|
| 288 | float fInv = 1.0 / fScalar; |
---|
| 289 | |
---|
| 290 | r *= fInv; |
---|
| 291 | g *= fInv; |
---|
| 292 | b *= fInv; |
---|
| 293 | a *= fInv; |
---|
| 294 | |
---|
| 295 | return *this; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | /** Set a colour value from Hue, Saturation and Brightness. |
---|
| 299 | @param hue Hue value, scaled to the [0,1] range as opposed to the 0-360 |
---|
| 300 | @param saturation Saturation level, [0,1] |
---|
| 301 | @param brightness Brightness level, [0,1] |
---|
| 302 | */ |
---|
| 303 | void setHSB(Real hue, Real saturation, Real brightness); |
---|
| 304 | |
---|
| 305 | |
---|
| 306 | /** Function for writing to a stream. |
---|
| 307 | */ |
---|
| 308 | inline _OgreExport friend std::ostream& operator << |
---|
| 309 | ( std::ostream& o, const ColourValue& c ) |
---|
| 310 | { |
---|
| 311 | o << "ColourValue(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")"; |
---|
| 312 | return o; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | }; |
---|
| 316 | |
---|
| 317 | } // namespace |
---|
| 318 | |
---|
| 319 | #endif |
---|