glcolor.h

00001 /*
00002 Copyright (c) 2000-2003 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 "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

Generated on Thu Jul 20 20:45:31 2006 for Kyra by  doxygen 1.4.7