gfx_rgba.hh

Go to the documentation of this file.
00001 /*
00008  * LEGAL:   COPYRIGHT (C) 2004 JIM E. BROOKS
00009  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
00010  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
00011  *****************************************************************************/
00012 
00013 #ifndef GFX_RGBA_HH
00014 #define GFX_RGBA_HH 1
00015 
00016 namespace gfx {
00017 
00018 // Index to bytes of RGBA object.
00019 // In OpenGL, RGBA byte-order is independent of CPU endianness.
00020 // glColor4ubv() always expects red at GLubyte[0].
00021 enum { RR, GG, BB, AA };
00022 
00023 #define RGBA_LEN    4
00024 #define RGBA8_LEN   4
00025 #define BGRA_LEN    4
00026 #define BGRA8_LEN   4
00027 
00031 class RGBA
00032 {
00033 public:
00034 
00035 // Cast uint8[4] as uint32.
00036 #define RGBA_CAST(RGBA)       *reinterpret_cast<uint32*>( RGBA )
00037 #define CONST_RGBA_CAST(RGBA) *reinterpret_cast<const uint32*>( RGBA )
00038 
00039     RGBA( void )
00040     {
00041         RGBA_CAST( mRgba ) = 0;  // idiom: RGBA_CAST() is a cast, not a call
00042     }
00043 
00044     explicit RGBA( const uint8 rgba[4] )
00045     {
00046         // rgba[4] input is implicity clamped to a byte.
00047         RGBA_CAST( mRgba ) = CONST_RGBA_CAST( rgba );
00048     }
00049 
00050     explicit RGBA( const fp rgba[4] )
00051     {
00052         Set( rgba[RR], rgba[GG], rgba[BB], rgba[AA] );
00053     }
00054 
00055     RGBA( int r, int g, int b, int a )
00056     {
00057         Set( r, g, b, a );  // Set(int,int,int,int)
00058     }
00059 
00060     RGBA( uint r, uint g, uint b, uint a )
00061     {
00062         Set( r, g, b, a );  // Set(uint,uint,uint,uint)
00063     }
00064 
00065     RGBA( fp r, fp g, fp b, fp a )
00066     {
00067         Set( r, g, b, a );
00068     }
00069 
00070     RGBA( const RGBA& src )
00071     {
00072         RGBA_CAST( mRgba ) = CONST_RGBA_CAST( src.mRgba );
00073     }
00074 
00075     void Set( const uint8 rgba[4] )
00076     {
00077         RGBA_CAST( mRgba ) = CONST_RGBA_CAST( rgba );
00078     }
00079 
00080     // Set(int)/Set(uint) differ.
00081     // ClampFF(int) clamps negatives at 0 but ClampFF(uint) doesn't.
00082     void Set( int r, int g, int b, int a )
00083     {
00084         mRgba[RR] = ClampFF( r );
00085         mRgba[GG] = ClampFF( g );
00086         mRgba[BB] = ClampFF( b );
00087         mRgba[AA] = ClampFF( a );
00088     }
00089 
00090     void Set( uint r, uint g, uint b, uint a )
00091     {
00092         mRgba[RR] = ClampFF( r );
00093         mRgba[GG] = ClampFF( g );
00094         mRgba[BB] = ClampFF( b );
00095         mRgba[AA] = ClampFF( a );
00096     }
00097 
00098     void Set( fp r, fp g, fp b, fp a )
00099     {
00100         mRgba[RR] = ClampFF( uint(r * 256.0) );
00101         mRgba[GG] = ClampFF( uint(g * 256.0) );
00102         mRgba[BB] = ClampFF( uint(b * 256.0) );
00103         mRgba[AA] = ClampFF( uint(a * 256.0) );
00104     }
00105 
00106     void Set( int same )
00107     {
00108         mRgba[RR] = mRgba[GG] = mRgba[BB] = mRgba[AA] = same;
00109     }
00110 
00111     const uint8* Get( void ) const
00112     {
00113         return mRgba;
00114     }
00115 
00116     // RGBA object --> fp[4] (rare).
00117     template<typename FP>  // should be a float type
00118     void Get4f( FP out[4] /*OUT*/ ) const
00119     {
00120         out[RR] = mRgba[RR] / 256.0;
00121         out[GG] = mRgba[GG] / 256.0;
00122         out[BB] = mRgba[BB] / 256.0;
00123         out[AA] = mRgba[AA] / 256.0;
00124     }
00125 
00126     void Get4f( TinyArray<4,fp>& out /*OUT*/ ) const
00127     {
00128         out[RR] = mRgba[RR] / 256.0;
00129         out[GG] = mRgba[GG] / 256.0;
00130         out[BB] = mRgba[BB] / 256.0;
00131         out[AA] = mRgba[AA] / 256.0;
00132     }
00133 
00134     // RGBA object --> uint[4]
00135     void Get4ub( uint8 out[4]/*OUT*/ ) const
00136     {
00137         RGBA_CAST( out ) = CONST_RGBA_CAST( mRgba );
00138     }
00139 
00140     RGBA& operator=( const RGBA& src )
00141     {
00142         RGBA_CAST( mRgba ) = CONST_RGBA_CAST( src.mRgba );
00143         return *this;
00144     }
00145 
00146     uint8& operator[]( uint i )
00147     {
00148         ASSERT( i < 4 );
00149         return mRgba[i];
00150     }
00151 
00152     const uint8& operator[]( uint i ) const // compiled when object is const
00153     {
00154         ASSERT( i < 4 );
00155         return mRgba[i];
00156     }
00157 
00158     bool IfZero( void ) const
00159     {
00160         return CONST_RGBA_CAST( mRgba ) == 0;
00161     }
00162 
00163     friend inline bool operator<( const RGBA src1, const RGBA src2 )
00164     {
00165         return CONST_RGBA_CAST( src1.mRgba ) < CONST_RGBA_CAST( src2.mRgba );
00166     }
00167 
00168     friend inline bool operator==( const RGBA src1, const RGBA src2 )
00169     {
00170         return CONST_RGBA_CAST( src1.mRgba ) == CONST_RGBA_CAST( src2.mRgba );
00171     }
00172 
00173     friend inline bool operator!=( const RGBA src1, const RGBA src2 )
00174     {
00175         return CONST_RGBA_CAST( src1.mRgba ) != CONST_RGBA_CAST( src2.mRgba );
00176     }
00177 
00178 private:
00179     uint8   mRgba[4];
00180 };
00181 
00182 RGBA
00183 Interpolate( const RGBA rgba0, const RGBA rgba1, const fp factor = 0.5 );
00184 
00188 INLINE ostream&
00189 operator<<( ostream& strm, const RGBA rgba )
00190 {
00191     ios::fmtflags savedFlags = strm.flags();
00192     strm << '('
00193          << OMANIP_FIELD(3) << uint(rgba[RR]) << ','  // print numbers, not chars
00194          << OMANIP_FIELD(3) << uint(rgba[GG]) << ','  // (uint8 is uchar)
00195          << OMANIP_FIELD(3) << uint(rgba[BB]) << ','
00196          << OMANIP_FIELD(3) << uint(rgba[AA]) << ')';
00197     strm.flags(savedFlags);
00198     return strm;
00199 }
00200 
00202 
00203 #define RGB_BROWN       0.7,    0.35,   0.2
00204 #define RGB_GRAY        0.55,   0.55,   0.55
00205 #define RGB_GREEN       0.05,   0.9,    0.05
00206 #define RGB_DARK_GREEN  0.05,   0.5,    0.05
00207 #define RGB_DARK_GREEN2 0.05,   0.45,   0.05
00208 #define RGB_PURPLE      0.6,    0.1,    0.6
00209 #define RGB_RED         0.8,    0.1,    0.1
00210 #define RGB_BLUE        0.1,    0.1,    0.8
00211 #define RGB_SKY_BLUE    0.468,  0.684,  0.922   // natural
00212 #define RGB_SKY_BLUE2   0.2,    0.6,    0.99    // artificial
00213 #define RGB_SHADOW      0.35,   0.35,   0.35
00214 #define RGB_WHITE       0.95,   0.95,   0.95
00215 #define RGB_YELLOW      1.0,    1.0,    0.0
00216 
00217 } // namespace gfx
00218 
00219 #endif // GFX_RGBA_HH
Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:11 2007