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 44 of file palette.h.

Member Typedef Documentation

◆ CharColors

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

Definition at line 153 of file palette.h.

◆ Colors

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

Definition at line 152 of file palette.h.

◆ Palettes

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

Definition at line 87 of file palette.h.

Constructor & Destructor Documentation

◆ Palette()

Palette::Palette ( const int  size)
explicitprotected

Constructor

Definition at line 52 of file palette.cpp.

52  :
55  mCharColors(),
56  mGradVector()
57 {
58  mInstances.insert(this);
59 }
Colors mColors
Definition: palette.h:154
static Palettes mInstances
Definition: palette.h:88
CharColors mCharColors
Definition: palette.h:155
std::vector< ColorElem * > mGradVector
Definition: palette.h:156
int mRainbowTime
Definition: palette.h:85
std::vector< ColorElem > Colors
Definition: palette.h:152
volatile int tick_time
Definition: timer.cpp:53
int size()
Definition: emotedb.cpp:306

References mInstances.

◆ ~Palette()

Palette::~Palette ( )
protectedvirtual

Destructor

Definition at line 61 of file palette.cpp.

62 {
63  mInstances.erase(this);
64 }

References mInstances.

Member Function Documentation

◆ advanceGradient()

void Palette::advanceGradient ( )
protected

Definition at line 85 of file palette.cpp.

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

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, and tick_time.

◆ advanceGradients()

void Palette::advanceGradients ( )
static

Updates all colors, that are non-static.

Definition at line 79 of file palette.cpp.

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

References FOR_EACH, and mInstances.

Referenced by Gui::slowLogic().

◆ 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 66 of file palette.cpp.

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

References BLACK, mCharColors, mColors, and anonymous_namespace{libxml.cpp}::valid.

◆ 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 71 of file palette.h.

72  { return mColors[CAST_SIZE(type)].ch; }
#define CAST_SIZE
Definition: cast.h:34

References CAST_SIZE, and mColors.

Field Documentation

◆ BLACK

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

Black Color Constant

Definition at line 48 of file palette.h.

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

◆ mCharColors

CharColors Palette::mCharColors
protected

Definition at line 155 of file palette.h.

Referenced by getCharColor(), UserPalette::getIdByChar(), and Theme::Theme().

◆ mColors

Colors Palette::mColors
protected

◆ mGradVector

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

Definition at line 156 of file palette.h.

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

◆ mInstances

Palette::Palettes Palette::mInstances
staticprotected

Definition at line 88 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 85 of file palette.h.

Referenced by advanceGradient().

◆ RAINBOW_COLOR_COUNT

const int Palette::RAINBOW_COLOR_COUNT = 7
staticprotected

Definition at line 82 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 81 of file palette.h.

Referenced by advanceGradient().


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