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