Go to the documentation of this file.00001 #ifndef VGColor_H
00002 #define VGColor_H
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 #include <iostream>
00018 
00024 
00025 #define ALPHA_TRANSPARENT   0
00026 #define ALPHA_OPAQUE        255
00027 
00028 
00029 
00030 
00031 
00034 class VGColor
00035 {
00036     public:     
00037                     VGColor( unsigned char gray  = 0 ) 
00038                         : mRed( gray ), mGreen( gray ), mBlue( gray ), mAlpha( ALPHA_OPAQUE ) { }
00039                     VGColor( const VGColor & in ) 
00040                         { Set( in ); }
00041                     VGColor( unsigned char r, unsigned char g, 
00042                              unsigned char b, unsigned char a = ALPHA_OPAQUE ) 
00043                         : mRed( r ), mGreen( g ), mBlue ( b ), mAlpha( a ) { }
00044 
00045         explicit    VGColor( const unsigned char col [4] ) 
00046                         { if( col ) Set( col[0], col[1], col[2], col[3] ); else Set( 0 ); }
00047 
00048         void        Set( unsigned char r, unsigned char g, 
00049                          unsigned char b, unsigned char a = ALPHA_OPAQUE )
00050                         { mRed = r; mGreen = g; mBlue = b, mAlpha = a; }
00051         void        Set( const VGColor & in )
00052                         { mRed = in.mRed; mGreen = in.mGreen; mBlue = in.mBlue, mAlpha = in.mAlpha; }
00053 
00054                 
00055         bool        operator == ( const VGColor & col ) const
00056                         { return (( col.mRed == mRed ) && ( col.mGreen == mGreen ) 
00057                           && ( col.mBlue == mBlue ) && ( col.mAlpha == mAlpha )); }
00058         
00059         bool        operator != ( const VGColor & col ) const
00060                         { return (( col.mRed != mRed ) || ( col.mGreen != mGreen ) 
00061                           || ( col.mBlue != mBlue ) || ( col.mAlpha != mAlpha )); }
00062                 
00063         VGColor &   operator += ( short v );
00064 
00065         
00066         
00067         std::ostream& print(std::ostream& out) const
00068         {
00069             out << "#";
00070             char prev = out.fill ('0');
00071             out.width(2);out << std::hex << int(mRed);
00072             out.width(2);out << std::hex << int(mGreen);
00073             out.width(2);out << std::hex << int(mBlue);
00074             out.width(1);out << "[";
00075             out.width(2);out << std::hex << int(mAlpha) << std::dec;
00076             out.width(1);out << "]";
00077             out.fill(prev);
00078 
00079             return out;
00080         }
00081             
00082         unsigned char mRed;
00083         unsigned char mGreen;
00084         unsigned char mBlue;
00085         unsigned char mAlpha; 
00086 };
00087 
00088                 
00089 inline VGColor & VGColor::operator += ( short v ) 
00090 { 
00091     int sred = mRed + v; 
00092     mRed   = (unsigned char)((sred > 255) ? 255 : sred);
00093     int sgreen = mGreen + v;
00094     mGreen = (unsigned char)((sgreen > 255) ? 255 : sgreen);
00095     int sblue = mBlue + v;
00096     mBlue  = (unsigned char)((sblue > 255) ? 255 : sblue);
00097     return *this;
00098 }
00099 
00100 inline std::ostream& operator << (std::ostream& out, const VGColor& c)
00101 {
00102     return (&c)->print(out);
00103 }
00104 
00105 
00108 #endif