/*---------------------------------------------------------------------------* Project: Horizon File: util_Color.h Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 31235 $ *---------------------------------------------------------------------------*/ #ifndef NN_UTIL_UTIL_COLOR_H_ #define NN_UTIL_UTIL_COLOR_H_ #include #ifdef __cplusplus namespace nn { namespace util { struct FloatColor; //-------------------------------------------------------------------------- //! @brief 整数カラーを表す構造体です。 //--------------------------------------------------------------------------- struct Color8 { public: typedef Color8 SelfType; //!< @details :private /* ------------------------------------------------------------------------ 定数 ------------------------------------------------------------------------ */ static const int ELEMENT_MIN = 0; //!< カラー要素の最小値です。 static const int ELEMENT_MAX = 255; //!< カラー要素の最大値です。 static const int ALPHA_MIN = ELEMENT_MIN; //!< alpha の最小値です。 static const int ALPHA_MAX = ELEMENT_MAX; //!< alpha の最大値です。 static const int ALPHA_OPACITY = ALPHA_MAX; //!< 不透明の alpha 値です。 static const int ALPHA_TRANSPARENT = ALPHA_MIN; //!< 透明の alpha 値です。 static const u32 BLACK = 0x000000FF; //!< 黒色の定数です。 static const u32 GRAY = 0x808080FF; //!< 灰色の定数です。 static const u32 WHITE = 0xFFFFFFFF; //!< 白色の定数です。 static const u32 RED = 0xFF0000FF; //!< 赤色の定数です。 static const u32 GREEN = 0x00FF00FF; //!< 緑色の定数です。 static const u32 BLUE = 0x0000FFFF; //!< 青色の定数です。 static const u32 YELLOW = 0xFFFF00FF; //!< 黄色の定数です。 static const u32 MAGENTA = 0xFF00FFFF; //!< マゼンダの定数です。 static const u32 CYAN = 0x00FFFFFF; //!< シアンの定数です。 /* ------------------------------------------------------------------------ 関数 ------------------------------------------------------------------------ */ //---------------------------------------- //! @name コンストラクタ/デストラクタ //@{ //-------------------------------------------------------------------------- //! @brief デフォルトコンストラクタです。 //-------------------------------------------------------------------------- /* ctor */ Color8() { this->SetU32(WHITE); } //-------------------------------------------------------------------------- //! @brief u32 の値を設定するコンストラクタです。 //-------------------------------------------------------------------------- /* ctor */ /*implicit*/ Color8(u32 color) { this->SetU32(color); } //-------------------------------------------------------------------------- //! @brief RGBA 各要素を設定するコンストラクタです。 //-------------------------------------------------------------------------- /* ctor */ /*implicit*/ Color8(s32 red, s32 green, s32 blue, s32 alpha) : r(static_cast(red)), g(static_cast(green)), b(static_cast(blue)), a(static_cast(alpha)) { } //-------------------------------------------------------------------------- //! @brief デストラクタです。 //-------------------------------------------------------------------------- /* dtor */ ~Color8() {} //@} //---------------------------------------- //! @name 演算子オーバーロード //@{ //-------------------------------------------------------------------------- //! @brief u32 型を代入する代入演算子です。 //! //! @param[out] color RGBA8 の u32 型インスタンスです。 //! //! @return Color8 のインスタンスを返します。 //--------------------------------------------------------------------------- SelfType& operator =(u32 color) { this->SetU32(color); return *this; } //-------------------------------------------------------------------------- //! @brief FloatColor 型を代入する代入演算子です。 //! //! @param[out] color FloatColor 型のカラーインスタンスです。 //! //! @return Color8 のインスタンスを返します。 //--------------------------------------------------------------------------- SelfType& operator =(FloatColor& color); //-------------------------------------------------------------------------- //! @brief u32 型へのキャスト演算子です。 //! //! @return u32 で表現した値を返します。 //--------------------------------------------------------------------------- operator u32() const { return ToU32(); } //---- 四則演算 //-------------------------------------------------------------------------- //! @brief カラー同士の加算演算子です。 //! //! @param[in] right 加算をおこなうカラー値です。 //! //! @return 加算後の値を返します。 //--------------------------------------------------------------------------- const SelfType operator +(const SelfType& right) const { SelfType color( nn::math::Min(this->r + right.r, ELEMENT_MAX), nn::math::Min(this->g + right.g, ELEMENT_MAX), nn::math::Min(this->b + right.b, ELEMENT_MAX), nn::math::Min(this->a + right.a, ELEMENT_MAX) ); return color; } //-------------------------------------------------------------------------- //! @brief カラー同士の減算演算子です。 //! //! @param[in] right 減算をおこなうカラー値です。 //! //! @return 減算後の値を返します。 //--------------------------------------------------------------------------- const SelfType operator -(const SelfType& right) const { SelfType color( nn::math::Max(this->r - right.r, ELEMENT_MIN), nn::math::Max(this->g - right.g, ELEMENT_MIN), nn::math::Max(this->b - right.b, ELEMENT_MIN), nn::math::Max(this->a - right.a, ELEMENT_MIN) ); return color; } //-------------------------------------------------------------------------- //! @brief カラー同士の乗算演算子です。 //! //! @param[in] right 乗算をおこなうカラー値です。 //! //! @return 乗算後の値を返します。 //--------------------------------------------------------------------------- const SelfType operator *(const SelfType& right) const { SelfType color( this->r * right.r / ELEMENT_MAX, this->g * right.g / ELEMENT_MAX, this->b * right.b / ELEMENT_MAX, this->a * right.a / ELEMENT_MAX ); return color; } //-------------------------------------------------------------------------- //! @brief カラー同士の除算演算子です。 //! //! @param[in] right 除算をおこなうカラー値です。 //! //! @return 除算後の値を返します。 //--------------------------------------------------------------------------- const SelfType operator /(const SelfType& right) const { SelfType color( (right.r != 0) ? nn::math::Min(this->r * ELEMENT_MAX / right.r, ELEMENT_MAX): ELEMENT_MAX, (right.g != 0) ? nn::math::Min(this->g * ELEMENT_MAX / right.g, ELEMENT_MAX): ELEMENT_MAX, (right.b != 0) ? nn::math::Min(this->b * ELEMENT_MAX / right.b, ELEMENT_MAX): ELEMENT_MAX, (right.a != 0) ? nn::math::Min(this->a * ELEMENT_MAX / right.a, ELEMENT_MAX): ELEMENT_MAX ); return color; } //---- インクリメント、デクリメント //-------------------------------------------------------------------------- //! @brief 前置のインクリメント演算子です。 //! //! @return 各要素をインクリメントした値を返します。 //--------------------------------------------------------------------------- const SelfType operator ++() { if (r < ELEMENT_MAX) { r++; } if (g < ELEMENT_MAX) { g++; } if (b < ELEMENT_MAX) { b++; } if (a < ELEMENT_MAX) { a++; } return *this; } //-------------------------------------------------------------------------- //! @brief 後置のインクリメント演算子です。 //! //! @return インクリメント前の値を返します。 //--------------------------------------------------------------------------- const SelfType operator ++(int) { SelfType old = *this; if (r < ELEMENT_MAX) { r++; } if (g < ELEMENT_MAX) { g++; } if (b < ELEMENT_MAX) { b++; } if (a < ELEMENT_MAX) { a++; } return old; } //-------------------------------------------------------------------------- //! @brief 前置のデクリメント演算子です。 //! //! @return 各要素をデクリメントした値を返します。 //--------------------------------------------------------------------------- const SelfType operator --() { if (r > ELEMENT_MIN) { r--; } if (g > ELEMENT_MIN) { g--; } if (b > ELEMENT_MIN) { b--; } if (a > ELEMENT_MIN) { a--; } return *this; } //-------------------------------------------------------------------------- //! @brief 後置のデクリメント演算子です。 //! //! @return デクリメント前の値を返します。 //--------------------------------------------------------------------------- const SelfType operator --(int) { SelfType old = *this; if (r > ELEMENT_MIN) { r--; } if (g > ELEMENT_MIN) { g--; } if (b > ELEMENT_MIN) { b--; } if (a > ELEMENT_MIN) { a--; } return old; } //---- bitごとの和と積 //-------------------------------------------------------------------------- //! @brief u32 としての ビット和を求めます。 //! //! @param[in] right ビット和をとるカラーです。 //! //! @return ビット和をとったカラーを返します。 //--------------------------------------------------------------------------- const SelfType operator |(const SelfType& right) const { return U32RawToColor(this->ToU32Raw() | right.ToU32Raw()); } //-------------------------------------------------------------------------- //! @brief u32 としての ビット積を求めます。 //! //! @param[in] right ビット積をとるカラーです。 //! //! @return ビット積をとったカラーを返します。 //--------------------------------------------------------------------------- const SelfType operator &(const SelfType& right) const { return U32RawToColor(this->ToU32Raw() & right.ToU32Raw()); } //-------------------------------------------------------------------------- //! @brief カラー同士の加算代入演算子です。 //! //! @param[in] rhs 加算をおこなうカラー値です。 //! //! @return 加算後のオブジェクトへの参照を返します。 //--------------------------------------------------------------------------- SelfType& operator +=(const SelfType& rhs) { *this = *this + rhs; return *this; } //-------------------------------------------------------------------------- //! @brief カラー同士の減算代入演算子です。 //! //! @param[in] rhs 減算をおこなうカラー値です。 //! //! @return 減算後のオブジェクトへの参照を返します。 //--------------------------------------------------------------------------- SelfType& operator -=(const SelfType& rhs) { *this = *this - rhs; return *this; } //-------------------------------------------------------------------------- //! @brief カラー同士の乗算代入演算子です。 //! //! @param[in] rhs 乗算をおこなうカラー値です。 //! //! @return 乗算後のオブジェクトへの参照を返します。 //--------------------------------------------------------------------------- SelfType& operator *=(const SelfType& rhs) { *this = *this * rhs; return *this; } //-------------------------------------------------------------------------- //! @brief カラー同士の除算代入演算子です。 //! //! @param[in] rhs 除算をおこなうカラー値です。 //! //! @return 除算後のオブジェクトへの参照を返します。 //--------------------------------------------------------------------------- SelfType& operator /=(const SelfType& rhs) { *this = *this / rhs; return *this; } //-------------------------------------------------------------------------- //! @brief u32 をカラーとして加算をおこなう演算子です。 //! //! @param[in] rhs 加算をおこなう u32 値です。 //! //! @return 加算後の値を返します。 //--------------------------------------------------------------------------- const SelfType operator +(u32 rhs) const { const SelfType right = SelfType(rhs); SelfType color( nn::math::Min(this->r + right.r, ELEMENT_MAX), nn::math::Min(this->g + right.g, ELEMENT_MAX), nn::math::Min(this->b + right.b, ELEMENT_MAX), nn::math::Min(this->a + right.a, ELEMENT_MAX) ); return color; } //-------------------------------------------------------------------------- //! @brief u32 をカラーとして減算をおこなう演算子です。 //! //! @param[in] rhs 減算をおこなう u32 値です。 //! //! @return 減算後の値を返します。 //--------------------------------------------------------------------------- const SelfType operator -(u32 rhs) const { const SelfType right = SelfType(rhs); SelfType color( nn::math::Max(this->r - right.r, ELEMENT_MIN), nn::math::Max(this->g - right.g, ELEMENT_MIN), nn::math::Max(this->b - right.b, ELEMENT_MIN), nn::math::Max(this->a - right.a, ELEMENT_MIN) ); return color; } //-------------------------------------------------------------------------- //! @brief u32 をカラーとして乗算をおこなう演算子です。 //! //! @param[in] rhs 乗算をおこなう u32 値です。 //! //! @return 乗算後の値を返します。 //--------------------------------------------------------------------------- const SelfType operator *(u32 rhs) const { const SelfType right = SelfType(rhs); SelfType color( this->r * right.r / ELEMENT_MAX, this->g * right.g / ELEMENT_MAX, this->b * right.b / ELEMENT_MAX, this->a * right.a / ELEMENT_MAX ); return color; } //-------------------------------------------------------------------------- //! @brief u32 をカラーとして除算をおこなう演算子です。 //! //! @param[in] rhs 除算をおこなう u32 値です。 //! //! @return 除算後の値を返します。 //--------------------------------------------------------------------------- const SelfType operator /(u32 rhs) const { const SelfType right = SelfType(rhs); SelfType color( (right.r != 0) ? nn::math::Min(this->r * ELEMENT_MAX / right.r, ELEMENT_MAX): ELEMENT_MAX, (right.g != 0) ? nn::math::Min(this->g * ELEMENT_MAX / right.g, ELEMENT_MAX): ELEMENT_MAX, (right.b != 0) ? nn::math::Min(this->b * ELEMENT_MAX / right.b, ELEMENT_MAX): ELEMENT_MAX, (right.a != 0) ? nn::math::Min(this->a * ELEMENT_MAX / right.a, ELEMENT_MAX): ELEMENT_MAX ); return color; } //-------------------------------------------------------------------------- //! @brief u32 をカラーとして論理和演算をおこなう演算子です。 //! //! @param[in] right 論理和演算をおこなう u32 値です。 //! //! @return 論理和演算後の値を返します。 //--------------------------------------------------------------------------- const SelfType operator |(u32 right) const { return *this | SelfType( right ); } //-------------------------------------------------------------------------- //! @brief u32 をカラーとして論理積演算をおこなう演算子です。 //! //! @param[in] right 論理積演算をおこなう u32 値です。 //! //! @return 論理積演算後の値を返します。 //--------------------------------------------------------------------------- const SelfType operator &(u32 right) const { return *this & SelfType( right ); } //@} //---------------------------------------- //! @name 設定/取得 //@{ //---- アクセサ //-------------------------------------------------------------------------- //! @brief RGBA の値を設定します。 //! //! @param[in] red カラーの R 要素です。 //! @param[in] green カラーの G 要素です。 //! @param[in] blue カラーの B 要素です。 //! @param[in] alpha カラーの A 要素です。(デフォルト値: 255) //--------------------------------------------------------------------------- void Set( s32 red, s32 green, s32 blue, s32 alpha = ALPHA_OPACITY ) { r = static_cast(red); g = static_cast(green); b = static_cast(blue); a = static_cast(alpha); } //-------------------------------------------------------------------------- //! @brief RGBA8 のカラー値を設定します。 //! //! @param[in] color 設定するカラー値です。 //--------------------------------------------------------------------------- void Set(SelfType color) { operator =(color); } //---- 明示的な型変換 #if ( NN_ENDIAN == NN_ENDIAN_VALUE_LITTLE ) //-------------------------------------------------------------------------- //! @brief u32 としてカラー値を取得します。 //! //! @return カラーを u32 へ変換した値です。 //--------------------------------------------------------------------------- u32 ToU32() const { u32 val = *reinterpret_cast(this); return (u32)( (((val) >> 24UL) & 0x000000FFUL) | (((val) >> 8UL) & 0x0000FF00UL) | (((val) << 8UL) & 0x00FF0000UL) | (((val) << 24UL) & 0xFF000000UL) ); } //-------------------------------------------------------------------------- //! @brief u32 として値を設定します。 //! //! @param[in] value u32 としてのカラー値です。 //--------------------------------------------------------------------------- void SetU32(u32 value) { this->ToU32RawRef() = (u32)( (((value) >> 24UL) & 0x000000FFUL) | (((value) >> 8UL) & 0x0000FF00UL) | (((value) << 8UL) & 0x00FF0000UL) | (((value) << 24UL) & 0xFF000000UL) ); } #else //-------------------------------------------------------------------------- //! @brief u32 としてカラー値を取得します。 //! //! @return カラーを u32 へ変換した値です。 //--------------------------------------------------------------------------- u32 ToU32() const { return this->ToU32RawRef(); } //-------------------------------------------------------------------------- //! @brief u32 として値を設定します。 //! //! @param[in] value u32 としてのカラー値です。 //--------------------------------------------------------------------------- void SetU32(u32 value) { this->ToU32RawRef() = value; } #endif //@} protected: /* ------------------------------------------------------------------------ 関数 ------------------------------------------------------------------------ */ //! @details :private static SelfType U32RawToColor(u32 value) { return *reinterpret_cast(&value); } //! @details :private u32& ToU32RawRef() { return *reinterpret_cast(this); } //! @details :private const u32& ToU32RawRef() const { return *reinterpret_cast(this); } //! @details :private u32 ToU32Raw() const { return *reinterpret_cast(this); } public: u8 r; //!< カラーの R 要素です。 0 ~ 255 の範囲で値を保持します。 u8 g; //!< カラーの G 要素です。 0 ~ 255 の範囲で値を保持します。 u8 b; //!< カラーの B 要素です。 0 ~ 255 の範囲で値を保持します。 u8 a; //!< カラーの A 要素です。 0 ~ 255 の範囲で値を保持します。 }; //! @details :private typedef Color8 Color; //-------------------------------------------------------------------------- //! @brief 浮動小数カラーを表す構造体です。 //--------------------------------------------------------------------------- struct FloatColor { public: typedef FloatColor SelfType; //!< @details :private /* ------------------------------------------------------------------------ 定数 ------------------------------------------------------------------------ */ static const int ELEMENT_MIN = 0; //!< カラー要素の最小値です。 static const int ELEMENT_MAX = 1; //!< カラー要素の最大値です。 static const int ALPHA_MIN = ELEMENT_MIN; //!< αの最小値です。 static const int ALPHA_MAX = ELEMENT_MAX; //!< αの最大値です。 static const int ALPHA_OPACITY = ALPHA_MAX; //!< 不透明のα値です。 static const int ALPHA_TRANSPARENT = ALPHA_MIN; //!< 透明のα値です。 /* ------------------------------------------------------------------------ 関数 ------------------------------------------------------------------------ */ //---------------------------------------- //! @name コンストラクタ/デストラクタ //@{ //-------------------------------------------------------------------------- //! @brief デフォルトコンストラクタです。 //-------------------------------------------------------------------------- /* ctor */ FloatColor() : r(0.0f), g(0.0f), b(0.0f), a(1.0f) { } //-------------------------------------------------------------------------- //! @brief VEC4を引数にとるコンストラクタです。 //! //! @param[in] vec FloatColor に変換するベクトルです。 //-------------------------------------------------------------------------- /* ctor */ /* implicit */ FloatColor(const nn::math::VEC4& vec) : r(vec.x), g(vec.y), b(vec.z), a(vec.w) { } //-------------------------------------------------------------------------- //! @brief Color8 から FloatColor に変換するコンストラクタです。 //! //! @param[in] color RGBA8 のカラーです。 //-------------------------------------------------------------------------- /* ctor */ /* implicit */ FloatColor(Color8 color) { operator =(color); } //-------------------------------------------------------------------------- //! @brief RGBA をそれぞれ指定したコンストクタです。 //! //! @param[in] red カラーの R 要素です。 //! @param[in] green カラーの G 要素です。 //! @param[in] blue カラーの B 要素です。 //! @param[in] alpha カラーの A 要素です。 //-------------------------------------------------------------------------- /* ctor */ FloatColor(f32 red, f32 green, f32 blue, f32 alpha) : r(red), g(green), b(blue), a(alpha) { } //-------------------------------------------------------------------------- //! @brief デストラクタです。 //-------------------------------------------------------------------------- /* dtor */ ~FloatColor() {} //@} //---------------------------------------- //! @name 演算子オーバーロード //@{ //-------------------------------------------------------------------------- //! @brief RGBA8 のカラーを FloatColor に設定する代入演算子です。 //! //! @param[in] color RGBA8 のカラーです。 //! //! @return FloatColor のインスタンスを返します。 //-------------------------------------------------------------------------- SelfType& operator =(Color8 color) { this->Set( static_cast(color.r), static_cast(color.g), static_cast(color.b), static_cast(color.a) ); return *this; } //-------------------------------------------------------------------------- //! @brief VEC4 型を代入する代入演算子です。 //! //! @param[out] vec VEC4 型のインスタンスです。 //! //! @return FloatColor のインスタンスを返します。 //--------------------------------------------------------------------------- SelfType& operator =(const nn::math::VEC4& vec) { this->Set( vec.x, vec.y, vec.z, vec.w ); return *this; } //-------------------------------------------------------------------------- //! @brief f32 配列へのキャスト演算子です。 //--------------------------------------------------------------------------- operator f32*() { return &r; } //-------------------------------------------------------------------------- //! @brief f32 配列へのキャスト演算子 const 版です。 //--------------------------------------------------------------------------- operator const f32*() const { return &r; } //-------------------------------------------------------------------------- //! @brief RGBA8 カラーへのキャスト演算子です。 //--------------------------------------------------------------------------- operator Color8() const { return Color8( static_cast( 0.5f + nn::math::Clamp(r, 0.0f, 1.0f) * 255.f ), static_cast( 0.5f + nn::math::Clamp(g, 0.0f, 1.0f) * 255.f ), static_cast( 0.5f + nn::math::Clamp(b, 0.0f, 1.0f) * 255.f ), static_cast( 0.5f + nn::math::Clamp(a, 0.0f, 1.0f) * 255.f )); } //-------------------------------------------------------------------------- //! @brief VEC4へのキャスト演算子です。 //--------------------------------------------------------------------------- operator nn::math::VEC4&() { return this->ToVEC4(); } //-------------------------------------------------------------------------- //! @brief VEC4へのキャスト演算子 const 版です。 //--------------------------------------------------------------------------- operator const nn::math::VEC4&() const { return this->ToVEC4(); } //---- 四則演算 //-------------------------------------------------------------------------- //! @brief カラー同士の加算演算子です。 //! //! @param[in] right 加算をおこなうカラー値です。 //! //! @return 加算後の値を返します。 //--------------------------------------------------------------------------- const SelfType operator +(const SelfType& right) const { SelfType color( this->r + right.r, this->g + right.g, this->b + right.b, this->a + right.a ); return color; } //-------------------------------------------------------------------------- //! @brief カラー同士の減算演算子です。 //! //! @param[in] right 減算をおこなうカラー値です。 //! //! @return 減算後の値を返します。 //--------------------------------------------------------------------------- const SelfType operator -(const SelfType& right) const { SelfType color( this->r - right.r, this->g - right.g, this->b - right.b, this->a - right.a ); return color; } //-------------------------------------------------------------------------- //! @brief カラー同士の乗算演算子です。 //! //! @param[in] right 乗算をおこなうカラー値です。 //! //! @return 乗算後の値を返します。 //--------------------------------------------------------------------------- const SelfType operator *(const SelfType& right) const { SelfType color( this->r * right.r, this->g * right.g, this->b * right.b, this->a * right.a ); return color; } //-------------------------------------------------------------------------- //! @brief カラー同士の除算演算子です。 //! //! @param[in] right 除算をおこなうカラー値です。 //! //! @return 除算後の値を返します。 //--------------------------------------------------------------------------- const SelfType operator /(const SelfType& right) const { SelfType color( (right.r != 0) ? (this->r / right.r) : ELEMENT_MAX, (right.g != 0) ? (this->g / right.g) : ELEMENT_MAX, (right.b != 0) ? (this->b / right.b) : ELEMENT_MAX, (right.a != 0) ? (this->a / right.a) : ELEMENT_MAX ); return color; } //-------------------------------------------------------------------------- //! @brief カラー同士の加算代入演算子です。 //! //! @param[in] rhs 加算をおこなうカラー値です。 //! //! @return 加算後のオブジェクトへの参照を返します。 //--------------------------------------------------------------------------- SelfType& operator +=(const SelfType& rhs) { *this = *this + rhs; return *this; } //-------------------------------------------------------------------------- //! @brief カラー同士の減算代入演算子です。 //! //! @param[in] rhs 減算をおこなうカラー値です。 //! //! @return 減算後のオブジェクトへの参照を返します。 //--------------------------------------------------------------------------- SelfType& operator -=(const SelfType& rhs) { *this = *this - rhs; return *this; } //-------------------------------------------------------------------------- //! @brief カラー同士の乗算代入演算子です。 //! //! @param[in] rhs 乗算をおこなうカラー値です。 //! //! @return 乗算後のオブジェクトへの参照を返します。 //--------------------------------------------------------------------------- SelfType& operator *=(const SelfType& rhs) { *this = *this * rhs; return *this; } //-------------------------------------------------------------------------- //! @brief カラー同士の除算代入演算子です。 //! //! @param[in] rhs 除算をおこなうカラー値です。 //! //! @return 除算後のオブジェクトへの参照を返します。 //--------------------------------------------------------------------------- SelfType& operator /=(const SelfType& rhs) { *this = *this / rhs; return *this; } //---- 四則演算 //-------------------------------------------------------------------------- //! @brief カラーへ実数を加算する代入演算子です。 //! //! @param[in] right RGBAそれぞれに加算する値です。 //! //! @return 加算後のオブジェクトへの参照を返します。 //--------------------------------------------------------------------------- SelfType& operator +=(f32 right) { this->r += right; this->g += right; this->b += right; this->a += right; return *this; } //-------------------------------------------------------------------------- //! @brief カラーから実数を減算する代入演算子です。 //! //! @param[in] right RGBAそれぞれに減算する値です。 //! //! @return 減算後のオブジェクトへの参照を返します。 //--------------------------------------------------------------------------- SelfType& operator -=(f32 right) { this->r -= right; this->g -= right; this->b -= right; this->a -= right; return *this; } //-------------------------------------------------------------------------- //! @brief カラーへ実数を乗算する代入演算子です。 //! //! @param[in] right RGBAそれぞれに乗算する値です。 //! //! @return 乗算後のオブジェクトへの参照を返します。 //--------------------------------------------------------------------------- SelfType& operator *=(f32 right) { this->r *= right; this->g *= right; this->b *= right; this->a *= right; return *this; } //-------------------------------------------------------------------------- //! @brief カラーへ実数を除算する代入演算子です。 //! //! @param[in] right RGBAそれぞれに除算する値です。 //! //! @return 除算後のオブジェクトへの参照を返します。 //--------------------------------------------------------------------------- SelfType& operator /=(f32 right) { if (right != 0.0f) { this->r /= right; this->g /= right; this->b /= right; this->a /= right; } else { this->r = ELEMENT_MAX; this->g = ELEMENT_MAX; this->b = ELEMENT_MAX; this->a = ELEMENT_MAX; } return *this; } //-------------------------------------------------------------------------- //! @brief 等号の比較演算子です。 //! //! @param[in] rhs 比較する値です。 //! //! @return 等しい場合は true、そうでない場合は false を返します。 //--------------------------------------------------------------------------- bool operator ==(const SelfType& rhs) const { return (r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a); } //-------------------------------------------------------------------------- //! @brief 否定の比較演算子です。 //! //! @param[in] rhs 比較する値です。 //! //! @return 等しい場合は false、そうでない場合は true を返します。 //--------------------------------------------------------------------------- bool operator !=(const SelfType& rhs) const { return !(*this == rhs); } //@} //---------------------------------------- //! @name 設定/取得 //@{ //---- アクセサ //-------------------------------------------------------------------------- //! @brief カラーに値を設定します。 //! //! @param[in] red カラーの R 要素です。 //! @param[in] green カラーの G 要素です。 //! @param[in] blue カラーの B 要素です。 //! @param[in] alpha カラーの A 要素です。(デフォルト値: 1.0) //--------------------------------------------------------------------------- void Set( f32 red, f32 green, f32 blue, f32 alpha = ALPHA_OPACITY ) { r = red; g = green; b = blue; a = alpha; } //---- アクセサ //-------------------------------------------------------------------------- //! @brief 0~255 の整数値でカラーを設定します。 //! //! @param[in] red カラーの R 要素です。 //! @param[in] green カラーの G 要素です。 //! @param[in] blue カラーの B 要素です。 //! @param[in] alpha カラーの A 要素です。(デフォルト値: 255) //--------------------------------------------------------------------------- void Set( s32 red, s32 green, s32 blue, s32 alpha = Color8::ALPHA_OPACITY ) { r = static_cast( red ) / 255.f; g = static_cast( green ) / 255.f;; b = static_cast( blue ) / 255.f;; a = static_cast( alpha ) / 255.f;; } //-------------------------------------------------------------------------- //! @brief カラー型の値を設定します。 //! //! @param[in] color //--------------------------------------------------------------------------- void Set(const SelfType& color) { operator =(color); } //-------------------------------------------------------------------------- //! @brief 各要素を 0.0 ~ 1.0 の範囲にクランプします。 //--------------------------------------------------------------------------- void Clamp() { this->Clamp( 0.0f, 1.0f ); } //-------------------------------------------------------------------------- //! @brief 各要素を指定の範囲にクランプします。 //! @param[in] low 下限値です。 //! @param[in] high 上限値です。 //--------------------------------------------------------------------------- void Clamp( f32 low, f32 high ) { r = nn::math::Clamp( r, low, high ); g = nn::math::Clamp( g, low, high ); b = nn::math::Clamp( b, low, high ); a = nn::math::Clamp( a, low, high ); } //-------------------------------------------------------------------------- //! @brief 浮動小数型の配列へ変換します。 //--------------------------------------------------------------------------- f32* ToArray() { return reinterpret_cast( this ); } //-------------------------------------------------------------------------- //! @brief 浮動小数型の const 配列へ変換します。 //--------------------------------------------------------------------------- const f32* ToArray() const { return reinterpret_cast( this ); } //-------------------------------------------------------------------------- //! @brief Picaの u32 へ変換します。 //--------------------------------------------------------------------------- u32 ToPicaU32() const { u8 red = static_cast( 0.5f + nn::math::Clamp(r, 0.0f, 1.0f) * 255.f ); u8 green = static_cast( 0.5f + nn::math::Clamp(g, 0.0f, 1.0f) * 255.f ); u8 blue = static_cast( 0.5f + nn::math::Clamp(b, 0.0f, 1.0f) * 255.f ); u8 alpha = static_cast( 0.5f + nn::math::Clamp(a, 0.0f, 1.0f) * 255.f ); return (alpha << 24) | (blue << 16) | (green << 8) | (red); } //@} protected: //! @details :private nn::math::VEC4& ToVEC4() { return *reinterpret_cast(this); } //! @details :private const nn::math::VEC4& ToVEC4() const { return *reinterpret_cast(this); } public: f32 r; //!< カラーの R 要素です。 f32 g; //!< カラーの G 要素です。 f32 b; //!< カラーの B 要素です。 f32 a; //!< カラーの A 要素です。 }; #if !defined( NN_SWAP_ENDIAN ) //! @details :private typedef FloatColor ResFloatColor; #else //! @details :private typedef struct { nw::ut::ResF32 r; nw::ut::ResF32 g; nw::ut::ResF32 b; nw::ut::ResF32 a; operator FloatColor() const { FloatColor color; color.r = r; color.g = g; color.b = b; color.a = a; return color; } void operator = (const FloatColor& val) { r = val.r; g = val.g; b = val.b; a = val.a; } } ResFloatColor; #endif } /* namespace util */ } /* namespace nn */ #endif // __cplusplus #endif // NN_UTIL_UTIL_COLOR_H_