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
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #ifndef COLOR_INCLUDED
00071 #define COLOR_INCLUDED
00072
00073 #include "SDL.h"
00074 #include "../util/gltypes.h"
00075
00076
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
00127
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
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
00218
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
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
00279 void Composite( const KrColorTransform& color );
00280
00281
00282 inline U8 TransformRed( U8 red ) const { return (( red*m.c.red ) >> 8 ) + b.c.red; }
00283
00284 inline U8 TransformGreen( U8 green ) const { return (( green*m.c.green ) >> 8 ) + b.c.green; }
00285
00286 inline U8 TransformBlue( U8 blue ) const { return (( blue*m.c.blue ) >> 8 ) + b.c.blue; }
00287
00288
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;
00298 bool hasAlpha;
00299 bool hasColor;
00300
00301 public:
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 KrRGBA m,
00314 b;
00315 };
00316
00317
00318
00319 #endif