Advanced Strategic Command
ColorTransform_PlayerColor.h
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 
22 #ifndef ColorTransform_PlayerColorH
23 #define ColorTransform_PlayerColorH
24 
25 
26 #include "../playercolor.h"
27 #include "colorizer.h"
28 
29 template<int pixelsize>
31 {
32  typedef typename PixelSize2Type<pixelsize>::PixelType PixelType;
33  protected:
35  {}
36  ;
37 
38  PixelType transform( PixelType col)
39  {
40  return col;
41  };
42 
43  void init( const Surface& src )
44  {}
45  ;
46 
47  public:
49  {}
50  ;
52  {}
53  ;
54 
55  void setPlayer( int player )
56  {}
57  ;
58 };
59 
60 template<>
62 {
63  int shift;
64  typedef PixelSize2Type<1>::PixelType PixelType;
65 
66  protected:
68  {}
69  ;
70 
71  PixelType transform( PixelType col)
72  {
73  if ( col >= 16 && col < 24 )
74  return col + shift;
75  else
76  return col;
77  };
78 
79  void init( const Surface& src )
80  {}
81  ;
82 
83  public:
85  {
86  setPlayer( player.getNum() );
87  };
88 
90  {}
91  ;
92 
93  void setPlayer( int player )
94  {
95  shift = player*8;
96  };
97 };
98 
99 
100 template<int pixelsize>
102 {
103  typedef typename PixelSize2Type<pixelsize>::PixelType PixelType;
104  bool lateConversion;
105  DI_Color sourceColor;
106 
107  PixelType refColor;
108  int refr, refg, refb; // this is NOT RED, GREEN, BLUE, but device dependant. Should be renamed
109  protected:
110  ColorTransform_PlayerTrueCol() : refColor(0),refr(0),refg(0),refb(0)
111  {}
112  ;
113 
114  PixelType transform( PixelType col)
115  {
116  int r = (col >> 16) & 0xff;
117  int g = (col >> 8) & 0xff;
118  int b = (col ) & 0xff;
119 
120  if ( g==0 && b==0) {
121  return ((refr * r / 256) << 16) + ((refg * r / 256) << 8) + (refb * r / 256) + (col & 0xff000000);
122  } else
123  if ( r==255 && g==b ) {
124  return ((refr + ( 255-refr) * g / 255) << 16) + ((refg + ( 255-refg) * g / 255) << 8) + (refb + ( 255-refb) * g / 255) + (col & 0xff000000);
125  } else
126  return col;
127  };
128 
129  void init( const Surface& src )
130  {
131  if ( lateConversion ) {
132  setColor( src.GetPixelFormat().MapRGB( sourceColor ));
133  lateConversion = false;
134  }
135  }
136 
137  public:
138  ColorTransform_PlayerTrueCol( PixelType color ) : lateConversion( false )
139  {
140  setColor(color);
141  };
142  ColorTransform_PlayerTrueCol( DI_Color color ) : lateConversion( false )
143  {
144  setColor(color);
145  };
146  ColorTransform_PlayerTrueCol ( NullParamType npt ) : lateConversion( false ) , refColor(0),refr(0),refg(0),refb(0)
147  {}
148  ;
149 
150  void setColor( PixelType color )
151  {
152  refColor = color;
153  refr = (color >> 16) & 0xff;
154  refg = (color >> 8) & 0xff;
155  refb = (color ) & 0xff;
156  };
157 
158  void setColor( DI_Color color )
159  {
160  lateConversion = true;
161  sourceColor = color;
162  };
163 };
164 
165 template<int pixelsize>
167 {
168  typedef typename PixelSize2Type<pixelsize>::PixelType PixelType;
169  int player;
170 
171  int rshift;
172  int gshift;
173  int bshift;
174  int ashift;
175  protected:
177  {}
178  ;
179 
180  PixelType transform( PixelType col)
181  {
182  if ( (col >> ashift) != Surface::transparent ) {
183  int r = (col >> rshift) & 0xff;
184  int g = (col >> gshift) & 0xff;
185  int b = (col >> bshift) & 0xff;
186 
187  DI_Color d = colorSwitch.switchC( player,r,g,b);
188  return (d.r << rshift) + (d.g << gshift) + (d.b << bshift) + (col & (0xff << ashift));
189  } else
190  return col;
191 
192  };
193 
194 
195  public:
196  ColorTransform_PlayerTrueColHSV( int playerNum ) : player ( playerNum )
197  {
198  };
200  {};
201 
202  void setPlayer( int playerNum )
203  {
204  player = playerNum;
205  }
206 
207  void init( const Surface& src )
208  {
209  rshift = src.GetPixelFormat().Rshift();
210  gshift = src.GetPixelFormat().Gshift();
211  bshift = src.GetPixelFormat().Bshift();
212  ashift = src.GetPixelFormat().Ashift();
213  }
214 
215 };
216 
217  template<>
219 
220  public:
221  ColorTransform_PlayerCol( const PlayerColor& player ) : ColorTransform_PlayerTrueCol<4>( player.getColor() )
222  {
223  };
224  };
225 
226 
227 
228 #endif
ColorSwitch colorSwitch
Definition: colorizer.cpp:115
A Device Independent color. Shamelessly pinched from Paragui to reduce coupling.
Definition: surface.h:30
ColorTransform_PlayerCol(const PlayerColor &player)
static const Uint32 transparent
Definition: surface.h:65
ColorTransform_PlayerCol(const PlayerColor &player)
ColorTransform_PlayerCol(const PlayerColor &player)
PixelType transform(PixelType col)
DI_Color switchC(int player, int r, int g, int b)
Definition: colorizer.h:40
ColorTransform_PlayerCol(NullParamType npt)
int getNum() const
Definition: playercolor.h:29
void init(const Surface &src)
const int playerNum
the maximum number of players that ASC can manage.
Definition: typen.h:54