Main Page   Class Hierarchy   Compound List   File List   Compound Members  

color.h

00001 /*--License:
00002         Kyra Sprite Engine
00003         Copyright Lee Thomason (Grinning Lizard Software) 2001-2002
00004         www.grinninglizard.com/kyra
00005         www.sourceforge.net/projects/kyra
00006 
00007         Kyra is provided under 2 licenses:
00008 
00009         - The GPL, with no additional restrictions.
00010         - The LGPL, provided you display the Kyra splash screen, described below.
00011 
00012 
00013 --- GPL License --
00014         This program is free software; you can redistribute it and/or
00015         modify it under the terms of the GNU General Public License
00016         as published by the Free Software Foundation; either version 2
00017         of the License, or (at your option) any later version.
00018 
00019         This program is distributed in the hope that it will be useful,
00020         but WITHOUT ANY WARRANTY; without even the implied warranty of
00021         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022         GNU General Public License for more details.
00023 
00024         You should have received a copy of the GNU General Public License
00025         along with this program; if not, write to the Free Software
00026         Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028         The full text of the license can be found in license.txt
00029 
00030 
00031 --- LGPL License --
00032   **Provided you kindly display the Kyra splash screen (details below), 
00033         you     may use the LGPL license:**
00034 
00035     This library is free software; you can redistribute it and/or
00036     modify it under the terms of the GNU Lesser General Public
00037     License as published by the Free Software Foundation; either
00038     version 2.1 of the License, or (at your option) any later version.
00039 
00040     This library is distributed in the hope that it will be useful,
00041     but WITHOUT ANY WARRANTY; without even the implied warranty of
00042     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00043     Lesser General Public License for more details.
00044 
00045     You should have received a copy of the GNU Lesser General Public
00046     License along with this library; if not, write to the Free Software
00047     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00048 
00049         The full text of the license can be found in lgpl.txt
00050 
00051 
00052 --- Kyra Splash Screen.
00053 
00054         It would be appreciate if you display the Kyra splash screen when using
00055         either license, however it is only required for the LGPL. All the
00056         resources for the splash are compiled into the library, and it can be
00057         accessed through the following API:
00058 
00059                 KrEngine::StartSplash
00060                 KrEngine::UpdateSplash
00061                 KrEngine::EndSplash
00062 
00063         Full documentation is provided with the KrEngine class. The splash screen
00064         should be displayed for 2 seconds.
00065 
00066         Thank you.
00067 */
00068 
00069 
00070 #ifndef COLOR_INCLUDED
00071 #define COLOR_INCLUDED
00072 
00073 #include "SDL.h"
00074 #include "../util/gltypes.h"
00075 
00076 // This is a guess, on what the byte ordering for 32 bit should be:
00077 #if !defined( SDL_BYTEORDER )
00078         #error Need byte order!
00079 #endif
00080 
00081 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
00082         #define KYRA_FLIP_COLORS
00083 #endif
00084 
00115 union KrRGBA
00116 {
00117         enum  
00118         {
00119                 KR_TRANSPARENT = 0,
00120                 KR_OPAQUE      = 255,
00121         };
00122 
00123         
00124         enum {  
00125                         
00126 // It will work regardless of the endian, or whether the colors
00127 // are aligned. But it is much faster if the colors are aligned.
00128 #ifndef KYRA_FLIP_COLORS
00129                 RED,
00130                 GREEN,
00131                 BLUE,
00132 #else
00133                 BLUE,
00134                 GREEN,
00135                 RED,
00136 #endif
00137                 ALPHA,
00138 
00139                 START = 0,
00140                 END = 3                 // r, g, b...alpha not in this.
00141         };
00142 
00143         struct 
00144         {
00145 #ifndef KYRA_FLIP_COLORS
00146                 U8 red;                 
00147                 U8 green;               
00148                 U8 blue;                
00149 #else
00150                 U8 blue;
00151                 U8 green;
00152                 U8 red;
00153 #endif
00154                 U8 alpha;               
00155         } c;
00156 
00157         U8              array[4];
00158         U32             all;
00159 
00161         float Redf()    const { return float( c.red )   / 255.0f; }
00162         float Greenf()  const { return float( c.green ) / 255.0f; }
00163         float Bluef()   const { return float( c.blue )  / 255.0f; }
00164         float Alphaf()  const { return float( c.alpha ) / 255.0f; }
00165 
00167         void Set( U8 _red, U8 _green, U8 _blue, U8 _alpha = 255 )       
00168                         {       c.red = _red; 
00169                                 c.green = _green; 
00170                                 c.blue = _blue; 
00171                                 c.alpha = _alpha; 
00172                         }
00173 
00174         bool operator==( KrRGBA rhs )   { return ( all == rhs.all ); }
00175         bool operator!=( KrRGBA rhs )   { return ( all != rhs.all ); }
00176 
00180         void FromString( const char* str );
00181 };
00182 
00183 
00184 
00206 class KrColorTransform
00207 {
00208   public:
00209         KrColorTransform()      :       identity( true ),
00210                                                         hasAlpha( false ),
00211                                                         hasColor( false )
00212                                                 {
00213                                                         m.Set( 255, 255, 255, 0 );
00214                                                         b.Set( 0, 0, 0, 255 );
00215                                                 }
00216         // --------- Friendly API ---------- //
00218         void SetIdentity()                                      { m.Set( 255, 255, 255, 0 );
00219                                                                                   b.Set( 0, 0, 0, 255 );
00220                                                                                 }
00221         void SetAlpha( U8 a )                           { b.c.alpha = a; CalcState(); }         
00222 
00223         void TintRed( U8 tint )                         { SetRed( 255 - tint, tint ); }         
00224         void TintGreen( U8 tint )                       { SetGreen( 255 - tint, tint ); }       
00225         void TintBlue( U8 tint )                        { SetBlue( 255 - tint, tint ); }        
00226         void TintAlpha( U8 tint )                       { SetAlpha( 255 - tint ); }                     
00227 
00229         void Brighten( U8 val )                 { m.c.red   = 255-val; b.c.red   = val; 
00230                                                                           m.c.green = 255-val; b.c.green = val; 
00231                                                                           m.c.blue  = 255-val; b.c.blue  = val; 
00232                                                                           CalcState();
00233                                                                         }
00234 
00236         void Darken( U8 val  )                  { m.c.red   = 255-val; b.c.red = 0;
00237                                                                           m.c.green = 255-val; b.c.green = 0;
00238                                                                           m.c.blue  = 255-val; b.c.blue = 0;
00239                                                                           CalcState();
00240                                                                         }
00241 
00242         // --------- Advanced API ------- //
00263         void Set( U8 mRed, U8 bRed, U8 mGreen, U8 bGreen, U8 mBlue, U8 bBlue, U8 alpha );
00264 
00266         void SetRed( U8 _m, U8 _b )             {       m.c.red = _m; b.c.red = _b; CalcState(); }
00268         void SetGreen( U8 _m, U8 _b )   {       m.c.green = _m; b.c.green = _b; CalcState(); }
00270         void SetBlue( U8 _m, U8 _b )    {       m.c.blue = _m; b.c.blue = _b; CalcState(); }
00271 
00272         bool IsIdentity() const { return identity; }
00273         bool HasAlpha() const   { return hasAlpha; }
00274         bool HasColor() const   { return hasColor; }
00275 
00276         U8   Alpha() const              { return b.c.alpha; }
00277 
00278         // Apply 'color' to this transformation. Composite assumes 'color' PRE transforms this.
00279         void Composite( const KrColorTransform& color );
00280 
00281         // Note this transforms color but not alpha
00282         inline U8 TransformRed( U8 red ) const          { return (( red*m.c.red ) >> 8 ) + b.c.red; }
00283         // Note this transforms color but not alpha
00284         inline U8 TransformGreen( U8 green ) const      { return (( green*m.c.green ) >> 8 ) + b.c.green; }
00285         // Note this transforms color but not alpha
00286         inline U8 TransformBlue( U8 blue ) const        { return (( blue*m.c.blue ) >> 8 ) + b.c.blue; }
00287 
00288         // Transform the color (all channels) no alpha is used.
00289         void ApplyTransform( KrRGBA* ) const;
00290 
00291         bool operator==( const KrColorTransform& rhs ) const    { return  ( m.all == rhs.m.all && b.all == rhs.b.all ); }
00292         bool operator!=( const KrColorTransform& rhs ) const    { return !( m.all == rhs.m.all && b.all == rhs.b.all ); }
00293 
00294   private:
00295         void CalcState();
00296         
00297         bool identity;          // no color transform?
00298         bool hasAlpha;          // alpha transform?
00299         bool hasColor;          // rgb transform?
00300 
00301   public:
00302         // These are public so the blit macros can get to them without
00303         // showing up when the code is profiled. DO NOT change these
00304         // directly -- use the API above.
00305         
00306         // For consistency with loops and types, we use RGBAs here as
00307         // well. Note this results in an extra alpha: alpha is an 
00308         // absolute, not slope/intercept like the other components.
00309         // (At least in Kyra...)
00310         // So: m.c.alpha must always be 0.
00311         //     b.c.alpha is the alpha channel used.
00312 
00313         KrRGBA  m,      // multiplication of color (255 == 1.0)
00314                         b;      // addtion of color (0 == no color change)
00315 };
00316 
00317 
00318 
00319 #endif

Generated on Mon Sep 15 12:01:10 2003 for Kyra by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001