ManaPlus
Data Structures | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Types | Protected Member Functions | Protected Attributes | Static Protected Attributes
Palette Class Reference

#include <palette.h>

Inheritance diagram for Palette:
Theme UserPalette

Data Structures

struct  ColorElem
 

Public Member Functions

const ColorgetCharColor (const signed char c, bool &valid) const
 
char getColorChar (const int type) const
 

Static Public Member Functions

static void advanceGradients ()
 

Static Public Attributes

static const Color BLACK = Color(0, 0, 0, 255)
 

Protected Types

typedef std::set< Palette * > Palettes
 
typedef std::vector< ColorElemColors
 
typedef std::map< unsigned char, int > CharColors
 

Protected Member Functions

 Palette (const int size)
 
virtual ~Palette ()
 
void advanceGradient ()
 

Protected Attributes

int mRainbowTime
 
Colors mColors
 
CharColors mCharColors
 
std::vector< ColorElem * > mGradVector
 

Static Protected Attributes

static const Color RAINBOW_COLORS [7]
 
static const int RAINBOW_COLOR_COUNT = 7
 
static Palettes mInstances
 

Detailed Description

Class controlling the game's color palette.

Definition at line 43 of file palette.h.

Member Typedef Documentation

◆ CharColors

typedef std::map<unsigned char, int> Palette::CharColors
protected

Definition at line 152 of file palette.h.

◆ Colors

typedef std::vector<ColorElem> Palette::Colors
protected

Definition at line 151 of file palette.h.

◆ Palettes

typedef std::set<Palette*> Palette::Palettes
protected

Definition at line 86 of file palette.h.

Constructor & Destructor Documentation

◆ Palette()

Palette::Palette ( const int  size)
explicitprotected

Constructor

Definition at line 51 of file palette.cpp.

References mInstances.

51  :
54  mCharColors(),
55  mGradVector()
56 {
57  mInstances.insert(this);
58 }
static Palettes mInstances
Definition: palette.h:87
volatile int tick_time
Definition: timer.cpp:52
std::vector< ColorElem * > mGradVector
Definition: palette.h:155
Colors mColors
Definition: palette.h:153
std::vector< ColorElem > Colors
Definition: palette.h:151
CharColors mCharColors
Definition: palette.h:154
int mRainbowTime
Definition: palette.h:84

◆ ~Palette()

Palette::~Palette ( )
protectedvirtual

Destructor

Definition at line 60 of file palette.cpp.

References mInstances.

61 {
62  mInstances.erase(this);
63 }
static Palettes mInstances
Definition: palette.h:87

Member Function Documentation

◆ advanceGradient()

void Palette::advanceGradient ( )
protected

Definition at line 84 of file palette.cpp.

References A_NONNULLPOINTER, Color::b, CAST_S32, Color::g, get_elapsed_time(), mGradVector, mRainbowTime, GradientType::PULSE, Color::r, GradientType::RAINBOW, RAINBOW_COLOR_COUNT, RAINBOW_COLORS, GradientType::SPECTRUM, tick_time, and ServerCommandType::time.

85 {
86  const int time = get_elapsed_time(mRainbowTime);
87  if (time > 5)
88  {
89  // For slower systems, advance can be greater than one (advance > 1
90  // skips advance-1 steps). Should make gradient look the same
91  // independent of the framerate.
92  const int advance = time / 5;
93 
94  for (size_t i = 0, sz = mGradVector.size(); i < sz; i++)
95  {
96  ColorElem *const elem A_NONNULLPOINTER = mGradVector[i];
97  if (elem == nullptr)
98  continue;
99 
100  int delay = elem->delay;
101  const GradientTypeT &grad = elem->grad;
102 
103  if (grad == GradientType::PULSE)
104  delay = delay / 20;
105 
106  const int numOfColors = (elem->grad == GradientType::SPECTRUM ? 6 :
107  grad == GradientType::PULSE ? 127 :
109 
110  elem->gradientIndex = (elem->gradientIndex + advance)
111  % (delay * numOfColors);
112 
113  const int gradIndex = elem->gradientIndex;
114  const int pos = delay != 0 ? (gradIndex % delay) : gradIndex;
115  int colIndex;
116  if (delay != 0)
117  colIndex = gradIndex / delay;
118  else
119  colIndex = gradIndex;
120 
121  Color &color = elem->color;
122  int colVal;
123 
124  if (grad == GradientType::PULSE)
125  {
126  colVal = CAST_S32(255.0 *
127  sin(M_PI * colIndex / numOfColors));
128 
129  const Color &col = elem->testColor;
130 
131  color.r = ((colVal * col.r) / 255) % (col.r + 1);
132  color.g = ((colVal * col.g) / 255) % (col.g + 1);
133  color.b = ((colVal * col.b) / 255) % (col.b + 1);
134  }
135  else if (grad == GradientType::SPECTRUM)
136  {
137  if ((colIndex % 2) != 0)
138  { // falling curve
139  if (delay != 0)
140  {
141  colVal = CAST_S32(255.0 *
142  (cos(M_PI * pos / delay) + 1) / 2);
143  }
144  else
145  {
146  colVal = CAST_S32(255.0 *
147  (cos(M_PI * pos) + 1) / 2);
148  }
149  }
150  else
151  { // ascending curve
152  if (delay != 0)
153  {
154  colVal = CAST_S32(255.0 * (cos(M_PI *
155  (delay - pos) / delay) + 1) / 2);
156  }
157  else
158  {
159  colVal = CAST_S32(255.0 * (cos(M_PI *
160  (delay - pos)) + 1) / 2);
161  }
162  }
163 
164  color.r = (colIndex == 0 || colIndex == 5) ? 255 :
165  (colIndex == 1 || colIndex == 4) ? colVal : 0;
166  color.g = (colIndex == 1 || colIndex == 2) ? 255 :
167  (colIndex == 0 || colIndex == 3) ? colVal : 0;
168  color.b = (colIndex == 3 || colIndex == 4) ? 255 :
169  (colIndex == 2 || colIndex == 5) ? colVal : 0;
170  }
171  else if (elem->grad == GradientType::RAINBOW)
172  {
173  const Color &startCol = RAINBOW_COLORS[colIndex];
174  const Color &destCol
175  = RAINBOW_COLORS[(colIndex + 1) % numOfColors];
176  double startColVal;
177  double destColVal;
178 
179  if (delay != 0)
180  startColVal = (cos(M_PI * pos / delay) + 1) / 2;
181  else
182  startColVal = 0;
183 
184  destColVal = 1 - startColVal;
185 
186  color.r = CAST_S32(startColVal
187  * startCol.r + destColVal * destCol.r);
188 
189  color.g = CAST_S32(startColVal
190  * startCol.g + destColVal * destCol.g);
191 
192  color.b = CAST_S32(startColVal
193  * startCol.b + destColVal * destCol.b);
194  }
195  }
196 
198  }
199 }
volatile int tick_time
Definition: timer.cpp:52
#define A_NONNULLPOINTER
Definition: localconsts.h:265
GradientType ::T GradientTypeT
Definition: gradienttype.h:37
unsigned int g
Definition: color.h:239
std::vector< ColorElem * > mGradVector
Definition: palette.h:155
static const Color RAINBOW_COLORS[7]
Definition: palette.h:80
int get_elapsed_time(const int startTime)
Definition: timer.cpp:93
#define CAST_S32
Definition: cast.h:29
unsigned int b
Definition: color.h:244
static const int RAINBOW_COLOR_COUNT
Definition: palette.h:81
Definition: color.h:74
int mRainbowTime
Definition: palette.h:84
unsigned int r
Definition: color.h:234

◆ advanceGradients()

void Palette::advanceGradients ( )
static

Updates all colors, that are non-static.

Definition at line 78 of file palette.cpp.

References FOR_EACH, and mInstances.

Referenced by getColorChar(), and Gui::slowLogic().

79 {
80  FOR_EACH (Palettes::const_iterator, it, mInstances)
81  (*it)->advanceGradient();
82 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
static Palettes mInstances
Definition: palette.h:87

◆ getCharColor()

const Color & Palette::getCharColor ( const signed char  c,
bool valid 
) const

Returns the color associated with a character, if it exists. Returns Palette::BLACK if the character is not found.

Parameters
ccharacter requested
validindicate whether character is known
Returns
the requested color or Palette::BLACK

Definition at line 65 of file palette.cpp.

References BLACK, mCharColors, and mColors.

66 {
67  const CharColors::const_iterator it = mCharColors.find(c);
68  if (it != mCharColors.end())
69  {
70  valid = true;
71  return mColors[(*it).second].color;
72  }
73 
74  valid = false;
75  return BLACK;
76 }
static const Color BLACK
Definition: palette.h:47
Colors mColors
Definition: palette.h:153
CharColors mCharColors
Definition: palette.h:154

◆ getColorChar()

char Palette::getColorChar ( const int  type) const
inline

Get the character used by the specified color.

Parameters
typethe color type of the color
Returns
the color char of the color with the given index

Definition at line 70 of file palette.h.

References advanceGradients(), CAST_SIZE, and mColors.

71  { return mColors[CAST_SIZE(type)].ch; }
Colors mColors
Definition: palette.h:153
#define CAST_SIZE
Definition: cast.h:33

Field Documentation

◆ BLACK

const Color Palette::BLACK = Color(0, 0, 0, 255)
static

Black Color Constant

Definition at line 47 of file palette.h.

Referenced by getCharColor(), Widget2::getThemeCharColor(), and readColor().

◆ mCharColors

CharColors Palette::mCharColors
protected

◆ mColors

Colors Palette::mColors
protected

◆ mGradVector

std::vector<ColorElem*> Palette::mGradVector
protected

Definition at line 155 of file palette.h.

Referenced by UserPalette::addColor(), advanceGradient(), and UserPalette::setGradient().

◆ mInstances

Palette::Palettes Palette::mInstances
staticprotected

Definition at line 87 of file palette.h.

Referenced by advanceGradients(), Palette(), and ~Palette().

◆ mRainbowTime

int Palette::mRainbowTime
protected

Time tick, that gradient-type colors were updated the last time.

Definition at line 84 of file palette.h.

Referenced by advanceGradient().

◆ RAINBOW_COLOR_COUNT

const int Palette::RAINBOW_COLOR_COUNT = 7
staticprotected

Definition at line 81 of file palette.h.

Referenced by advanceGradient().

◆ RAINBOW_COLORS

const Color Palette::RAINBOW_COLORS
staticprotected
Initial value:
=
{
Color(255, 0, 0, 255),
Color(255, 153, 0, 255),
Color(255, 255, 0, 255),
Color(0, 153, 0, 255),
Color(0, 204, 204, 255),
Color(51, 0, 153, 255),
Color(153, 0, 153, 255)
}

Colors used for the rainbow gradient

Definition at line 80 of file palette.h.

Referenced by advanceGradient().


The documentation for this class was generated from the following files: