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

Color4c.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_4C_H_
00026 #define COLOR_4C_H_
00027 
00028 #include <Core/System/Math.h>
00029 
00030 namespace Lamp{
00031 
00032 class Color3c;
00033 class Color3f;
00034 class Color4f;
00035 
00036 //------------------------------------------------------------------------------
00037 /**
00038  * 四要素キャラクタカラー
00039  *
00040  * このクラスは継承しないで下さい。
00041  */
00042 class Color4c{
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             u_char a;
00059         };
00060 
00061         /// 配列
00062         u_char array[4];
00063     };
00064 
00065     //--------------------------------------------------------------------------
00066     // 定数
00067     //--------------------------------------------------------------------------
00068     /// 白
00069     static const Color4c white;
00070 
00071     /// 灰色
00072     static const Color4c gray;
00073 
00074     /// 黒
00075     static const Color4c black;
00076 
00077     /// 赤
00078     static const Color4c red;
00079 
00080     /// 緑
00081     static const Color4c green;
00082 
00083     /// 青
00084     static const Color4c blue;
00085 
00086     /// 黄
00087     static const Color4c yellow;
00088 
00089     /// 青緑
00090     static const Color4c cyan;
00091 
00092     /// 赤紫
00093     static const Color4c magenta;
00094 
00095     //--------------------------------------------------------------------------
00096     // コンストラクタ
00097     //--------------------------------------------------------------------------
00098     /**
00099      * コンストラクタ
00100      *
00101      * このコンストラクタは初期値の設定を行わないため値は不定です。
00102      */
00103     Color4c(){}
00104 
00105     /**
00106      * コンストラクタ
00107      * @param sourceR 赤の初期値
00108      * @param sourceG 緑の初期値
00109      * @param sourceB 青の初期値
00110      * @param sourceA アルファの初期値
00111      */
00112     inline Color4c(u_char sourceR, u_char sourceG, u_char sourceB,
00113         u_char sourceA = 255) :
00114         r(sourceR), g(sourceG), b(sourceB), a(sourceA){
00115     }
00116 
00117     /**
00118      * コンストラクタ
00119      * @param source 設定する色
00120      */
00121     explicit Color4c(const Color3c& source);
00122 
00123     /**
00124      * コンストラクタ
00125      * @param source 設定する色
00126      */
00127     explicit Color4c(const Color3f& source);
00128 
00129     /**
00130      * コンストラクタ
00131      * @param source 設定する色
00132      */
00133     explicit Color4c(const Color4f& source);
00134 
00135     //--------------------------------------------------------------------------
00136     // 値の設定
00137     //--------------------------------------------------------------------------
00138     /**
00139      * 値の設定
00140      * @param sourceR 赤の設定値
00141      * @param sourceG 緑の設定値
00142      * @param sourceB 青の設定値
00143      * @param sourceA アルファの設定値
00144      */
00145     inline void set(u_char sourceR, u_char sourceG, u_char sourceB,
00146         u_char sourceA = 255){
00147         r = sourceR;
00148         g = sourceG;
00149         b = sourceB;
00150         a = sourceA;
00151     }
00152 
00153     /**
00154      * 三要素キャラクタカラーの設定
00155      * @param source 設定する色
00156      */
00157     void set(const Color3c& source);
00158 
00159     /**
00160      * 三要素実数カラーの設定
00161      * @param source 設定する色
00162      */
00163     void set(const Color3f& source);
00164 
00165     /**
00166      * 四要素実数カラーの設定
00167      * @param source 設定する色
00168      */
00169     void set(const Color4f& source);
00170 
00171     /**
00172      * ARGBカラーの設定
00173      * @param source
00174      */
00175     void setARGB(u_int source){
00176         r = (u_char)((source & 0xff0000) >> 16);
00177         g = (u_char)((source & 0xff00) >> 8);
00178         b = (u_char)(source & 0xff);
00179         a = (u_char)((source & 0xff000000) >> 24);
00180     }
00181 
00182     //--------------------------------------------------------------------------
00183     // 値の取得
00184     //--------------------------------------------------------------------------
00185     /**
00186      * ARGBカラーの取得
00187      * @return ARGBカラー
00188      */
00189     u_int getARGB() const{
00190         return (((u_int)a << 24) | ((u_int)r << 16) |
00191             ((u_int)g << 8) | ((u_int)b));
00192     }
00193 
00194     //--------------------------------------------------------------------------
00195     // 演算
00196     //--------------------------------------------------------------------------
00197     /**
00198      * 加算
00199      * @param addColor 加算する色
00200      * @return 加算された色
00201      */
00202     inline Color4c operator +(const Color4c& addColor) const{
00203         int addR = (int)r + addColor.r;
00204         if(addR > 255){ addR = 255; }
00205         int addG = (int)g + addColor.g;
00206         if(addG > 255){ addG = 255; }
00207         int addB = (int)b + addColor.b;
00208         if(addB > 255){ addB = 255; }
00209         int addA = (int)a + addColor.a;
00210         if(addA > 255){ addA = 255; }
00211         return Color4c(addR, addG, addB, addA);
00212     }
00213 
00214     /**
00215      * 減算
00216      * @param subColor 減算する色
00217      * @return 減算された色
00218      */
00219     inline Color4c operator -(const Color4c& subColor) const{
00220         int subR = (int)r - subColor.r;
00221         if(subR < 0){ subR = 0; }
00222         int subG = (int)g - subColor.g;
00223         if(subG < 0){ subG = 0; }
00224         int subB = (int)b - subColor.b;
00225         if(subB < 0){ subB = 0; }
00226         int subA = (int)a - subColor.a;
00227         if(subA < 0){ subA = 0; }
00228         return Color4c(subR, subG, subB, subA);
00229     }
00230 
00231     /**
00232      * 乗算
00233      * @param mulColor 乗算する色
00234      * @return 乗算された色
00235      */
00236     inline Color4c operator *(const Color4c& mulColor) const{
00237         int mulR = ((int)r * mulColor.r) / 255;
00238         int mulG = ((int)g * mulColor.g) / 255;
00239         int mulB = ((int)b * mulColor.b) / 255;
00240         int mulA = ((int)a * mulColor.a) / 255;
00241         return Color4c(mulR, mulG, mulB, mulA);
00242     }
00243 
00244     /**
00245      * 乗算
00246      * @param mulValue 乗算する値
00247      * @return 乗算された色
00248      */
00249     inline Color4c operator *(float mulValue) const{
00250         int mulR = (int)(r * mulValue);
00251         if(mulR > 255){
00252             mulR = 255;
00253         }else if(mulR < 0){
00254             mulR = 0;
00255         }
00256         int mulG = (int)(g * mulValue);
00257         if(mulG > 255){
00258             mulG = 255;
00259         } else if(mulG < 0){
00260             mulG = 0;
00261         }
00262         int mulB = (int)(b * mulValue);
00263         if(mulB > 255){
00264             mulB = 255;
00265         }else if(mulB < 0){
00266             mulB = 0;
00267         }
00268         int mulA = (int)(a * mulValue);
00269         if(mulA > 255){
00270             mulA = 255;
00271         }else if(mulA < 0){
00272             mulA = 0;
00273         }
00274         return Color4c(mulR, mulG, mulB, mulA);
00275     }
00276 
00277     /**
00278      * 乗算
00279      * @param mulValue 乗算する値
00280      * @param mulColor 乗算される色
00281      * @return 乗算された色
00282      */
00283     inline friend Color4c operator *(float mulValue, const Color4c& mulColor){
00284         int mulR = (int)(mulColor.r * mulValue);
00285         if(mulR > 255){
00286             mulR = 255;
00287         }else if(mulR < 0){
00288             mulR = 0;
00289         }
00290         int mulG = (int)(mulColor.g * mulValue);
00291         if(mulG > 255){
00292             mulG = 255;
00293         } else if(mulG < 0){
00294             mulG = 0;
00295         }
00296         int mulB = (int)(mulColor.b * mulValue);
00297         if(mulB > 255){
00298             mulB = 255;
00299         }else if(mulB < 0){
00300             mulB = 0;
00301         }
00302         int mulA = (int)(mulColor.a * mulValue);
00303         if(mulA > 255){
00304             mulA = 255;
00305         }else if(mulA < 0){
00306             mulA = 0;
00307         }
00308         return Color4c(mulR, mulG, mulB, mulA);
00309     }
00310 
00311     //--------------------------------------------------------------------------
00312     // 代入演算
00313     //--------------------------------------------------------------------------
00314     /**
00315      * 代入加算
00316      * @param addColor 加算する色
00317      * @return 加算された色
00318      */
00319     inline Color4c& operator +=(const Color4c& addColor){
00320         int addR = (int)r + addColor.r;
00321         if(addR > 255){ addR = 255; }
00322         int addG = (int)g + addColor.g;
00323         if(addG > 255){ addG = 255; }
00324         int addB = (int)b + addColor.b;
00325         if(addB > 255){ addB = 255; }
00326         int addA = (int)a + addColor.a;
00327         if(addA > 255){ addA = 255; }
00328         set(addR, addG, addB, addA);
00329         return (*this);
00330     }
00331 
00332     /**
00333      * 代入減算
00334      * @param subColor 減算する色
00335      * @return 減算された色
00336      */
00337     inline Color4c& operator -=(const Color4c& subColor){
00338         int subR = (int)r - subColor.r;
00339         if(subR < 0){ subR = 0; }
00340         int subG = (int)g - subColor.g;
00341         if(subG < 0){ subG = 0; }
00342         int subB = (int)b - subColor.b;
00343         if(subB < 0){ subB = 0; }
00344         int subA = (int)a - subColor.a;
00345         if(subA < 0){ subA = 0; }
00346         set(subR, subG, subB, subA);
00347         return (*this);
00348     }
00349 
00350     /**
00351      * 代入乗算
00352      * @param mulColor 乗算する色
00353      * @return 乗算された色
00354      */
00355     inline Color4c& operator *=(const Color4c& mulColor){
00356         int mulR = ((int)r * mulColor.r) / 255;
00357         int mulG = ((int)g * mulColor.g) / 255;
00358         int mulB = ((int)b * mulColor.b) / 255;
00359         int mulA = ((int)a * mulColor.a) / 255;
00360         set(mulR, mulG, mulB, mulA);
00361         return (*this);
00362     }
00363 
00364     /**
00365      * 代入乗算
00366      * @param mulValue 乗算する値
00367      * @return 乗算された色
00368      */
00369     inline Color4c& operator *=(float mulValue){
00370         int mulR = (int)(r * mulValue);
00371         if(mulR > 255){
00372             mulR = 255;
00373         }else if(mulR < 0){
00374             mulR = 0;
00375         }
00376         int mulG = (int)(g * mulValue);
00377         if(mulG > 255){
00378             mulG = 255;
00379         } else if(mulG < 0){
00380             mulG = 0;
00381         }
00382         int mulB = (int)(b * mulValue);
00383         if(mulB > 255){
00384             mulB = 255;
00385         }else if(mulB < 0){
00386             mulB = 0;
00387         }
00388         int mulA = (int)(a * mulValue);
00389         if(mulA > 255){
00390             mulA = 255;
00391         }else if(mulA < 0){
00392             mulA = 0;
00393         }
00394         set(mulR, mulG, mulB, mulA);
00395         return (*this);
00396     }
00397 
00398     //--------------------------------------------------------------------------
00399     // 色演算
00400     //--------------------------------------------------------------------------
00401     /**
00402      * 反対色
00403      *
00404      * アルファ値は変化しません。
00405      * @return 反転された色
00406      */
00407     inline Color4c& negative(){
00408         set(255 - r, 255 - g, 255 - b, a);
00409         return (*this);
00410     }
00411 
00412     /**
00413      * 色の線形補間
00414      * @param source 開始色
00415      * @param target 対象色
00416      * @param alpha ブレンド係数
00417      * @return 線形補間された色
00418      */
00419     inline static Color4c lerp(
00420         const Color4c& source, const Color4c& target, float alpha){
00421         float beta = 1.f - alpha;
00422         Color4c result;
00423         result.r = (u_char)(source.r * beta + target.r * alpha);
00424         result.g = (u_char)(source.g * beta + target.g * alpha);
00425         result.b = (u_char)(source.b * beta + target.b * alpha);
00426         result.a = (u_char)(source.a * beta + target.a * alpha);
00427         return result;
00428     }
00429 
00430     //--------------------------------------------------------------------------
00431     // 論理演算
00432     //--------------------------------------------------------------------------
00433     /**
00434      * 同じ値かどうか
00435      * @param target 比較するカラー
00436      * @return 同じ値であればtrueを返す
00437      */
00438     inline bool operator ==(const Color4c& target) const{
00439         return ((r == target.r) && (g == target.g) &&
00440             (b == target.b) && (a == target.a));
00441     }
00442 
00443     /**
00444      * 同じ値でないかどうか
00445      * @param target 比較するカラー
00446      * @return 同じ値でなければtrueを返す
00447      */
00448     inline bool operator !=(const Color4c& target) const{
00449         return ((r != target.r) || (g != target.g) ||
00450             (b != target.b) || (a != target.a));
00451     }
00452 
00453     //--------------------------------------------------------------------------
00454     // その他
00455     //--------------------------------------------------------------------------
00456     /**
00457      * 文字列化
00458      * @return カラーの文字列表記
00459      */
00460     inline String toString() const{
00461         String returnString;
00462         returnString.format("( %d, %d, %d, %d )", r, g, b, a);
00463         return returnString;
00464     }
00465 
00466     //--------------------------------------------------------------------------
00467 private:
00468 
00469 };
00470 
00471 //------------------------------------------------------------------------------
00472 } // End of namespace Lamp
00473 #endif // End of COLOR_4C_H_
00474 //------------------------------------------------------------------------------

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