Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

Color3c.h

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * 三要素キャラクタカラーヘッダ
00022  * @author Junpee
00023  */
00024 
00025 #ifndef COLOR_3C_H_
00026 #define COLOR_3C_H_
00027 
00028 #include <Core/System/Math.h>
00029 
00030 namespace Lamp{
00031 
00032 class Color4c;
00033 class Color3f;
00034 class Color4f;
00035 
00036 //------------------------------------------------------------------------------
00037 /**
00038  * 三要素キャラクタカラー
00039  *
00040  * このクラスは継承しないで下さい。
00041  */
00042 class Color3c{
00043 public:
00044     //--------------------------------------------------------------------------
00045     // メンバ変数
00046     //--------------------------------------------------------------------------
00047     /// メンバ変数
00048     union{
00049         /// 各要素
00050         struct{
00051             /// 赤
00052             u_char r;
00053             /// 緑
00054             u_char g;
00055             /// 青
00056             u_char b;
00057         };
00058 
00059         /// 配列
00060         u_char array[3];
00061     };
00062 
00063     //--------------------------------------------------------------------------
00064     // 定数
00065     //--------------------------------------------------------------------------
00066     /// 白
00067     static const Color3c white;
00068 
00069     /// 灰色
00070     static const Color3c gray;
00071 
00072     /// 黒
00073     static const Color3c black;
00074 
00075     /// 赤
00076     static const Color3c red;
00077 
00078     /// 緑
00079     static const Color3c green;
00080 
00081     /// 青
00082     static const Color3c blue;
00083 
00084     /// 黄
00085     static const Color3c yellow;
00086 
00087     /// 青緑
00088     static const Color3c cyan;
00089 
00090     /// 赤紫
00091     static const Color3c magenta;
00092 
00093     //--------------------------------------------------------------------------
00094     // コンストラクタ
00095     //--------------------------------------------------------------------------
00096     /**
00097      * コンストラクタ
00098      *
00099      * このコンストラクタは初期値の設定を行わないため値は不定です。
00100      */
00101     Color3c(){}
00102 
00103     /**
00104      * コンストラクタ
00105      * @param sourceR 赤の初期値
00106      * @param sourceG 緑の初期値
00107      * @param sourceB 青の初期値
00108      */
00109     inline Color3c(u_char sourceR, u_char sourceG, u_char sourceB) :
00110         r(sourceR), g(sourceG), b(sourceB){
00111     }
00112 
00113     /**
00114      * コンストラクタ
00115      * @param source 設定する色
00116      */
00117     explicit Color3c(const Color4c& source);
00118 
00119     /**
00120      * コンストラクタ
00121      * @param source 設定する色
00122      */
00123     explicit Color3c(const Color3f& source);
00124 
00125     /**
00126      * コンストラクタ
00127      * @param source 設定する色
00128      */
00129     explicit Color3c(const Color4f& source);
00130 
00131     //--------------------------------------------------------------------------
00132     // 値の設定
00133     //--------------------------------------------------------------------------
00134     /**
00135      * 値の設定
00136      * @param sourceR 赤の設定値
00137      * @param sourceG 緑の設定値
00138      * @param sourceB 青の設定値
00139      */
00140     inline void set(u_char sourceR, u_char sourceG, u_char sourceB){
00141         r = sourceR;
00142         g = sourceG;
00143         b = sourceB;
00144     }
00145 
00146     /**
00147      * 四要素キャラクタカラーの設定
00148      * @param source 設定する色
00149      */
00150     void set(const Color4c& source);
00151 
00152     /**
00153      * 三要素実数カラーの設定
00154      * @param source 設定する色
00155      */
00156     void set(const Color3f& source);
00157 
00158     /**
00159      * 四要素実数カラーの設定
00160      * @param source 設定する色
00161      */
00162     void set(const Color4f& source);
00163 
00164     /**
00165      * ARGBカラーの設定
00166      * @param source
00167      */
00168     void setARGB(u_int source){
00169         r = (u_char)((source & 0xff0000) >> 16);
00170         g = (u_char)((source & 0xff00) >> 8);
00171         b = (u_char)(source & 0xff);
00172     }
00173 
00174     //--------------------------------------------------------------------------
00175     // 値の取得
00176     //--------------------------------------------------------------------------
00177     /**
00178      * ARGBカラーの取得
00179      * @return ARGBカラー
00180      */
00181     u_int getARGB() const{
00182         return (0xff000000 | ((u_int)r << 16) | ((u_int)g << 8) | ((u_int)b));
00183     }
00184 
00185     //--------------------------------------------------------------------------
00186     // 演算
00187     //--------------------------------------------------------------------------
00188     /**
00189      * 加算
00190      * @param addColor 加算する色
00191      * @return 加算された色
00192      */
00193     inline Color3c operator +(const Color3c& addColor) const{
00194         int addR = (int)r + addColor.r;
00195         if(addR > 255){ addR = 255; }
00196         int addG = (int)g + addColor.g;
00197         if(addG > 255){ addG = 255; }
00198         int addB = (int)b + addColor.b;
00199         if(addB > 255){ addB = 255; }
00200         return Color3c(addR, addG, addB);
00201     }
00202 
00203     /**
00204      * 減算
00205      * @param subColor 減算する色
00206      * @return 減算された色
00207      */
00208     inline Color3c operator -(const Color3c& subColor) const{
00209         int subR = (int)r - subColor.r;
00210         if(subR < 0){ subR = 0; }
00211         int subG = (int)g - subColor.g;
00212         if(subG < 0){ subG = 0; }
00213         int subB = (int)b - subColor.b;
00214         if(subB < 0){ subB = 0; }
00215         return Color3c(subR, subG, subB);
00216     }
00217 
00218     /**
00219      * 乗算
00220      * @param mulColor 乗算する色
00221      * @return 乗算された色
00222      */
00223     inline Color3c operator *(const Color3c& mulColor) const{
00224         int mulR = ((int)r * mulColor.r) / 255;
00225         int mulG = ((int)g * mulColor.g) / 255;
00226         int mulB = ((int)b * mulColor.b) / 255;
00227         return Color3c(mulR, mulG, mulB);
00228     }
00229 
00230     /**
00231      * 乗算
00232      * @param mulValue 乗算する値
00233      * @return 乗算された色
00234      */
00235     inline Color3c operator *(float mulValue) const{
00236         int mulR = (int)(r * mulValue);
00237         if(mulR > 255){
00238             mulR = 255;
00239         }else if(mulR < 0){
00240             mulR = 0;
00241         }
00242         int mulG = (int)(g * mulValue);
00243         if(mulG > 255){
00244             mulG = 255;
00245         } else if(mulG < 0){
00246             mulG = 0;
00247         }
00248         int mulB = (int)(b * mulValue);
00249         if(mulB > 255){
00250             mulB = 255;
00251         }else if(mulB < 0){
00252             mulB = 0;
00253         }
00254         return Color3c(mulR, mulG, mulB);
00255     }
00256 
00257     /**
00258      * 乗算
00259      * @param mulValue 乗算する値
00260      * @param mulColor 乗算される色
00261      * @return 乗算された色
00262      */
00263     inline friend Color3c operator *(float mulValue, const Color3c& mulColor){
00264         int mulR = (int)(mulColor.r * mulValue);
00265         if(mulR > 255){
00266             mulR = 255;
00267         }else if(mulR < 0){
00268             mulR = 0;
00269         }
00270         int mulG = (int)(mulColor.g * mulValue);
00271         if(mulG > 255){
00272             mulG = 255;
00273         } else if(mulG < 0){
00274             mulG = 0;
00275         }
00276         int mulB = (int)(mulColor.b * mulValue);
00277         if(mulB > 255){
00278             mulB = 255;
00279         }else if(mulB < 0){
00280             mulB = 0;
00281         }
00282         return Color3c(mulR, mulG, mulB);
00283     }
00284 
00285     //--------------------------------------------------------------------------
00286     // 代入演算
00287     //--------------------------------------------------------------------------
00288     /**
00289      * 代入加算
00290      * @param addColor 加算する色
00291      * @return 加算された色
00292      */
00293     inline Color3c& operator +=(const Color3c& addColor){
00294         int addR = (int)r + addColor.r;
00295         if(addR > 255){ addR = 255; }
00296         int addG = (int)g + addColor.g;
00297         if(addG > 255){ addG = 255; }
00298         int addB = (int)b + addColor.b;
00299         if(addB > 255){ addB = 255; }
00300         set(addR, addG, addB);
00301         return (*this);
00302     }
00303 
00304     /**
00305      * 代入減算
00306      * @param subColor 減算する色
00307      * @return 減算された色
00308      */
00309     inline Color3c& operator -=(const Color3c& subColor){
00310         int subR = (int)r - subColor.r;
00311         if(subR < 0){ subR = 0; }
00312         int subG = (int)g - subColor.g;
00313         if(subG < 0){ subG = 0; }
00314         int subB = (int)b - subColor.b;
00315         if(subB < 0){ subB = 0; }
00316         set(subR, subG, subB);
00317         return (*this);
00318     }
00319 
00320     /**
00321      * 代入乗算
00322      * @param mulColor 乗算する色
00323      * @return 乗算された色
00324      */
00325     inline Color3c& operator *=(const Color3c& mulColor){
00326         int mulR = ((int)r * mulColor.r) / 255;
00327         int mulG = ((int)g * mulColor.g) / 255;
00328         int mulB = ((int)b * mulColor.b) / 255;
00329         set(mulR, mulG, mulB);
00330         return (*this);
00331     }
00332 
00333     /**
00334      * 代入乗算
00335      * @param mulValue 乗算する値
00336      * @return 乗算された色
00337      */
00338     inline Color3c& operator *=(float mulValue){
00339         int mulR = (int)(r * mulValue);
00340         if(mulR > 255){
00341             mulR = 255;
00342         }else if(mulR < 0){
00343             mulR = 0;
00344         }
00345         int mulG = (int)(g * mulValue);
00346         if(mulG > 255){
00347             mulG = 255;
00348         } else if(mulG < 0){
00349             mulG = 0;
00350         }
00351         int mulB = (int)(b * mulValue);
00352         if(mulB > 255){
00353             mulB = 255;
00354         }else if(mulB < 0){
00355             mulB = 0;
00356         }
00357         set(mulR, mulG, mulB);
00358         return (*this);
00359     }
00360 
00361     //--------------------------------------------------------------------------
00362     // 色演算
00363     //--------------------------------------------------------------------------
00364     /**
00365      * 反対色
00366      * @return 反転された色
00367      */
00368     inline Color3c& negative(){
00369         set(255 - r, 255 - g, 255 - b);
00370         return (*this);
00371     }
00372 
00373     /**
00374      * 色の線形補間
00375      * @param source 開始色
00376      * @param target 対象色
00377      * @param alpha ブレンド係数
00378      * @return 線形補間された色
00379      */
00380     inline static Color3c lerp(
00381         const Color3c& source, const Color3c& target, float alpha){
00382         float beta = 1.f - alpha;
00383         Color3c result;
00384         result.r = (u_char)(source.r * beta + target.r * alpha);
00385         result.g = (u_char)(source.g * beta + target.g * alpha);
00386         result.b = (u_char)(source.b * beta + target.b * alpha);
00387         return result;
00388     }
00389 
00390     //--------------------------------------------------------------------------
00391     // 論理演算
00392     //--------------------------------------------------------------------------
00393     /**
00394      * 同じ値かどうか
00395      * @param target 比較するカラー
00396      * @return 同じ値であればtrueを返す
00397      */
00398     inline bool operator ==(const Color3c& target) const{
00399         return ((r == target.r) && (g == target.g) && (b == target.b));
00400     }
00401 
00402     /**
00403      * 同じ値でないかどうか
00404      * @param target 比較するカラー
00405      * @return 同じ値でなければtrueを返す
00406      */
00407     inline bool operator !=(const Color3c& target) const{
00408         return ((r != target.r) || (g != target.g) || (b != target.b));
00409     }
00410 
00411     //--------------------------------------------------------------------------
00412     // その他
00413     //--------------------------------------------------------------------------
00414     /**
00415      * 文字列化
00416      * @return ベクトルの文字列表記
00417      */
00418     inline String toString() const{
00419         String returnString;
00420         returnString.format("( %d, %d, %d )", r, g, b);
00421         return returnString;
00422     }
00423 
00424     //--------------------------------------------------------------------------
00425 private:
00426 
00427 };
00428 
00429 //------------------------------------------------------------------------------
00430 } // End of namespace Lamp
00431 #endif // End of COLOR_3C_H_
00432 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:29 2005 for Lamp by doxygen 1.3.2