00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef GRINLIZ_COLOR_INCLUDED
00027 #define GRINLIZ_COLOR_INCLUDED
00028
00029 #include "gldebug.h"
00030 #include "glutil.h"
00031 #include "glvector.h"
00032
00033 namespace grinliz {
00034
00036 struct Color3F
00037 {
00038 float r, g, b;
00039
00040 void Set( float _r, float _g, float _b ) { this->r = _r; this->g = _g; this->b = _b; }
00041 void Set255( int _r, int _g, int _b )
00042 {
00043 const float INV = 1.0f/255.0f;
00044 Set( (float)_r*INV, (float)_g*INV, (float)_b*INV );
00045 }
00046 void Scale( float v ) { r*=v; g*=v; b*=v; }
00047 float Average() { return (r+g+b)/3.0f; }
00048
00049 friend Color3F operator*( const Color3F& rh, float lh ) {
00050 Color3F result = { rh.r * lh, rh.g * lh, rh.b * lh };
00051 return result;
00052 }
00053 };
00054
00056 struct Color3U8
00057 {
00058 void Set( U8 _r, U8 _g, U8 _b ) { this->r = _r; this->g = _g; this->b = _b; }
00059 U8 r, g, b;
00060 };
00061
00063 struct Color4F
00064 {
00065 float Average() { return (r+g+b)/3.0f; }
00066 void Set( float _r, float _g, float _b, float _a ) { this->r = _r; this->g = _g; this->b = _b; this->a = _a; }
00067 float r, g, b, a;
00068 };
00069
00071 struct Color4U8
00072 {
00073 void Set( U8 _r, U8 _g, U8 _b, U8 _a ) { this->r = _r; this->g = _g; this->b = _b; this->a=_a; }
00074 U8 r, g, b, a;
00075 };
00076
00078 struct Color4U32
00079 {
00080 void Set( U32 _r, U32 _g, U32 _b, U32 _a ) { this->r = _r; this->g = _g; this->b = _b; this->a=_a; }
00081 U32 r, g, b, a;
00082 };
00083
00084
00086 inline void Convert( const Color4F& c0, Color3U8* c1 ) {
00087 c1->r = (U8)LRintf( c0.r * 255.0f );
00088 c1->g = (U8)LRintf( c0.g * 255.0f );
00089 c1->b = (U8)LRintf( c0.b * 255.0f );
00090 }
00091
00093 inline void InterpolateColor( const Color3U8& c0, const Color3U8& c1, float val, Color3U8* out ) {
00094 GLASSERT( val >= 0.0f && val <= 1.0f );
00095 int ival = LRintf( val * 255.0f );
00096 GLASSERT( ival >= 0 && ival < 256 );
00097 out->r = (U8) Interpolate( 0, (int)c0.r, 255, (int)c1.r, ival );
00098 out->g = (U8) Interpolate( 0, (int)c0.g, 255, (int)c1.g, ival );
00099 out->b = (U8) Interpolate( 0, (int)c0.b, 255, (int)c1.b, ival );
00100 }
00101
00102
00103 };
00104
00105 #endif