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 "glutil.h"
00030 #include "gldebug.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 Scale( float v ) { r*=v; g*=v; b*=v; }
00042 float Average() { return (r+g+b)/3.0f; }
00043
00044 friend Color3F operator*( const Color3F& rh, float lh ) {
00045 Color3F result = { rh.r * lh, rh.g * lh, rh.b * lh };
00046 return result;
00047 }
00048 };
00049
00051 struct Color3U8
00052 {
00053 void Set( U8 r, U8 g, U8 b ) { this->r = r; this->g = g; this->b = b; }
00054 U8 r, g, b;
00055 };
00056
00058 struct Color4F
00059 {
00060 void Set( float r, float g, float b, float a ) { this->r = r; this->g = g; this->b = b; this->a = a; }
00061 float r, g, b, a;
00062 };
00063
00065 struct Color4U8
00066 {
00067 void Set( U8 r, U8 g, U8 b, U8 a ) { this->r = r; this->g = g; this->b = b; this->a=a; }
00068 U8 r, g, b, a;
00069 };
00070
00071
00073 inline void Convert( const Color4F& c0, Color3U8* c1 ) {
00074 c1->r = (U8)LRintf( c0.r * 255.0f );
00075 c1->g = (U8)LRintf( c0.g * 255.0f );
00076 c1->b = (U8)LRintf( c0.b * 255.0f );
00077 }
00078
00080 inline void InterpolateColor( const Color3U8& c0, const Color3U8& c1, float val, Color3U8* out ) {
00081 GLASSERT( val >= 0.0f && val <= 1.0f );
00082 int ival = LRintf( val * 255.0f );
00083 GLASSERT( ival >= 0 && ival < 256 );
00084 out->r = (U8) Interpolate( 0, (int)c0.r, 255, (int)c1.r, ival );
00085 out->g = (U8) Interpolate( 0, (int)c0.g, 255, (int)c1.g, ival );
00086 out->b = (U8) Interpolate( 0, (int)c0.b, 255, (int)c1.b, ival );
00087 }
00088
00089
00090 };
00091
00092 #endif