glcolor.h

00001 /*
00002 Copyright (c) 2000-2007 Lee Thomason (www.grinninglizard.com)
00003 Grinning Lizard Utilities.
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any 
00010 purpose, including commercial applications, and to alter it and 
00011 redistribute it freely, subject to the following restrictions:
00012 
00013 1. The origin of this software must not be misrepresented; you must 
00014 not claim that you wrote the original software. If you use this 
00015 software in a product, an acknowledgment in the product documentation 
00016 would be appreciated but is not required.
00017 
00018 2. Altered source versions must be plainly marked as such, and 
00019 must not be misrepresented as being the original software.
00020 
00021 3. This notice may not be removed or altered from any source 
00022 distribution.
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

Generated on Fri Mar 23 19:36:21 2007 for Lilith3D by  doxygen 1.5.1-p1