ManaPlus
Public Member Functions | Static Public Member Functions | Private Attributes
Dye Class Reference

#include <dye.h>

Public Member Functions

 Dye (const std::string &dye)
 
 ~Dye ()
 
const DyePalettegetSPalete () const
 
const DyePalettegetAPalete () const
 
int getType () const
 
void normalDye (uint32_t *pixels, const int bufSize) const
 
void normalOGLDye (uint32_t *pixels, const int bufSize) const
 

Static Public Member Functions

static void instantiate (std::string &target, const std::string &palettes)
 

Private Attributes

DyePalettemDyePalettes [dyePalateSize]
 

Detailed Description

Class for dispatching pixel-recoloring amongst several palettes.

Definition at line 39 of file dye.h.

Constructor & Destructor Documentation

◆ Dye()

Dye::Dye ( const std::string &  dye)
explicit

Creates a set of palettes based on the given string.

The parts of string are separated by semi-colons. Each part starts by an uppercase letter, followed by a colon and then a palette name.

Definition at line 42 of file dye.cpp.

43 {
44  for (int i = 0; i < dyePalateSize; ++i)
45  mDyePalettes[i] = nullptr;
46 
47  if (description.empty())
48  return;
49 
50  size_t next_pos = 0;
51  const size_t length = description.length();
52  do
53  {
54  const size_t pos = next_pos;
55  next_pos = description.find(';', pos);
56 
57  if (next_pos == std::string::npos)
58  next_pos = length;
59 
60  if (next_pos <= pos + 3 || description[pos + 1] != ':')
61  {
62  logger->log("Error, invalid dye: %s", description.c_str());
63  return;
64  }
65 
66  int i = 0;
67 
68  switch (description[pos])
69  {
70  case 'R': i = 0; break;
71  case 'G': i = 1; break;
72  case 'Y': i = 2; break;
73  case 'B': i = 3; break;
74  case 'M': i = 4; break;
75  case 'C': i = 5; break;
76  case 'W': i = 6; break;
77  case 'S': i = 7; break;
78  case 'A': i = 8; break;
79  default:
80  logger->log("Error, invalid dye: %s", description.c_str());
81  return;
82  }
83  mDyePalettes[i] = new DyePalette(description.substr(
84  pos + 2, next_pos - pos - 2), i != 8 ? 6 : 8);
85  ++next_pos;
86  }
87  while (next_pos < length);
88 }

References dyePalateSize, Logger::log(), logger, and mDyePalettes.

◆ ~Dye()

Dye::~Dye ( )

Destroys the associated palettes.

Definition at line 90 of file dye.cpp.

91 {
92  for (int i = 0; i < dyePalateSize; ++i)
94 }

References delete2, dyePalateSize, and mDyePalettes.

Member Function Documentation

◆ getAPalete()

const DyePalette* Dye::getAPalete ( ) const
inline

Return special dye palete (A)

Definition at line 72 of file dye.h.

73  { return mDyePalettes[aPaleteIndex]; }

References aPaleteIndex, and mDyePalettes.

Referenced by ImageHelper::load(), SDLImageHelper::load(), SafeOpenGLImageHelper::load(), and OpenGLImageHelper::load().

◆ getSPalete()

const DyePalette* Dye::getSPalete ( ) const
inline

Return special dye palete (S)

Definition at line 66 of file dye.h.

67  { return mDyePalettes[sPaleteIndex]; }

References mDyePalettes, and sPaleteIndex.

Referenced by ImageHelper::load(), SDLImageHelper::load(), SafeOpenGLImageHelper::load(), and OpenGLImageHelper::load().

◆ getType()

int Dye::getType ( ) const

Return dye type for S - 1, for A - 2, 0 for other

Definition at line 148 of file dye.cpp.

149 {
150  if (mDyePalettes[sPaleteIndex] != nullptr)
151  return 1;
152  if (mDyePalettes[aPaleteIndex] != nullptr)
153  return 2;
154  return 0;
155 }

References aPaleteIndex, mDyePalettes, and sPaleteIndex.

Referenced by ImageHelper::load(), SDLImageHelper::load(), SafeOpenGLImageHelper::load(), and OpenGLImageHelper::load().

◆ instantiate()

void Dye::instantiate ( std::string &  target,
const std::string &  palettes 
)
static

Fills the blank in a dye placeholder with some palette names.

Definition at line 96 of file dye.cpp.

98 {
99  size_t next_pos = target.find('|');
100 
101  if (next_pos == std::string::npos || palettes.empty())
102  return;
103 
104  ++next_pos;
105 
106  std::ostringstream s;
107  s << target.substr(0, next_pos);
108  size_t last_pos = target.length();
109  size_t pal_pos = 0;
110  do
111  {
112  const size_t pos = next_pos;
113  next_pos = target.find(';', pos);
114 
115  if (next_pos == std::string::npos)
116  next_pos = last_pos;
117 
118  if (next_pos == pos + 1 && pal_pos != std::string::npos)
119  {
120  const size_t pal_next_pos = palettes.find(';', pal_pos);
121  s << target[pos] << ':';
122  if (pal_next_pos == std::string::npos)
123  {
124  s << palettes.substr(pal_pos);
125  s << target.substr(next_pos);
126  break;
127  }
128  s << palettes.substr(pal_pos, pal_next_pos - pal_pos);
129  pal_pos = pal_next_pos + 1;
130  }
131  else if (next_pos > pos + 2)
132  {
133  s << target.substr(pos, next_pos - pos);
134  }
135  else
136  {
137  logger->log("Error, invalid dye placeholder: %s", target.c_str());
138  return;
139  }
140  s << target[next_pos];
141  ++next_pos;
142  }
143  while (next_pos < last_pos);
144 
145  target = s.str();
146 }

References Logger::log(), and logger.

Referenced by Particle::addEffect(), ParticleEngine::addEffect(), SimpleAnimation::initializeAnimation(), SpriteDef::loadImageSet(), and ParticleEmitter::ParticleEmitter().

◆ normalDye()

void Dye::normalDye ( uint32_t *  pixels,
const int  bufSize 
) const

Definition at line 157 of file dye.cpp.

159 {
160  if (pixels == nullptr)
161  return;
162 
163  for (const uint32_t *const p_end = pixels + CAST_SIZE(bufSize);
164  pixels != p_end;
165  ++ pixels)
166  {
167  const uint32_t p = *pixels;
168 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
169  const int alpha = p & 0xff000000;
170 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
171 
172  const int alpha = p & 0xff;
173 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
174 
175  if (alpha == 0)
176  continue;
177  unsigned int color[3];
178 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
179  color[0] = (p) & 255U;
180  color[1] = (p >> 8U) & 255U;
181  color[2] = (p >> 16U) & 255U;
182 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
183 
184  color[0] = (p >> 24U) & 255U;
185  color[1] = (p >> 16U) & 255U;
186  color[2] = (p >> 8U) & 255U;
187 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
188 
189  const unsigned int cmax = std::max(
190  color[0], std::max(color[1], color[2]));
191  if (cmax == 0)
192  continue;
193 
194  const unsigned int cmin = std::min(
195  color[0], std::min(color[1], color[2]));
196  const unsigned int intensity = color[0] + color[1] + color[2];
197 
198  if (cmin != cmax && (cmin != 0 || (intensity != cmax
199  && intensity != 2 * cmax)))
200  {
201  // not pure
202  continue;
203  }
204 
205  const unsigned int i = static_cast<int>(color[0] != 0) |
206  (static_cast<int>(color[1] != 0) << 1) |
207  (static_cast<int>(color[2] != 0) << 2);
208 
209  if (mDyePalettes[i - 1] != nullptr)
210  mDyePalettes[i - 1]->getColor(cmax, color);
211 
212 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
213  *pixels = (color[0]) | (color[1] << 8)
214  | (color[2] << 16) | alpha;
215 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
216 
217  *pixels = (color[0] << 24) | (color[1] << 16)
218  | (color[2] << 8) | alpha;
219 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
220  }
221 }

References CAST_SIZE.

Referenced by ImageHelper::load(), and SDLImageHelper::load().

◆ normalOGLDye()

void Dye::normalOGLDye ( uint32_t *  pixels,
const int  bufSize 
) const

Definition at line 223 of file dye.cpp.

225 {
226  if (pixels == nullptr)
227  return;
228 
229  for (const uint32_t *const p_end = pixels + CAST_SIZE(bufSize);
230  pixels != p_end;
231  ++ pixels)
232  {
233  const uint32_t p = *pixels;
234 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
235  const uint32_t alpha = p & 255U;
236 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
237 
238  const uint32_t alpha = p & 0xff000000U;
239 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
240 
241  if (alpha == 0)
242  continue;
243  unsigned int color[3];
244 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
245  color[0] = (p >> 24U) & 255U;
246  color[1] = (p >> 16U) & 255U;
247  color[2] = (p >> 8U) & 255U;
248 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
249 
250  color[0] = (p) & 255U;
251  color[1] = (p >> 8U) & 255U;
252  color[2] = (p >> 16U) & 255U;
253 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
254 
255  const unsigned int cmax = std::max(
256  color[0], std::max(color[1], color[2]));
257  if (cmax == 0)
258  continue;
259 
260  const unsigned int cmin = std::min(
261  color[0], std::min(color[1], color[2]));
262  const unsigned int intensity = color[0] + color[1] + color[2];
263 
264  if (cmin != cmax && (cmin != 0 || (intensity != cmax
265  && intensity != 2 * cmax)))
266  {
267  // not pure
268  continue;
269  }
270 
271  const unsigned int i = static_cast<int>(color[0] != 0) |
272  (static_cast<int>(color[1] != 0) << 1) |
273  (static_cast<int>(color[2] != 0) << 2);
274 
275  if (mDyePalettes[i - 1] != nullptr)
276  mDyePalettes[i - 1]->getColor(cmax, color);
277 
278 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
279  *pixels = (color[0] << 24) | (color[1] << 16)
280  | (color[2] << 8) | alpha;
281 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
282 
283  *pixels = (color[0]) | (color[1] << 8)
284  | (color[2] << 16) | alpha;
285 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
286  }
287 }

References CAST_SIZE.

Referenced by OpenGLImageHelper::load(), and SafeOpenGLImageHelper::load().

Field Documentation

◆ mDyePalettes

DyePalette* Dye::mDyePalettes[dyePalateSize]
private

The order of the palettes, as well as their uppercase letter, is:

Red, Green, Yellow, Blue, Magenta, White (or rather gray), Simple.

Definition at line 92 of file dye.h.

Referenced by Dye(), getAPalete(), getSPalete(), getType(), and ~Dye().


The documentation for this class was generated from the following files:
sPaleteIndex
const int sPaleteIndex
Definition: dye.h:33
CAST_SIZE
#define CAST_SIZE
Definition: cast.h:33
aPaleteIndex
const int aPaleteIndex
Definition: dye.h:34
Dye::mDyePalettes
DyePalette * mDyePalettes[dyePalateSize]
Definition: dye.h:92
DyePalette
Definition: dyepalette.h:45
delete2
#define delete2(var)
Definition: delete2.h:24
logger
Logger * logger
Definition: logger.cpp:88
DyePalette::getColor
void getColor(const unsigned int intensity, unsigned int(&color)[3]) const
Definition: dyepalette.cpp:151
dyePalateSize
const int dyePalateSize
Definition: dye.h:32
Logger::log
void log(const char *const log_text,...)
Definition: logger.cpp:268