1 #include "mof/Color.hpp"
3 #include "mof/ConsoleIO.hpp"
4 #include "mof/tstring.hpp"
7 mof::Color4f::Color4f() :
8 alpha(0) , red(1) , green(1) , blue(1)
12 mof::Color4f::Color4f(float alpha_ , float red_ , float green_ , float blue_) :
13 alpha(alpha_) , red(red_) , green(green_) , blue(blue_)
17 mof::Color4f::Color4f( float red_ , float green_ , float blue_) :
18 alpha(1) , red(red_) , green(green_) , blue(blue_)
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 )
30 mof::Color mof::Color4f::toColorCode() const{
32 static_cast<ColorChannel>(alpha * 255) ,
33 static_cast<ColorChannel>(red * 255) ,
34 static_cast<ColorChannel>(green * 255) ,
35 static_cast<ColorChannel>(blue * 255)
39 mof::Color4f mof::Color4f::operator +(const mof::Color4f& rhs) const{
47 mof::Color4f mof::Color4f::operator -(const mof::Color4f& rhs) const{
55 mof::Color4f mof::Color4f::operator *(const Color4f& color ) const
57 return mof::Color4f(color.alpha * alpha , color.red * red , color.green * green , color.blue * blue );
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);
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);
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;
81 char decimal2hex(unsigned char n){
82 if( n >= 16)return '-';//error
83 return "0123456789abcdef"[n];
86 mof::Color mof::createColor(unsigned char r , unsigned char g , unsigned char b){
87 return D3DCOLOR_XRGB(r , g , b);
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);
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
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);
107 unsigned char mof::getAlpha(const mof::Color& color){
108 return (unsigned char)((color >> 24) & 0xff);
112 unsigned char mof::getRed(const mof::Color& color){
113 return (unsigned char)((color >> 16) & 0xff);
116 unsigned char mof::getGreen(const mof::Color& color){
117 return (unsigned char)((color >> 8) & 0xff);
121 unsigned char mof::getBlue(const mof::Color& color){
122 return (unsigned char)(color & 0xff);
126 mof::Color mof::blendColor( mof::Color a , mof::Color b , float rate ){
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
134 mof::Color mof::inverseColor(mof::Color src){
136 255 - getAlpha(src) , //alpha
137 255 - getRed(src) , //red
138 255 - getGreen(src) , //green
139 255 - getBlue(src) ); //blue
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) ;
158 std::ostream& mof::operator <<(std::ostream& os, const mof::Color4f& color)
160 mof::Color code = color.toColorCode();
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))