1 /*---------------------------------------------------------------------------* 2 Project: NintendoWare 3 File: ut_Color.h 4 5 Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. All rights reserved. 6 7 These coded instructions, statements, and computer programs contain 8 proprietary information of Nintendo of America Inc. and/or Nintendo 9 Company Ltd., and are protected by Federal copyright law. They may 10 not be disclosed to third parties or copied or duplicated in any form, 11 in whole or in part, without the prior written consent of Nintendo. 12 13 $Revision:$ 14 *---------------------------------------------------------------------------*/ 15 16 #ifndef NW_UT_COLOR_H_ 17 #define NW_UT_COLOR_H_ 18 19 //#include <twl/gx/GXStruct.h> 20 #include <nw/ut/ut_Inlines.h> 21 #include <nw/math/math_Types.h> 22 23 namespace nw { 24 namespace ut { 25 26 struct FloatColor; 27 28 //-------------------------------------------------------------------------- 29 //! @brief 整数カラーを表す構造体です。 30 //--------------------------------------------------------------------------- 31 struct Color8 32 { 33 public: 34 typedef Color8 SelfType; //!< @details :private 35 36 /* ------------------------------------------------------------------------ 37 定数 38 ------------------------------------------------------------------------ */ 39 static const int ELEMENT_MIN = 0; //!< カラー要素の最小値です。 40 static const int ELEMENT_MAX = 255; //!< カラー要素の最大値です。 41 static const int ALPHA_MIN = ELEMENT_MIN; //!< αの最小値です。 42 static const int ALPHA_MAX = ELEMENT_MAX; //!< αの最大値です。 43 static const int ALPHA_OPACITY = ALPHA_MAX; //!< 不透明のα値です。 44 static const int ALPHA_TRANSPARENT = ALPHA_MIN; //!< 透明のα値です。 45 46 static const u32 BLACK = 0x000000FF; //!< 黒色の定数です。 47 static const u32 GRAY = 0x808080FF; //!< 灰色の定数です。 48 static const u32 WHITE = 0xFFFFFFFF; //!< 白色の定数です。 49 static const u32 RED = 0xFF0000FF; //!< 赤色の定数です。 50 static const u32 GREEN = 0x00FF00FF; //!< 緑色の定数です。 51 static const u32 BLUE = 0x0000FFFF; //!< 青色の定数です。 52 static const u32 YELLOW = 0xFFFF00FF; //!< 黄色の定数です。 53 static const u32 MAGENTA = 0xFF00FFFF; //!< マゼンダの定数です。 54 static const u32 CYAN = 0x00FFFFFF; //!< シアンの定数です。 55 56 57 /* ------------------------------------------------------------------------ 58 関数 59 ------------------------------------------------------------------------ */ 60 //---------------------------------------- 61 //! @name コンストラクタ/デストラクタ 62 //@{ 63 64 //-------------------------------------------------------------------------- 65 //! @brief デフォルトコンストラクタです。 66 //-------------------------------------------------------------------------- Color8Color867 /* ctor */ Color8() { this->SetU32(WHITE); } 68 69 //-------------------------------------------------------------------------- 70 //! @brief u32 の値を設定するコンストラクタです。 71 //-------------------------------------------------------------------------- Color8Color872 /* ctor */ /*implicit*/ Color8(u32 color) { this->SetU32(color); } 73 74 //-------------------------------------------------------------------------- 75 //! @brief RGBA 各要素を設定するコンストラクタです。 76 //-------------------------------------------------------------------------- Color8Color877 /* ctor */ /*implicit*/ Color8(s32 red, s32 green, s32 blue, s32 alpha) 78 : r(static_cast<u8>(red)), 79 g(static_cast<u8>(green)), 80 b(static_cast<u8>(blue)), 81 a(static_cast<u8>(alpha)) 82 { 83 } 84 85 //-------------------------------------------------------------------------- 86 //! @brief デストラクタです。 87 //-------------------------------------------------------------------------- ~Color8Color888 /* dtor */ ~Color8() {} 89 90 //@} 91 92 //---------------------------------------- 93 //! @name 演算子オーバーロード 94 //@{ 95 96 //-------------------------------------------------------------------------- 97 //! @brief u32 型を代入する代入演算子です。 98 //! 99 //! @param[out] color RGBA8 の u32 型インスタンスです。 100 //! 101 //! @return Color8 のインスタンスを返します。 102 //--------------------------------------------------------------------------- 103 SelfType& operator =(u32 color) 104 { 105 this->SetU32(color); 106 return *this; 107 } 108 109 //-------------------------------------------------------------------------- 110 //! @brief FloatColor 型を代入する代入演算子です。 111 //! 112 //! @param[out] color FloatColor 型のカラーインスタンスです。 113 //! 114 //! @return Color8 のインスタンスを返します。 115 //--------------------------------------------------------------------------- 116 SelfType& operator =(FloatColor& color); 117 118 //-------------------------------------------------------------------------- 119 //! @brief u32 型へのキャスト演算子です。 120 //! 121 //! @return u32 で表現した値を返します。 122 //--------------------------------------------------------------------------- u32Color8123 operator u32() const { return ToU32(); } 124 125 //---- 四則演算 126 //-------------------------------------------------------------------------- 127 //! @brief カラー同士の加算演算子です。 128 //! 129 //! @param[in] right 加算をおこなうカラー値です。 130 //! 131 //! @return 加算後の値を返します。 132 //--------------------------------------------------------------------------- 133 const SelfType operator +(const SelfType& right) const 134 { 135 SelfType color( 136 Min(this->r + right.r, ELEMENT_MAX), 137 Min(this->g + right.g, ELEMENT_MAX), 138 Min(this->b + right.b, ELEMENT_MAX), 139 Min(this->a + right.a, ELEMENT_MAX) 140 ); 141 return color; 142 } 143 144 //-------------------------------------------------------------------------- 145 //! @brief カラー同士の減算演算子です。 146 //! 147 //! @param[in] right 減算をおこなうカラー値です。 148 //! 149 //! @return 減算後の値を返します。 150 //--------------------------------------------------------------------------- 151 const SelfType operator -(const SelfType& right) const 152 { 153 SelfType color( 154 Max(this->r - right.r, ELEMENT_MIN), 155 Max(this->g - right.g, ELEMENT_MIN), 156 Max(this->b - right.b, ELEMENT_MIN), 157 Max(this->a - right.a, ELEMENT_MIN) 158 ); 159 return color; 160 } 161 162 //-------------------------------------------------------------------------- 163 //! @brief カラー同士の乗算演算子です。 164 //! 165 //! @param[in] right 乗算をおこなうカラー値です。 166 //! 167 //! @return 乗算後の値を返します。 168 //--------------------------------------------------------------------------- 169 const SelfType operator *(const SelfType& right) const 170 { 171 SelfType color( 172 this->r * right.r / ELEMENT_MAX, 173 this->g * right.g / ELEMENT_MAX, 174 this->b * right.b / ELEMENT_MAX, 175 this->a * right.a / ELEMENT_MAX 176 ); 177 return color; 178 } 179 180 //-------------------------------------------------------------------------- 181 //! @brief カラー同士の除算演算子です。 182 //! 183 //! @param[in] right 除算をおこなうカラー値です。 184 //! 185 //! @return 除算後の値を返します。 186 //--------------------------------------------------------------------------- 187 const SelfType operator /(const SelfType& right) const 188 { 189 SelfType color( 190 (right.r != 0) ? Min(this->r * ELEMENT_MAX / right.r, ELEMENT_MAX): ELEMENT_MAX, 191 (right.g != 0) ? Min(this->g * ELEMENT_MAX / right.g, ELEMENT_MAX): ELEMENT_MAX, 192 (right.b != 0) ? Min(this->b * ELEMENT_MAX / right.b, ELEMENT_MAX): ELEMENT_MAX, 193 (right.a != 0) ? Min(this->a * ELEMENT_MAX / right.a, ELEMENT_MAX): ELEMENT_MAX 194 ); 195 return color; 196 } 197 198 //---- インクリメント、デクリメント 199 //-------------------------------------------------------------------------- 200 //! @brief 前置のインクリメント演算子です。 201 //! 202 //! @return 各要素をインクリメントした値を返します。 203 //--------------------------------------------------------------------------- 204 const SelfType operator ++() 205 { 206 if (r < ELEMENT_MAX) { r++; } 207 if (g < ELEMENT_MAX) { g++; } 208 if (b < ELEMENT_MAX) { b++; } 209 if (a < ELEMENT_MAX) { a++; } 210 211 return *this; 212 } 213 214 //-------------------------------------------------------------------------- 215 //! @brief 後置のインクリメント演算子です。 216 //! 217 //! @return インクリメント前の値を返します。 218 //--------------------------------------------------------------------------- 219 const SelfType operator ++(int) 220 { 221 SelfType old = *this; 222 if (r < ELEMENT_MAX) { r++; } 223 if (g < ELEMENT_MAX) { g++; } 224 if (b < ELEMENT_MAX) { b++; } 225 if (a < ELEMENT_MAX) { a++; } 226 227 return old; 228 } 229 230 //-------------------------------------------------------------------------- 231 //! @brief 前置のデクリメント演算子です。 232 //! 233 //! @return 各要素をデクリメントした値を返します。 234 //--------------------------------------------------------------------------- 235 const SelfType operator --() 236 { 237 if (r > ELEMENT_MIN) { r--; } 238 if (g > ELEMENT_MIN) { g--; } 239 if (b > ELEMENT_MIN) { b--; } 240 if (a > ELEMENT_MIN) { a--; } 241 242 return *this; 243 } 244 245 //-------------------------------------------------------------------------- 246 //! @brief 後置のデクリメント演算子です。 247 //! 248 //! @return デクリメント前の値を返します。 249 //--------------------------------------------------------------------------- 250 const SelfType operator --(int) 251 { 252 SelfType old = *this; 253 if (r > ELEMENT_MIN) { r--; } 254 if (g > ELEMENT_MIN) { g--; } 255 if (b > ELEMENT_MIN) { b--; } 256 if (a > ELEMENT_MIN) { a--; } 257 258 return old; 259 } 260 261 //---- bitごとの和と積 262 //-------------------------------------------------------------------------- 263 //! @brief u32 としての ビット和を求めます。 264 //! 265 //! @param[in] right ビット和をとるカラーです。 266 //! 267 //! @return ビット和をとったカラーを返します。 268 //--------------------------------------------------------------------------- 269 const SelfType operator |(const SelfType& right) const 270 { 271 return U32RawToColor(this->ToU32Raw() | right.ToU32Raw()); 272 } 273 274 //-------------------------------------------------------------------------- 275 //! @brief u32 としての ビット積を求めます。 276 //! 277 //! @param[in] right ビット積をとるカラーです。 278 //! 279 //! @return ビット積をとったカラーを返します。 280 //--------------------------------------------------------------------------- 281 const SelfType operator &(const SelfType& right) const 282 { 283 return U32RawToColor(this->ToU32Raw() & right.ToU32Raw()); 284 } 285 286 //-------------------------------------------------------------------------- 287 //! @brief カラー同士の加算代入演算子です。 288 //! 289 //! @param[in] rhs 加算をおこなうカラー値です。 290 //! 291 //! @return 加算後のオブジェクトへの参照を返します。 292 //--------------------------------------------------------------------------- 293 SelfType& operator +=(const SelfType& rhs) 294 { 295 *this = *this + rhs; 296 return *this; 297 } 298 299 //-------------------------------------------------------------------------- 300 //! @brief カラー同士の減算代入演算子です。 301 //! 302 //! @param[in] rhs 減算をおこなうカラー値です。 303 //! 304 //! @return 減算後のオブジェクトへの参照を返します。 305 //--------------------------------------------------------------------------- 306 SelfType& operator -=(const SelfType& rhs) 307 { 308 *this = *this - rhs; 309 return *this; 310 } 311 312 //-------------------------------------------------------------------------- 313 //! @brief カラー同士の乗算代入演算子です。 314 //! 315 //! @param[in] rhs 乗算をおこなうカラー値です。 316 //! 317 //! @return 乗算後のオブジェクトへの参照を返します。 318 //--------------------------------------------------------------------------- 319 SelfType& operator *=(const SelfType& rhs) 320 { 321 *this = *this * rhs; 322 return *this; 323 } 324 325 //-------------------------------------------------------------------------- 326 //! @brief カラー同士の除算代入演算子です。 327 //! 328 //! @param[in] rhs 除算をおこなうカラー値です。 329 //! 330 //! @return 除算後のオブジェクトへの参照を返します。 331 //--------------------------------------------------------------------------- 332 SelfType& operator /=(const SelfType& rhs) 333 { 334 *this = *this / rhs; 335 return *this; 336 } 337 338 //-------------------------------------------------------------------------- 339 //! @brief u32 をカラーとして加算をおこなう演算子です。 340 //! 341 //! @param[in] rhs 加算をおこなう u32 値です。 342 //! 343 //! @return 加算後の値を返します。 344 //--------------------------------------------------------------------------- 345 const SelfType operator +(u32 rhs) const 346 { 347 const SelfType right = SelfType(rhs); 348 SelfType color( 349 Min(this->r + right.r, ELEMENT_MAX), 350 Min(this->g + right.g, ELEMENT_MAX), 351 Min(this->b + right.b, ELEMENT_MAX), 352 Min(this->a + right.a, ELEMENT_MAX) 353 ); 354 return color; 355 } 356 357 //-------------------------------------------------------------------------- 358 //! @brief u32 をカラーとして減算をおこなう演算子です。 359 //! 360 //! @param[in] rhs 減算をおこなう u32 値です。 361 //! 362 //! @return 減算後の値を返します。 363 //--------------------------------------------------------------------------- 364 const SelfType operator -(u32 rhs) const 365 { 366 const SelfType right = SelfType(rhs); 367 SelfType color( 368 Max(this->r - right.r, ELEMENT_MIN), 369 Max(this->g - right.g, ELEMENT_MIN), 370 Max(this->b - right.b, ELEMENT_MIN), 371 Max(this->a - right.a, ELEMENT_MIN) 372 ); 373 return color; 374 } 375 376 //-------------------------------------------------------------------------- 377 //! @brief u32 をカラーとして乗算をおこなう演算子です。 378 //! 379 //! @param[in] rhs 乗算をおこなう u32 値です。 380 //! 381 //! @return 乗算後の値を返します。 382 //--------------------------------------------------------------------------- 383 const SelfType operator *(u32 rhs) const 384 { 385 const SelfType right = SelfType(rhs); 386 SelfType color( 387 this->r * right.r / ELEMENT_MAX, 388 this->g * right.g / ELEMENT_MAX, 389 this->b * right.b / ELEMENT_MAX, 390 this->a * right.a / ELEMENT_MAX 391 ); 392 return color; 393 } 394 395 //-------------------------------------------------------------------------- 396 //! @brief u32 をカラーとして除算をおこなう演算子です。 397 //! 398 //! @param[in] rhs 除算をおこなう u32 値です。 399 //! 400 //! @return 除算後の値を返します。 401 //--------------------------------------------------------------------------- 402 const SelfType operator /(u32 rhs) const 403 { 404 const SelfType right = SelfType(rhs); 405 SelfType color( 406 (right.r != 0) ? Min(this->r * ELEMENT_MAX / right.r, ELEMENT_MAX): ELEMENT_MAX, 407 (right.g != 0) ? Min(this->g * ELEMENT_MAX / right.g, ELEMENT_MAX): ELEMENT_MAX, 408 (right.b != 0) ? Min(this->b * ELEMENT_MAX / right.b, ELEMENT_MAX): ELEMENT_MAX, 409 (right.a != 0) ? Min(this->a * ELEMENT_MAX / right.a, ELEMENT_MAX): ELEMENT_MAX 410 ); 411 return color; 412 } 413 414 //-------------------------------------------------------------------------- 415 //! @brief u32 をカラーとして論理和演算をおこなう演算子です。 416 //! 417 //! @param[in] right 論理和演算をおこなう u32 値です。 418 //! 419 //! @return 論理和演算後の値を返します。 420 //--------------------------------------------------------------------------- 421 const SelfType operator |(u32 right) const 422 { 423 return *this | SelfType( right ); 424 } 425 426 //-------------------------------------------------------------------------- 427 //! @brief u32 をカラーとして論理積演算をおこなう演算子です。 428 //! 429 //! @param[in] right 論理積演算をおこなう u32 値です。 430 //! 431 //! @return 論理積演算後の値を返します。 432 //--------------------------------------------------------------------------- 433 const SelfType operator &(u32 right) const 434 { 435 return *this & SelfType( right ); 436 } 437 438 //@} 439 440 //---------------------------------------- 441 //! @name 設定/取得 442 //@{ 443 444 //---- アクセサ 445 //-------------------------------------------------------------------------- 446 //! @brief RGBA の値を設定します。 447 //! 448 //! @param[in] red カラーの R 要素です。 449 //! @param[in] green カラーの G 要素です。 450 //! @param[in] blue カラーの B 要素です。 451 //! @param[in] alpha カラーの A 要素です。(デフォルト値: 255) 452 //--------------------------------------------------------------------------- 453 void Set( 454 s32 red, 455 s32 green, 456 s32 blue, 457 s32 alpha = ALPHA_OPACITY ) 458 { 459 r = static_cast<u8>(red); 460 g = static_cast<u8>(green); 461 b = static_cast<u8>(blue); 462 a = static_cast<u8>(alpha); 463 } 464 465 //-------------------------------------------------------------------------- 466 //! @brief RGBA8 のカラー値を設定します。 467 //! 468 //! @param[in] color 設定するカラー値です。 469 //--------------------------------------------------------------------------- SetColor8470 void Set(SelfType color) 471 { 472 operator =(color); 473 } 474 475 //---- 明示的な型変換 476 #if defined(NW_LITTLE_ENDIAN) 477 //-------------------------------------------------------------------------- 478 //! @brief u32 としてカラー値を取得します。 479 //! 480 //! @return カラーを u32 へ変換した値です。 481 //--------------------------------------------------------------------------- ToU32Color8482 u32 ToU32() const 483 { 484 return ReverseEndian( *reinterpret_cast<const u32*>(this) ); 485 } 486 487 //-------------------------------------------------------------------------- 488 //! @brief u32 として値を設定します。 489 //! 490 //! @param[in] value u32 としてのカラー値です。 491 //--------------------------------------------------------------------------- SetU32Color8492 void SetU32(u32 value) { this->ToU32RawRef() = ReverseEndian( value ); } 493 494 #else 495 //-------------------------------------------------------------------------- 496 //! @brief u32 としてカラー値を取得します。 497 //! 498 //! @return カラーを u32 へ変換した値です。 499 //--------------------------------------------------------------------------- ToU32Color8500 u32 ToU32() const 501 { 502 return this->ToU32RawRef(); 503 } 504 505 //-------------------------------------------------------------------------- 506 //! @brief u32 として値を設定します。 507 //! 508 //! @param[in] value u32 としてのかr−ア値です。 509 //--------------------------------------------------------------------------- SetU32Color8510 void SetU32(u32 value) { this->ToU32RawRef() = value; } 511 512 #endif 513 514 //@} 515 516 protected: 517 518 /* ------------------------------------------------------------------------ 519 関数 520 ------------------------------------------------------------------------ */ 521 //! @details :private U32RawToColorColor8522 static SelfType U32RawToColor(u32 value) 523 { 524 return *reinterpret_cast<SelfType*>(&value); 525 } 526 527 //! @details :private ToU32RawRefColor8528 u32& ToU32RawRef() 529 { 530 return *reinterpret_cast<u32*>(this); 531 } 532 533 //! @details :private ToU32RawRefColor8534 const u32& ToU32RawRef() const 535 { 536 return *reinterpret_cast<const u32*>(this); 537 } 538 539 //! @details :private ToU32RawColor8540 u32 ToU32Raw() const 541 { 542 return *reinterpret_cast<const u32*>(this); 543 } 544 545 public: 546 u8 r; //!< カラーの R 要素です。 0 ~ 255 の範囲で値を保持します。 547 u8 g; //!< カラーの G 要素です。 0 ~ 255 の範囲で値を保持します。 548 u8 b; //!< カラーの B 要素です。 0 ~ 255 の範囲で値を保持します。 549 u8 a; //!< カラーの A 要素です。 0 ~ 255 の範囲で値を保持します。 550 }; 551 552 //! @details :private 553 typedef Color8 Color; 554 555 //-------------------------------------------------------------------------- 556 //! @brief 浮動小数カラーを表す構造体です。 557 //--------------------------------------------------------------------------- 558 struct FloatColor 559 { 560 public: 561 typedef FloatColor SelfType; //!< @details :private 562 563 /* ------------------------------------------------------------------------ 564 定数 565 ------------------------------------------------------------------------ */ 566 static const int ELEMENT_MIN = 0; //!< カラー要素の最小値です。 567 static const int ELEMENT_MAX = 1; //!< カラー要素の最大値です。 568 static const int ALPHA_MIN = ELEMENT_MIN; //!< αの最小値です。 569 static const int ALPHA_MAX = ELEMENT_MAX; //!< αの最小値です。 570 static const int ALPHA_OPACITY = ALPHA_MAX; //!< 不透明のα値です。 571 static const int ALPHA_TRANSPARENT = ALPHA_MIN; //!< 透明のα値です。 572 573 /* ------------------------------------------------------------------------ 574 関数 575 ------------------------------------------------------------------------ */ 576 //---------------------------------------- 577 //! @name コンストラクタ/デストラクタ 578 //@{ 579 580 //-------------------------------------------------------------------------- 581 //! @brief デフォルトコンストラクタです。 582 //-------------------------------------------------------------------------- FloatColorFloatColor583 /* ctor */ FloatColor() 584 : r(0.0f), g(0.0f), b(0.0f), a(1.0f) 585 { 586 } 587 588 //-------------------------------------------------------------------------- 589 //! @brief VEC4を引数にとるコンストラクタです。 590 //! 591 //! @param[in] vec FloatColor に変換するベクトルです。 592 //-------------------------------------------------------------------------- FloatColorFloatColor593 /* ctor */ /* implicit */ FloatColor(const nw::math::VEC4& vec) 594 : r(vec.x), g(vec.y), b(vec.z), a(vec.w) 595 { 596 } 597 598 //-------------------------------------------------------------------------- 599 //! @brief Color8 から FloatColor に変換するコンストラクタです。 600 //! 601 //! @param[in] color RGBA8 のカラーです。 602 //-------------------------------------------------------------------------- FloatColorFloatColor603 /* ctor */ /* implicit */ FloatColor(Color8 color) { operator =(color); } 604 605 //-------------------------------------------------------------------------- 606 //! @brief RGBA をそれぞれ指定したコンストクタです。 607 //! 608 //! @param[in] red カラーの R 要素です。 609 //! @param[in] green カラーの G 要素です。 610 //! @param[in] blue カラーの B 要素です。 611 //! @param[in] alpha カラーの A 要素です。 612 //-------------------------------------------------------------------------- FloatColorFloatColor613 /* ctor */ FloatColor(f32 red, f32 green, f32 blue, f32 alpha) 614 : r(red), g(green), b(blue), a(alpha) 615 { 616 } 617 618 //-------------------------------------------------------------------------- 619 //! @brief デストラクタです。 620 //-------------------------------------------------------------------------- ~FloatColorFloatColor621 /* dtor */ ~FloatColor() {} 622 623 //@} 624 625 //---------------------------------------- 626 //! @name 演算子オーバーロード 627 //@{ 628 629 //-------------------------------------------------------------------------- 630 //! @brief RGBA8 のカラーを FloatColor に設定する代入演算子です。 631 //! 632 //! @param[in] color RGBA8 のカラーです。 633 //! 634 //! @return FloatColor のインスタンスを返します。 635 //-------------------------------------------------------------------------- 636 SelfType& operator =(Color8 color) 637 { 638 this->Set( 639 static_cast<s32>(color.r), 640 static_cast<s32>(color.g), 641 static_cast<s32>(color.b), 642 static_cast<s32>(color.a) ); 643 return *this; 644 } 645 646 //-------------------------------------------------------------------------- 647 //! @brief VEC4 型を代入する代入演算子です。 648 //! 649 //! @param[out] vec VEC4 型のインスタンスです。 650 //! 651 //! @return FloatColor のインスタンスを返します。 652 //--------------------------------------------------------------------------- 653 SelfType& operator =(const nw::math::VEC4& vec) 654 { 655 this->Set( vec.x, vec.y, vec.z, vec.w ); 656 return *this; 657 } 658 659 //-------------------------------------------------------------------------- 660 //! @brief f32 配列へのキャスト演算子です。 661 //--------------------------------------------------------------------------- 662 operator f32*() { return &r; } 663 664 //-------------------------------------------------------------------------- 665 //! @brief f32 配列へのキャスト演算子 const 版です。 666 //--------------------------------------------------------------------------- 667 operator const f32*() const { return &r; } 668 669 //-------------------------------------------------------------------------- 670 //! @brief RGBA8 カラーへのキャスト演算子です。 671 //--------------------------------------------------------------------------- Color8FloatColor672 operator Color8() const 673 { 674 return Color8( static_cast<u8>( 0.5f + nw::ut::Clamp(r, 0.0f, 1.0f) * 255.f ), 675 static_cast<u8>( 0.5f + nw::ut::Clamp(g, 0.0f, 1.0f) * 255.f ), 676 static_cast<u8>( 0.5f + nw::ut::Clamp(b, 0.0f, 1.0f) * 255.f ), 677 static_cast<u8>( 0.5f + nw::ut::Clamp(a, 0.0f, 1.0f) * 255.f )); 678 } 679 680 //-------------------------------------------------------------------------- 681 //! @brief VEC4へのキャスト演算子です。 682 //--------------------------------------------------------------------------- 683 operator nw::math::VEC4&() 684 { 685 return this->ToVEC4(); 686 } 687 688 //-------------------------------------------------------------------------- 689 //! @brief VEC4へのキャスト演算子 const 版です。 690 //--------------------------------------------------------------------------- 691 operator const nw::math::VEC4&() const 692 { 693 return this->ToVEC4(); 694 } 695 696 //---- 四則演算 697 //-------------------------------------------------------------------------- 698 //! @brief カラー同士の加算演算子です。 699 //! 700 //! @param[in] right 加算をおこなうカラー値です。 701 //! 702 //! @return 加算後の値を返します。 703 //--------------------------------------------------------------------------- 704 const SelfType operator +(const SelfType& right) const 705 { 706 SelfType color( 707 this->r + right.r, 708 this->g + right.g, 709 this->b + right.b, 710 this->a + right.a 711 ); 712 return color; 713 } 714 715 //-------------------------------------------------------------------------- 716 //! @brief カラー同士の減算演算子です。 717 //! 718 //! @param[in] right 減算をおこなうカラー値です。 719 //! 720 //! @return 減算後の値を返します。 721 //--------------------------------------------------------------------------- 722 const SelfType operator -(const SelfType& right) const 723 { 724 SelfType color( 725 this->r - right.r, 726 this->g - right.g, 727 this->b - right.b, 728 this->a - right.a 729 ); 730 return color; 731 } 732 733 //-------------------------------------------------------------------------- 734 //! @brief カラー同士の乗算演算子です。 735 //! 736 //! @param[in] right 乗算をおこなうカラー値です。 737 //! 738 //! @return 乗算後の値を返します。 739 //--------------------------------------------------------------------------- 740 const SelfType operator *(const SelfType& right) const 741 { 742 SelfType color( 743 this->r * right.r, 744 this->g * right.g, 745 this->b * right.b, 746 this->a * right.a 747 ); 748 return color; 749 } 750 751 //-------------------------------------------------------------------------- 752 //! @brief カラー同士の除算演算子です。 753 //! 754 //! @param[in] right 除算をおこなうカラー値です。 755 //! 756 //! @return 除算後の値を返します。 757 //--------------------------------------------------------------------------- 758 const SelfType operator /(const SelfType& right) const 759 { 760 SelfType color( 761 (right.r != 0) ? (this->r / right.r) : ELEMENT_MAX, 762 (right.g != 0) ? (this->g / right.g) : ELEMENT_MAX, 763 (right.b != 0) ? (this->b / right.b) : ELEMENT_MAX, 764 (right.a != 0) ? (this->a / right.a) : ELEMENT_MAX 765 ); 766 return color; 767 } 768 769 //-------------------------------------------------------------------------- 770 //! @brief カラー同士の加算代入演算子です。 771 //! 772 //! @param[in] rhs 加算をおこなうカラー値です。 773 //! 774 //! @return 加算後のオブジェクトへの参照を返します。 775 //--------------------------------------------------------------------------- 776 SelfType& operator +=(const SelfType& rhs) 777 { 778 *this = *this + rhs; 779 return *this; 780 } 781 782 //-------------------------------------------------------------------------- 783 //! @brief カラー同士の減算代入演算子です。 784 //! 785 //! @param[in] rhs 減算をおこなうカラー値です。 786 //! 787 //! @return 減算後のオブジェクトへの参照を返します。 788 //--------------------------------------------------------------------------- 789 SelfType& operator -=(const SelfType& rhs) 790 { 791 *this = *this - rhs; 792 return *this; 793 } 794 795 //-------------------------------------------------------------------------- 796 //! @brief カラー同士の乗算代入演算子です。 797 //! 798 //! @param[in] rhs 乗算をおこなうカラー値です。 799 //! 800 //! @return 乗算後のオブジェクトへの参照を返します。 801 //--------------------------------------------------------------------------- 802 SelfType& operator *=(const SelfType& rhs) 803 { 804 *this = *this * rhs; 805 return *this; 806 } 807 808 //-------------------------------------------------------------------------- 809 //! @brief カラー同士の除算代入演算子です。 810 //! 811 //! @param[in] rhs 除算をおこなうカラー値です。 812 //! 813 //! @return 除算後のオブジェクトへの参照を返します。 814 //--------------------------------------------------------------------------- 815 SelfType& operator /=(const SelfType& rhs) 816 { 817 *this = *this / rhs; 818 return *this; 819 } 820 821 //---- 四則演算 822 //-------------------------------------------------------------------------- 823 //! @brief カラーへ実数を加算する代入演算子です。 824 //! 825 //! @param[in] right RGBAそれぞれに加算する値です。 826 //! 827 //! @return 加算後のオブジェクトへの参照を返します。 828 //--------------------------------------------------------------------------- 829 SelfType& operator +=(f32 right) 830 { 831 this->r += right; 832 this->g += right; 833 this->b += right; 834 this->a += right; 835 return *this; 836 } 837 838 //-------------------------------------------------------------------------- 839 //! @brief カラーから実数を減算する代入演算子です。 840 //! 841 //! @param[in] right RGBAそれぞれに減算する値です。 842 //! 843 //! @return 減算後のオブジェクトへの参照を返します。 844 //--------------------------------------------------------------------------- 845 SelfType& operator -=(f32 right) 846 { 847 this->r -= right; 848 this->g -= right; 849 this->b -= right; 850 this->a -= right; 851 return *this; 852 } 853 854 //-------------------------------------------------------------------------- 855 //! @brief カラーへ実数を乗算する代入演算子です。 856 //! 857 //! @param[in] right RGBAそれぞれに乗算する値です。 858 //! 859 //! @return 乗算後のオブジェクトへの参照を返します。 860 //--------------------------------------------------------------------------- 861 SelfType& operator *=(f32 right) 862 { 863 this->r *= right; 864 this->g *= right; 865 this->b *= right; 866 this->a *= right; 867 return *this; 868 } 869 870 //-------------------------------------------------------------------------- 871 //! @brief カラーへ実数を減算する代入演算子です。 872 //! 873 //! @param[in] right RGBAそれぞれに減算する値です。 874 //! 875 //! @return 減算後のオブジェクトへの参照を返します。 876 //--------------------------------------------------------------------------- 877 SelfType& operator /=(f32 right) 878 { 879 if (right != 0.0f) 880 { 881 this->r /= right; 882 this->g /= right; 883 this->b /= right; 884 this->a /= right; 885 } 886 else 887 { 888 this->r = ELEMENT_MAX; 889 this->g = ELEMENT_MAX; 890 this->b = ELEMENT_MAX; 891 this->a = ELEMENT_MAX; 892 } 893 return *this; 894 } 895 896 //-------------------------------------------------------------------------- 897 //! @brief 比較演算子です。 898 //! 899 //! @param[in] rhs 比較する値です。 900 //! 901 //! @return 等しい場合は true、そうでない場合は false を返します。 902 //--------------------------------------------------------------------------- 903 bool operator ==(const SelfType& rhs) const 904 { 905 return (r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a); 906 } 907 908 //-------------------------------------------------------------------------- 909 //! @brief 否定の比較演算子です。 910 //! 911 //! @param[in] rhs 比較する値です。 912 //! 913 //! @return 等しい場合は false、そうでない場合は true を返します。 914 //--------------------------------------------------------------------------- 915 bool operator !=(const SelfType& rhs) const 916 { 917 return !(*this == rhs); 918 } 919 920 //@} 921 922 //---------------------------------------- 923 //! @name 設定/取得 924 //@{ 925 926 //---- アクセサ 927 //-------------------------------------------------------------------------- 928 //! @brief カラーに値を設定します。 929 //! 930 //! @param[in] red カラーの R 要素です。 931 //! @param[in] green カラーの G 要素です。 932 //! @param[in] blue カラーの B 要素です。 933 //! @param[in] alpha カラーの A 要素です。(デフォルト値: 1.0) 934 //--------------------------------------------------------------------------- 935 void Set( 936 f32 red, 937 f32 green, 938 f32 blue, 939 f32 alpha = ALPHA_OPACITY ) 940 { 941 r = red; 942 g = green; 943 b = blue; 944 a = alpha; 945 } 946 947 //---- アクセサ 948 //-------------------------------------------------------------------------- 949 //! @brief カラーに値を設定します。 950 //! 951 //! @param[in] red カラーの R 要素です。 952 //! @param[in] green カラーの G 要素です。 953 //! @param[in] blue カラーの B 要素です。 954 //--------------------------------------------------------------------------- SetColorFloatColor955 void SetColor( 956 f32 red, 957 f32 green, 958 f32 blue) 959 { 960 this->Set(red, green, blue, a); 961 } 962 963 //---- アクセサ 964 //-------------------------------------------------------------------------- 965 //! @brief アルファに値を設定します。 966 //! 967 //! @param[in] alpha カラーの A 要素です。 968 //--------------------------------------------------------------------------- SetAlphaFloatColor969 void SetAlpha( 970 f32 alpha) 971 { 972 this->Set(r, g, b, alpha); 973 } 974 975 //---- アクセサ 976 //-------------------------------------------------------------------------- 977 //! @brief 0~255 の整数値でカラーを設定します。 978 //! 979 //! @param[in] red カラーの R 要素です。 980 //! @param[in] green カラーの G 要素です。 981 //! @param[in] blue カラーの B 要素です。 982 //! @param[in] alpha カラーの A 要素です。(デフォルト値: 255) 983 //--------------------------------------------------------------------------- 984 void Set( 985 s32 red, 986 s32 green, 987 s32 blue, 988 s32 alpha = Color8::ALPHA_OPACITY ) 989 { 990 r = static_cast<f32>( red ) / 255.f; 991 g = static_cast<f32>( green ) / 255.f; 992 b = static_cast<f32>( blue ) / 255.f; 993 a = static_cast<f32>( alpha ) / 255.f; 994 } 995 996 //---- アクセサ 997 //-------------------------------------------------------------------------- 998 //! @brief 0~255 の整数値でカラーを設定します。 999 //! 1000 //! @param[in] red カラーの R 要素です。 1001 //! @param[in] green カラーの G 要素です。 1002 //! @param[in] blue カラーの B 要素です。 1003 //--------------------------------------------------------------------------- SetColorFloatColor1004 void SetColor( 1005 s32 red, 1006 s32 green, 1007 s32 blue) 1008 { 1009 1010 f32 fr = static_cast<f32>( red ) / 255.f; 1011 f32 fg = static_cast<f32>( green ) / 255.f; 1012 f32 fb = static_cast<f32>( blue ) / 255.f; 1013 1014 this->Set(fr, fg, fb, a); 1015 } 1016 1017 //---- アクセサ 1018 //-------------------------------------------------------------------------- 1019 //! @brief 0~255 の整数値でアルファを設定します。 1020 //! 1021 //! @param[in] alpha カラーの A 要素です。 1022 //--------------------------------------------------------------------------- SetAlphaFloatColor1023 void SetAlpha( 1024 s32 alpha) 1025 { 1026 f32 fa = static_cast<f32>( alpha ) / 255.f; 1027 1028 this->Set(r, g, b, fa); 1029 } 1030 1031 //-------------------------------------------------------------------------- 1032 //! @brief カラー型の値を設定します。 1033 //! 1034 //! @param[in] color 1035 //--------------------------------------------------------------------------- SetFloatColor1036 void Set(const SelfType& color) { operator =(color); } 1037 1038 //-------------------------------------------------------------------------- 1039 //! @brief 各要素を 0.0 ~ 1.0 の範囲にクランプします。 1040 //--------------------------------------------------------------------------- ClampFloatColor1041 void Clamp() { this->Clamp( 0.0f, 1.0f ); } 1042 1043 //-------------------------------------------------------------------------- 1044 //! @brief 各要素を指定の範囲にクランプします。 1045 //! @param[in] low 下限値です。 1046 //! @param[in] high 上限値です。 1047 //--------------------------------------------------------------------------- ClampFloatColor1048 void Clamp( f32 low, f32 high ) 1049 { 1050 r = nw::ut::Clamp( r, low, high ); 1051 g = nw::ut::Clamp( g, low, high ); 1052 b = nw::ut::Clamp( b, low, high ); 1053 a = nw::ut::Clamp( a, low, high ); 1054 } 1055 1056 //-------------------------------------------------------------------------- 1057 //! @brief 浮動小数型の配列へ変換します。 1058 //--------------------------------------------------------------------------- ToArrayFloatColor1059 f32* ToArray() { return reinterpret_cast<f32*>( this ); } 1060 1061 //-------------------------------------------------------------------------- 1062 //! @brief 浮動小数型の const 配列へ変換します。 1063 //--------------------------------------------------------------------------- ToArrayFloatColor1064 const f32* ToArray() const { return reinterpret_cast<const f32*>( this ); } 1065 1066 //-------------------------------------------------------------------------- 1067 //! @brief Picaの u32 へ変換します。 1068 //--------------------------------------------------------------------------- ToPicaU32FloatColor1069 u32 ToPicaU32() const 1070 { 1071 u8 red = static_cast<u8>( 0.5f + nw::ut::Clamp(r, 0.0f, 1.0f) * 255.f ); 1072 u8 green = static_cast<u8>( 0.5f + nw::ut::Clamp(g, 0.0f, 1.0f) * 255.f ); 1073 u8 blue = static_cast<u8>( 0.5f + nw::ut::Clamp(b, 0.0f, 1.0f) * 255.f ); 1074 u8 alpha = static_cast<u8>( 0.5f + nw::ut::Clamp(a, 0.0f, 1.0f) * 255.f ); 1075 1076 return (alpha << 24) | (blue << 16) | (green << 8) | (red); 1077 } 1078 1079 //@} 1080 1081 protected: 1082 //! @details :private ToVEC4FloatColor1083 nw::math::VEC4& ToVEC4() 1084 { 1085 return *reinterpret_cast<nw::math::VEC4*>(this); 1086 } 1087 1088 //! @details :private ToVEC4FloatColor1089 const nw::math::VEC4& ToVEC4() const 1090 { 1091 return *reinterpret_cast<const nw::math::VEC4*>(this); 1092 } 1093 1094 public: 1095 f32 r; //!< カラーの R 要素です。 1096 f32 g; //!< カラーの G 要素です。 1097 f32 b; //!< カラーの B 要素です。 1098 f32 a; //!< カラーの A 要素です。 1099 1100 }; 1101 1102 #if !defined( NW_SWAP_ENDIAN ) 1103 //! @details :private 1104 typedef FloatColor ResFloatColor; 1105 #else 1106 //! @details :private 1107 typedef struct 1108 { 1109 nw::ut::ResF32 r; 1110 nw::ut::ResF32 g; 1111 nw::ut::ResF32 b; 1112 nw::ut::ResF32 a; 1113 FloatColor__anonc3dc436601081114 operator FloatColor() const 1115 { FloatColor color; color.r = r; color.g = g; color.b = b; color.a = a; return color; } 1116 void operator = (const FloatColor& val) { r = val.r; g = val.g; b = val.b; a = val.a; } 1117 } ResFloatColor; 1118 #endif 1119 1120 1121 } /* namespace ut */ 1122 } /* namespace nw */ 1123 1124 #endif // NW_UT_COLOR_H_ 1125