OSDN Git Service

fix cmake/Macros.cmake
[moflib/moflib.git] / moflib-1.0 / moflib / moflib / mof / Color.cpp
1 #include "mof/Color.hpp"
2 #include <d3dx9.h>
3 #include "mof/ConsoleIO.hpp"
4 #include "mof/tstring.hpp"
5
6
7 mof::Color4f::Color4f() : 
8         alpha(0) , red(1) , green(1) , blue(1)
9 {
10 }
11
12 mof::Color4f::Color4f(float alpha_ , float red_ , float green_ , float blue_) : 
13         alpha(alpha_) , red(red_) , green(green_) , blue(blue_)
14 {
15 }
16
17 mof::Color4f::Color4f( float red_ , float green_ , float blue_) :       
18         alpha(1) , red(red_) , green(green_) , blue(blue_)              
19 {
20 }
21
22 mof::Color4f::Color4f(mof::Color color) : 
23         alpha(static_cast<float>(getAlpha(color)) / 255) ,
24         red(static_cast<float>(getRed(color)) / 255) , 
25         green(static_cast<float>(getGreen(color)) / 255) ,
26         blue(static_cast<float>(getBlue(color)) / 255 )
27 {
28 }
29
30 mof::Color mof::Color4f::toColorCode() const{
31         return createColor(
32                 static_cast<ColorChannel>(alpha * 255) ,
33                 static_cast<ColorChannel>(red * 255) ,
34                 static_cast<ColorChannel>(green * 255) ,
35                 static_cast<ColorChannel>(blue * 255) 
36                 );
37 }
38
39 mof::Color4f mof::Color4f::operator +(const mof::Color4f& rhs) const{
40         return mof::Color4f(
41                 alpha + rhs.alpha , 
42                 red + rhs.red ,
43                 green + rhs.green ,
44                 blue + rhs.blue );
45 }
46
47 mof::Color4f mof::Color4f::operator -(const mof::Color4f& rhs) const{
48         return mof::Color4f(
49                 alpha - rhs.alpha , 
50                 red - rhs.red ,
51                 green - rhs.green ,
52                 blue - rhs.blue );
53 }
54
55 mof::Color4f mof::Color4f::operator *(const Color4f& color ) const
56 {
57         return mof::Color4f(color.alpha * alpha , color.red * red , color.green * green , color.blue * blue );
58 }
59
60 mof::Color4f mof::operator *(const Color4f& color , float f){
61         return mof::Color4f(color.alpha * f , color.red * f , color.green * f , color.blue * f);
62 }
63                 
64 mof::Color4f mof::operator *(float f ,  const mof::Color4f & color){
65         return mof::Color4f(color.alpha * f , color.red * f , color.green * f , color.blue * f);
66 }
67
68
69
70 unsigned char hex2Decimal(const TCHAR* code){
71         unsigned char a = *code;
72         if(a < '0')return 0;//error
73         else if( a <= '9')return a - '0';
74         else if( a < 'A')return 0;//error
75         else if( a <=  'Z')return a - 'A'+10;
76         else if( a < 'a')return 0;//error
77         else if( a <= 'z')return a - 'a'+10;
78         else return 0;//error
79 }
80
81 char decimal2hex(unsigned char n){
82         if( n >= 16)return '-';//error
83         return "0123456789abcdef"[n];
84 }
85
86 mof::Color mof::createColor(unsigned char r , unsigned char g , unsigned char b){
87         return D3DCOLOR_XRGB(r , g , b);
88 }
89
90 mof::Color mof::createColor(unsigned char a , unsigned char r , unsigned char g , unsigned char b){
91         return D3DCOLOR_ARGB(a , r , g , b);
92 }
93
94
95 mof::Color mof::createColor(const mof::tstring& code){
96         const TCHAR* pBuffer = code.c_str();
97         unsigned char a , r , g , b;
98         if(code.size() == 8)a = ( hex2Decimal(pBuffer++) << 4 ) + hex2Decimal(pBuffer++);
99         else if(code.size() != 6)return 0;//error
100         else a = 0xff;
101         r = (hex2Decimal(pBuffer++) << 4)  + hex2Decimal(pBuffer++);
102         g = (hex2Decimal(pBuffer++) << 4)  + hex2Decimal(pBuffer++);
103         b = (hex2Decimal(pBuffer++) << 4)  + hex2Decimal(pBuffer++);
104         return createColor(a , r , g , b);
105 }
106
107 unsigned char mof::getAlpha(const mof::Color& color){
108         return (unsigned char)((color >> 24) & 0xff);
109 }
110
111
112 unsigned char mof::getRed(const mof::Color& color){
113         return (unsigned char)((color >> 16) & 0xff);
114 }
115
116 unsigned char mof::getGreen(const mof::Color& color){
117         return (unsigned char)((color >> 8) & 0xff);
118 }
119
120
121 unsigned char mof::getBlue(const mof::Color& color){
122         return (unsigned char)(color & 0xff);
123 }
124
125
126 mof::Color mof::blendColor( mof::Color a , mof::Color b , float rate ){
127         return createColor(
128                 static_cast<unsigned char>(getAlpha(a) * rate + getAlpha(b) * (1 - rate)) , //alpha
129                 static_cast<unsigned char>(getRed(a) * rate + getRed(b) * (1 - rate)) , //red
130                 static_cast<unsigned char>(getGreen(a) * rate + getGreen(b) * (1 - rate)) , //green
131                 static_cast<unsigned char>(getBlue(a) * rate + getBlue(b) * (1 - rate)) ); //blue
132 }
133
134 mof::Color mof::inverseColor(mof::Color src){
135         return createColor(
136                 255 - getAlpha(src)  , //alpha
137                 255 - getRed(src)  , //red
138                 255 - getGreen(src)  , //green
139                 255 - getBlue(src) ); //blue
140 }
141
142
143 mof::tstring mof::toColorCode(mof::Color color){
144         mof::otstringstream os;
145         unsigned char a = getAlpha(color);
146         unsigned char r = getRed(color);
147         unsigned char g = getGreen(color);
148         unsigned char b = getBlue(color);
149         os  << decimal2hex(a >> 4) << decimal2hex(a & 0x0f) 
150                 << decimal2hex(r >> 4) << decimal2hex(r & 0x0f) 
151                 << decimal2hex(g >> 4) << decimal2hex(g & 0x0f) 
152                 << decimal2hex(b >> 4) << decimal2hex(b & 0x0f) ;
153         return os.str();
154
155 }
156
157
158 std::ostream& mof::operator <<(std::ostream& os, const mof::Color4f& color) 
159 {
160         mof::Color code = color.toColorCode();
161         os 
162                 << "(" 
163                 << static_cast<int>(mof::getAlpha(code)) << ", "
164                 << static_cast<int>(mof::getRed(code)) << ", "
165                 << static_cast<int>(mof::getGreen(code)) << ", "
166                 << static_cast<int>(mof::getBlue(code))
167                 << ")";
168         return os;
169 }