- Main Page
- Classes
- Files
- Directories
- Related Pages

# color.h

    00001 /*--License:
    00002         Kyra Sprite Engine
    00003         Copyright Lee Thomason (Grinning Lizard Software) 2001-2005
    00004         www.grinninglizard.com/kyra
    00005         www.sourceforge.net/projects/kyra
    00006
    00007         Kyra is provided under the LGPL.
    00008
    00009         I kindly request you display a splash screen (provided in the HTML documentation)
    00010         to promote Kyra and acknowledge the software and everyone who has contributed to it,
    00011         but it is not required by the license.
    00012
    00013 --- LGPL License --
    00014
    00015     This library is free software; you can redistribute it and/or
    00016     modify it under the terms of the GNU Lesser General Public
    00017     License as published by the Free Software Foundation; either
    00018     version 2.1 of the License, or (at your option) any later version.
    00019
    00020     This library is distributed in the hope that it will be useful,
    00021     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00022     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00023     Lesser General Public License for more details.
    00024
    00025     You should have received a copy of the GNU Lesser General Public
    00026     License along with this library; if not, write to the Free Software
    00027     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00028
    00029         The full text of the license can be found in lgpl.txt
    00030 */
    00031
    00032
    00033 #ifndef COLOR_INCLUDED
    00034 #define COLOR_INCLUDED
    00035
    00036 #include "SDL.h"
    00037 #include "../../grinliz/gltypes.h"
    00038
    00039 // This is a guess, on what the byte ordering for 32 bit should be:
    00040 #if !defined( SDL_BYTEORDER )
    00041         #error Need byte order!
    00042 #endif
    00043
    00044 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
    00045         #define KYRA_FLIP_COLORS
    00046 #endif
    00047
    00078 union KrRGBA
    00079 {
    00080         enum
    00081         {
    00082                 KR_TRANSPARENT = 0,
    00083                 KR_OPAQUE      = 255,
    00084         };
    00085
    00086
    00087         enum {
    00088
    00089 // It will work regardless of the endian, or whether the colors
    00090 // are aligned. But it is much faster if the colors are aligned.
    00091 #ifndef KYRA_FLIP_COLORS
    00092                 RED,
    00093                 GREEN,
    00094                 BLUE,
    00095 #else
    00096                 BLUE,
    00097                 GREEN,
    00098                 RED,
    00099 #endif
    00100                 ALPHA,
    00101
    00102                 START = 0,
    00103                 END = 3                 // r, g, b...alpha not in this.
    00104         };
    00105
    00106         struct
    00107         {
    00108 #ifndef KYRA_FLIP_COLORS
    00109                 U8 red;
    00110                 U8 green;
    00111                 U8 blue;
    00112 #else
    00113                 U8 blue;
    00114                 U8 green;
    00115                 U8 red;
    00116 #endif
    00117                 U8 alpha;
    00118         } c;
    00119
    00120         U8              array[4];
    00121         U32             all;
    00122
    00124         float Redf()    const { return float( c.red )   / 255.0f; }
    00125         float Greenf()  const { return float( c.green ) / 255.0f; }
    00126         float Bluef()   const { return float( c.blue )  / 255.0f; }
    00127         float Alphaf()  const { return float( c.alpha ) / 255.0f; }
    00128
    00130         void Set( U8 _red, U8 _green, U8 _blue, U8 _alpha = 255 )
    00131                         {       c.red = _red;
    00132                                 c.green = _green;
    00133                                 c.blue = _blue;
    00134                                 c.alpha = _alpha;
    00135                         }
    00136
    00137         bool operator==( KrRGBA rhs )   { return ( all == rhs.all ); }
    00138         bool operator!=( KrRGBA rhs )   { return ( all != rhs.all ); }
    00139
    00143         void FromString( const char* str );
    00144 };
    00145
    00146
    00147
    00169 class KrColorTransform
    00170 {
    00171   public:
    00172         KrColorTransform()      :       identity( true ),
    00173                                                         hasAlpha( false ),
    00174                                                         hasColor( false )
    00175                                                 {
    00176                                                         m.Set( 255, 255, 255, 0 );
    00177                                                         b.Set( 0, 0, 0, 255 );
    00178                                                 }
    00179         // --------- Friendly API ---------- //
    00181         void SetIdentity()                                      { m.Set( 255, 255, 255, 0 );
    00182                                                                                   b.Set( 0, 0, 0, 255 );
    00183                                                                                 }
    00184         void SetAlpha( U8 a )                           { b.c.alpha = a; CalcState(); }
    00185
    00186         void TintRed( U8 tint )                         { SetRed( 255 - tint, tint ); }
    00187         void TintGreen( U8 tint )                       { SetGreen( 255 - tint, tint ); }
    00188         void TintBlue( U8 tint )                        { SetBlue( 255 - tint, tint ); }
    00189         void TintAlpha( U8 tint )                       { SetAlpha( 255 - tint ); }
    00190
    00192         void Brighten( U8 val )                 { m.c.red   = 255-val; b.c.red   = val;
    00193                                                                           m.c.green = 255-val; b.c.green = val;
    00194                                                                           m.c.blue  = 255-val; b.c.blue  = val;
    00195                                                                           CalcState();
    00196                                                                         }
    00197
    00199         void Darken( U8 val  )                  { m.c.red   = 255-val; b.c.red = 0;
    00200                                                                           m.c.green = 255-val; b.c.green = 0;
    00201                                                                           m.c.blue  = 255-val; b.c.blue = 0;
    00202                                                                           CalcState();
    00203                                                                         }
    00204
    00205         // --------- Advanced API ------- //
    00226         void Set( U8 mRed, U8 bRed, U8 mGreen, U8 bGreen, U8 mBlue, U8 bBlue, U8 alpha );
    00227
    00229         void SetRed( U8 _m, U8 _b )             {       m.c.red = _m; b.c.red = _b; CalcState(); }
    00231         void SetGreen( U8 _m, U8 _b )   {       m.c.green = _m; b.c.green = _b; CalcState(); }
    00233         void SetBlue( U8 _m, U8 _b )    {       m.c.blue = _m; b.c.blue = _b; CalcState(); }
    00234
    00235         bool IsIdentity() const { return identity; }
    00236         bool HasAlpha() const   { return hasAlpha; }
    00237         bool HasColor() const   { return hasColor; }
    00238
    00239         U8   Alpha() const              { return b.c.alpha; }
    00240
    00241         // Apply 'color' to this transformation. Composite assumes 'color' PRE transforms this.
    00242         void Composite( const KrColorTransform& color );
    00243
    00244         // Note this transforms color but not alpha
    00245         inline U8 TransformRed( U8 red ) const          { return (( red*m.c.red ) >> 8 ) + b.c.red; }
    00246         // Note this transforms color but not alpha
    00247         inline U8 TransformGreen( U8 green ) const      { return (( green*m.c.green ) >> 8 ) + b.c.green; }
    00248         // Note this transforms color but not alpha
    00249         inline U8 TransformBlue( U8 blue ) const        { return (( blue*m.c.blue ) >> 8 ) + b.c.blue; }
    00250
    00251         // Transform the color (all channels) no alpha is used.
    00252         void ApplyTransform( KrRGBA* ) const;
    00253
    00254         bool operator==( const KrColorTransform& rhs ) const    { return  ( m.all == rhs.m.all && b.all == rhs.b.all ); }
    00255         bool operator!=( const KrColorTransform& rhs ) const    { return !( m.all == rhs.m.all && b.all == rhs.b.all ); }
    00256
    00257   private:
    00258         void CalcState();
    00259
    00260         bool identity;          // no color transform?
    00261         bool hasAlpha;          // alpha transform?
    00262         bool hasColor;          // rgb transform?
    00263
    00264   public:
    00265         // These are public so the blit macros can get to them without
    00266         // showing up when the code is profiled. DO NOT change these
    00267         // directly -- use the API above.
    00268
    00269         // For consistency with loops and types, we use RGBAs here as
    00270         // well. Note this results in an extra alpha: alpha is an
    00271         // absolute, not slope/intercept like the other components.
    00272         // (At least in Kyra...)
    00273         // So: m.c.alpha must always be 0.
    00274         //     b.c.alpha is the alpha channel used.
    00275
    00276         KrRGBA  m,      // multiplication of color (255 == 1.0)
    00277                         b;      // addtion of color (0 == no color change)
    00278 };
    00279
    00280
    00281
    00282 #endif