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