[692] | 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-2005 The OGRE Team
|
---|
| 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 | */
|
---|
| 25 | #include "OgreStableHeaders.h"
|
---|
| 26 | #include "OgreColourValue.h"
|
---|
| 27 |
|
---|
| 28 | namespace Ogre {
|
---|
| 29 |
|
---|
| 30 | const ColourValue ColourValue::ZERO = ColourValue(0.0,0.0,0.0,0.0);
|
---|
| 31 | const ColourValue ColourValue::Black = ColourValue(0.0,0.0,0.0);
|
---|
| 32 | const ColourValue ColourValue::White = ColourValue(1.0,1.0,1.0);
|
---|
| 33 | const ColourValue ColourValue::Red = ColourValue(1.0,0.0,0.0);
|
---|
| 34 | const ColourValue ColourValue::Green = ColourValue(0.0,1.0,0.0);
|
---|
| 35 | const ColourValue ColourValue::Blue = ColourValue(0.0,0.0,1.0);
|
---|
| 36 |
|
---|
| 37 | //---------------------------------------------------------------------
|
---|
| 38 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
|
---|
| 39 | ABGR ColourValue::getAsABGR(void) const
|
---|
| 40 | #else
|
---|
| 41 | RGBA ColourValue::getAsRGBA(void) const
|
---|
| 42 | #endif
|
---|
| 43 | {
|
---|
| 44 | uint8 val8;
|
---|
| 45 | uint32 val32 = 0;
|
---|
| 46 |
|
---|
| 47 | // Convert to 32bit pattern
|
---|
| 48 | // (RGBA = 8888)
|
---|
| 49 |
|
---|
| 50 | // Red
|
---|
| 51 | val8 = static_cast<uint8>(r * 255);
|
---|
| 52 | val32 = val8 << 24;
|
---|
| 53 |
|
---|
| 54 | // Green
|
---|
| 55 | val8 = static_cast<uint8>(g * 255);
|
---|
| 56 | val32 += val8 << 16;
|
---|
| 57 |
|
---|
| 58 | // Blue
|
---|
| 59 | val8 = static_cast<uint8>(b * 255);
|
---|
| 60 | val32 += val8 << 8;
|
---|
| 61 |
|
---|
| 62 | // Alpha
|
---|
| 63 | val8 = static_cast<uint8>(a * 255);
|
---|
| 64 | val32 += val8;
|
---|
| 65 |
|
---|
| 66 | return val32;
|
---|
| 67 | }
|
---|
| 68 | //---------------------------------------------------------------------
|
---|
| 69 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
|
---|
| 70 | BGRA ColourValue::getAsBGRA(void) const
|
---|
| 71 | #else
|
---|
| 72 | ARGB ColourValue::getAsARGB(void) const
|
---|
| 73 | #endif
|
---|
| 74 | {
|
---|
| 75 | uint8 val8;
|
---|
| 76 | uint32 val32 = 0;
|
---|
| 77 |
|
---|
| 78 | // Convert to 32bit pattern
|
---|
| 79 | // (ARGB = 8888)
|
---|
| 80 |
|
---|
| 81 | // Alpha
|
---|
| 82 | val8 = static_cast<uint8>(a * 255);
|
---|
| 83 | val32 = val8 << 24;
|
---|
| 84 |
|
---|
| 85 | // Red
|
---|
| 86 | val8 = static_cast<uint8>(r * 255);
|
---|
| 87 | val32 += val8 << 16;
|
---|
| 88 |
|
---|
| 89 | // Green
|
---|
| 90 | val8 = static_cast<uint8>(g * 255);
|
---|
| 91 | val32 += val8 << 8;
|
---|
| 92 |
|
---|
| 93 | // Blue
|
---|
| 94 | val8 = static_cast<uint8>(b * 255);
|
---|
| 95 | val32 += val8;
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | return val32;
|
---|
| 99 | }
|
---|
| 100 | //---------------------------------------------------------------------
|
---|
| 101 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
|
---|
| 102 | ARGB ColourValue::getAsARGB(void) const
|
---|
| 103 | #else
|
---|
| 104 | BGRA ColourValue::getAsBGRA(void) const
|
---|
| 105 | #endif
|
---|
| 106 | {
|
---|
| 107 | uint8 val8;
|
---|
| 108 | uint32 val32 = 0;
|
---|
| 109 |
|
---|
| 110 | // Convert to 32bit pattern
|
---|
| 111 | // (ARGB = 8888)
|
---|
| 112 |
|
---|
| 113 | // Blue
|
---|
| 114 | val8 = static_cast<uint8>(b * 255);
|
---|
| 115 | val32 = val8 << 24;
|
---|
| 116 |
|
---|
| 117 | // Green
|
---|
| 118 | val8 = static_cast<uint8>(g * 255);
|
---|
| 119 | val32 += val8 << 16;
|
---|
| 120 |
|
---|
| 121 | // Red
|
---|
| 122 | val8 = static_cast<uint8>(r * 255);
|
---|
| 123 | val32 += val8 << 8;
|
---|
| 124 |
|
---|
| 125 | // Alpha
|
---|
| 126 | val8 = static_cast<uint8>(a * 255);
|
---|
| 127 | val32 += val8;
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 | return val32;
|
---|
| 131 | }
|
---|
| 132 | //---------------------------------------------------------------------
|
---|
| 133 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
|
---|
| 134 | RGBA ColourValue::getAsRGBA(void) const
|
---|
| 135 | #else
|
---|
| 136 | ABGR ColourValue::getAsABGR(void) const
|
---|
| 137 | #endif
|
---|
| 138 | {
|
---|
| 139 | uint8 val8;
|
---|
| 140 | uint32 val32 = 0;
|
---|
| 141 |
|
---|
| 142 | // Convert to 32bit pattern
|
---|
| 143 | // (ABRG = 8888)
|
---|
| 144 |
|
---|
| 145 | // Alpha
|
---|
| 146 | val8 = static_cast<uint8>(a * 255);
|
---|
| 147 | val32 = val8 << 24;
|
---|
| 148 |
|
---|
| 149 | // Blue
|
---|
| 150 | val8 = static_cast<uint8>(b * 255);
|
---|
| 151 | val32 += val8 << 16;
|
---|
| 152 |
|
---|
| 153 | // Green
|
---|
| 154 | val8 = static_cast<uint8>(g * 255);
|
---|
| 155 | val32 += val8 << 8;
|
---|
| 156 |
|
---|
| 157 | // Red
|
---|
| 158 | val8 = static_cast<uint8>(r * 255);
|
---|
| 159 | val32 += val8;
|
---|
| 160 |
|
---|
| 161 |
|
---|
| 162 | return val32;
|
---|
| 163 | }
|
---|
| 164 | //---------------------------------------------------------------------
|
---|
| 165 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
|
---|
| 166 | void ColourValue::setAsABGR(const ABGR val)
|
---|
| 167 | #else
|
---|
| 168 | void ColourValue::setAsRGBA(const RGBA val)
|
---|
| 169 | #endif
|
---|
| 170 | {
|
---|
| 171 | uint32 val32 = val;
|
---|
| 172 |
|
---|
| 173 | // Convert from 32bit pattern
|
---|
| 174 | // (RGBA = 8888)
|
---|
| 175 |
|
---|
| 176 | // Red
|
---|
| 177 | r = static_cast<uint8>(val32 >> 24) / 255.0f;
|
---|
| 178 |
|
---|
| 179 | // Green
|
---|
| 180 | g = static_cast<uint8>(val32 >> 16) / 255.0f;
|
---|
| 181 |
|
---|
| 182 | // Blue
|
---|
| 183 | b = static_cast<uint8>(val32 >> 8) / 255.0f;
|
---|
| 184 |
|
---|
| 185 | // Alpha
|
---|
| 186 | a = static_cast<uint8>(val32) / 255.0f;
|
---|
| 187 | }
|
---|
| 188 | //---------------------------------------------------------------------
|
---|
| 189 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
|
---|
| 190 | void ColourValue::setAsBGRA(const BGRA val)
|
---|
| 191 | #else
|
---|
| 192 | void ColourValue::setAsARGB(const ARGB val)
|
---|
| 193 | #endif
|
---|
| 194 | {
|
---|
| 195 | uint32 val32 = val;
|
---|
| 196 |
|
---|
| 197 | // Convert from 32bit pattern
|
---|
| 198 | // (ARGB = 8888)
|
---|
| 199 |
|
---|
| 200 | // Alpha
|
---|
| 201 | a = static_cast<uint8>(val32 >> 24) / 255.0f;
|
---|
| 202 |
|
---|
| 203 | // Red
|
---|
| 204 | r = static_cast<uint8>(val32 >> 16) / 255.0f;
|
---|
| 205 |
|
---|
| 206 | // Green
|
---|
| 207 | g = static_cast<uint8>(val32 >> 8) / 255.0f;
|
---|
| 208 |
|
---|
| 209 | // Blue
|
---|
| 210 | b = static_cast<uint8>(val32) / 255.0f;
|
---|
| 211 | }
|
---|
| 212 | //---------------------------------------------------------------------
|
---|
| 213 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
|
---|
| 214 | void ColourValue::setAsARGB(const ARGB val)
|
---|
| 215 | #else
|
---|
| 216 | void ColourValue::setAsBGRA(const BGRA val)
|
---|
| 217 | #endif
|
---|
| 218 | {
|
---|
| 219 | uint32 val32 = val;
|
---|
| 220 |
|
---|
| 221 | // Convert from 32bit pattern
|
---|
| 222 | // (ARGB = 8888)
|
---|
| 223 |
|
---|
| 224 | // Blue
|
---|
| 225 | b = static_cast<uint8>(val32 >> 24) / 255.0f;
|
---|
| 226 |
|
---|
| 227 | // Green
|
---|
| 228 | g = static_cast<uint8>(val32 >> 16) / 255.0f;
|
---|
| 229 |
|
---|
| 230 | // Red
|
---|
| 231 | r = static_cast<uint8>(val32 >> 8) / 255.0f;
|
---|
| 232 |
|
---|
| 233 | // Alpha
|
---|
| 234 | a = static_cast<uint8>(val32) / 255.0f;
|
---|
| 235 | }
|
---|
| 236 | //---------------------------------------------------------------------
|
---|
| 237 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
|
---|
| 238 | void ColourValue::setAsRGBA(const RGBA val)
|
---|
| 239 | #else
|
---|
| 240 | void ColourValue::setAsABGR(const ABGR val)
|
---|
| 241 | #endif
|
---|
| 242 | {
|
---|
| 243 | uint32 val32 = val;
|
---|
| 244 |
|
---|
| 245 | // Convert from 32bit pattern
|
---|
| 246 | // (ABGR = 8888)
|
---|
| 247 |
|
---|
| 248 | // Alpha
|
---|
| 249 | a = static_cast<uint8>(val32 >> 24) / 255.0f;
|
---|
| 250 |
|
---|
| 251 | // Blue
|
---|
| 252 | b = static_cast<uint8>(val32 >> 16) / 255.0f;
|
---|
| 253 |
|
---|
| 254 | // Green
|
---|
| 255 | g = static_cast<uint8>(val32 >> 8) / 255.0f;
|
---|
| 256 |
|
---|
| 257 | // Red
|
---|
| 258 | r = static_cast<uint8>(val32) / 255.0f;
|
---|
| 259 | }
|
---|
| 260 | //---------------------------------------------------------------------
|
---|
| 261 | bool ColourValue::operator==(const ColourValue& rhs) const
|
---|
| 262 | {
|
---|
| 263 | return (r == rhs.r &&
|
---|
| 264 | g == rhs.g &&
|
---|
| 265 | b == rhs.b &&
|
---|
| 266 | a == rhs.a);
|
---|
| 267 | }
|
---|
| 268 | //---------------------------------------------------------------------
|
---|
| 269 | bool ColourValue::operator!=(const ColourValue& rhs) const
|
---|
| 270 | {
|
---|
| 271 | return !(*this == rhs);
|
---|
| 272 | }
|
---|
| 273 | //---------------------------------------------------------------------
|
---|
| 274 | void ColourValue::setHSB(Real hue, Real saturation, Real brightness)
|
---|
| 275 | {
|
---|
| 276 | // wrap hue
|
---|
| 277 | if (hue > 1.0f)
|
---|
| 278 | {
|
---|
| 279 | hue -= (int)hue;
|
---|
| 280 | }
|
---|
| 281 | else if (hue < 0.0f)
|
---|
| 282 | {
|
---|
| 283 | hue += (int)hue + 1;
|
---|
| 284 | }
|
---|
| 285 | // clamp saturation / brightness
|
---|
| 286 | saturation = std::min(saturation, (Real)1.0);
|
---|
| 287 | saturation = std::max(saturation, (Real)0.0);
|
---|
| 288 | brightness = std::min(brightness, (Real)1.0);
|
---|
| 289 | brightness = std::max(brightness, (Real)0.0);
|
---|
| 290 |
|
---|
| 291 | if (brightness == 0.0f)
|
---|
| 292 | {
|
---|
| 293 | // early exit, this has to be black
|
---|
| 294 | r = g = b = 0.0f;
|
---|
| 295 | return;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | if (saturation == 0.0f)
|
---|
| 299 | {
|
---|
| 300 | // early exit, this has to be grey
|
---|
| 301 |
|
---|
| 302 | r = g = b = brightness;
|
---|
| 303 | return;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 |
|
---|
| 307 | Real hueDomain = hue * 6.0f;
|
---|
| 308 | if (hueDomain >= 6.0f)
|
---|
| 309 | {
|
---|
| 310 | // wrap around, and allow mathematical errors
|
---|
| 311 | hueDomain = 0.0f;
|
---|
| 312 | }
|
---|
| 313 | unsigned short domain = (unsigned short)hueDomain;
|
---|
| 314 | Real f1 = brightness * (1 - saturation);
|
---|
| 315 | Real f2 = brightness * (1 - saturation * (hueDomain - domain));
|
---|
| 316 | Real f3 = brightness * (1 - saturation * (1 - (hueDomain - domain)));
|
---|
| 317 |
|
---|
| 318 | switch (domain)
|
---|
| 319 | {
|
---|
| 320 | case 0:
|
---|
| 321 | // red domain; green ascends
|
---|
| 322 | r = brightness;
|
---|
| 323 | g = f3;
|
---|
| 324 | b = f1;
|
---|
| 325 | break;
|
---|
| 326 | case 1:
|
---|
| 327 | // yellow domain; red descends
|
---|
| 328 | r = f2;
|
---|
| 329 | g = brightness;
|
---|
| 330 | b = f1;
|
---|
| 331 | break;
|
---|
| 332 | case 2:
|
---|
| 333 | // green domain; blue ascends
|
---|
| 334 | r = f1;
|
---|
| 335 | g = brightness;
|
---|
| 336 | b = f3;
|
---|
| 337 | break;
|
---|
| 338 | case 3:
|
---|
| 339 | // cyan domain; green descends
|
---|
| 340 | r = f1;
|
---|
| 341 | g = f2;
|
---|
| 342 | b = brightness;
|
---|
| 343 | break;
|
---|
| 344 | case 4:
|
---|
| 345 | // blue domain; red ascends
|
---|
| 346 | r = f3;
|
---|
| 347 | g = f1;
|
---|
| 348 | b = brightness;
|
---|
| 349 | break;
|
---|
| 350 | case 5:
|
---|
| 351 | // magenta domain; blue descends
|
---|
| 352 | r = brightness;
|
---|
| 353 | g = f1;
|
---|
| 354 | b = f2;
|
---|
| 355 | break;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 |
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | }
|
---|
| 362 |
|
---|