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
00027
00028
00029
00030
00031
00032
00033 #ifndef COLOR_INCLUDED
00034 #define COLOR_INCLUDED
00035
00036 #include "SDL.h"
00037 #include "../../grinliz/gltypes.h"
00038
00039
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
00090
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
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
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
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
00242 void Composite( const KrColorTransform& color );
00243
00244
00245 inline U8 TransformRed( U8 red ) const { return (( red*m.c.red ) >> 8 ) + b.c.red; }
00246
00247 inline U8 TransformGreen( U8 green ) const { return (( green*m.c.green ) >> 8 ) + b.c.green; }
00248
00249 inline U8 TransformBlue( U8 blue ) const { return (( blue*m.c.blue ) >> 8 ) + b.c.blue; }
00250
00251
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;
00261 bool hasAlpha;
00262 bool hasColor;
00263
00264 public:
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 KrRGBA m,
00277 b;
00278 };
00279
00280
00281
00282 #endif