Mana
Loading...
Searching...
No Matches
dye.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2007-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "resources/dye.h"
23
24#include "log.h"
25
26#include <cmath>
27#include <sstream>
28
29DyePalette::DyePalette(const std::string &description)
30{
31 int size = description.length();
32 if (size == 0)
33 return;
34 if (description[0] != '#')
35 {
36 // TODO: load palette from file.
37 return;
38 }
39
40 int pos = 1;
41 for (;;)
42 {
43 if (pos + 6 > size)
44 break;
45
46 int v = 0;
47 for (int i = 0; i < 6; ++i)
48 {
49 char c = description[pos + i];
50 int n;
51
52 if ('0' <= c && c <= '9')
53 {
54 n = c - '0';
55 }
56 else if ('A' <= c && c <= 'F')
57 {
58 n = c - 'A' + 10;
59 }
60 else if ('a' <= c && c <= 'f')
61 {
62 n = c - 'a' + 10;
63 }
64 else
65 {
66 Log::info("Error, invalid embedded palette: %s",
67 description.c_str());
68 return;
69 }
70
71 v = (v << 4) | n;
72 }
73 Color c = { (unsigned char) (v >> 16), (unsigned char) (v >> 8), (unsigned char) v };
74 mColors.push_back(c);
75 pos += 6;
76
77 if (pos == size)
78 return;
79 if (description[pos] != ',')
80 break;
81
82 ++pos;
83 }
84
85 Log::info("Error, invalid embedded palette: %s", description.c_str());
86}
87
88void DyePalette::getColor(int intensity, int color[3]) const
89{
90 if (intensity == 0)
91 {
92 color[0] = 0;
93 color[1] = 0;
94 color[2] = 0;
95 return;
96 }
97
98 int last = mColors.size();
99 if (last == 0) return;
100
101 int i = intensity * last / 255;
102 int t = intensity * last % 255;
103
104 int j = t != 0 ? i : i - 1;
105 // Get the exact color if any, the next color otherwise.
106 int r2 = mColors[j].r,
107 g2 = mColors[j].g,
108 b2 = mColors[j].b;
109
110 if (t == 0)
111 {
112 // Exact color.
113 color[0] = r2;
114 color[1] = g2;
115 color[2] = b2;
116 return;
117 }
118
119 // Get the previous color. First color is implicitly black.
120 int r1 = 0, g1 = 0, b1 = 0;
121 if (i > 0)
122 {
123 r1 = mColors[i - 1].r;
124 g1 = mColors[i - 1].g;
125 b1 = mColors[i - 1].b;
126 }
127
128 // Perform a linear interpolation.
129 color[0] = ((255 - t) * r1 + t * r2) / 255;
130 color[1] = ((255 - t) * g1 + t * g2) / 255;
131 color[2] = ((255 - t) * b1 + t * b2) / 255;
132}
133
134void DyePalette::getColor(double intensity, int color[3]) const
135{
136 // Nothing to do here
137 if (mColors.empty())
138 return;
139
140 // Force range
141 if (intensity > 1.0)
142 intensity = 1.0;
143 else if (intensity < 0.0)
144 intensity = 0.0;
145
146 // Scale up
147 intensity = intensity * (mColors.size() - 1);
148
149 // Color indices
150 int i = (int) floor(intensity);
151 int j = (int) ceil(intensity);
152
153 if (i == j)
154 {
155 // Exact color.
156 color[0] = mColors[i].r;
157 color[1] = mColors[i].g;
158 color[2] = mColors[i].b;
159 return;
160 }
161
162 intensity -= i;
163 double rest = 1 - intensity;
164
165 // Get the colors
166 int r1 = mColors[i].r,
167 g1 = mColors[i].g,
168 b1 = mColors[i].b,
169 r2 = mColors[j].r,
170 g2 = mColors[j].g,
171 b2 = mColors[j].b;
172
173 // Perform the interpolation.
174 color[0] = (rest * r1 + intensity * r2);
175 color[1] = (rest * g1 + intensity * g2);
176 color[2] = (rest * b1 + intensity * b2);
177}
178
179Dye::Dye(const std::string &description)
180{
181 for (auto &dyePalette : mDyePalettes)
182 dyePalette = nullptr;
183
184 if (description.empty())
185 return;
186
187 std::string::size_type next_pos = 0, length = description.length();
188 do
189 {
190 std::string::size_type pos = next_pos;
191 next_pos = description.find(';', pos);
192
193 if (next_pos == std::string::npos)
194 next_pos = length;
195
196 if (next_pos <= pos + 3 || description[pos + 1] != ':')
197 {
198 Log::info("Error, invalid dye: %s", description.c_str());
199 return;
200 }
201
202 int i = 0;
203
204 switch (description[pos])
205 {
206 case 'R': i = 0; break;
207 case 'G': i = 1; break;
208 case 'Y': i = 2; break;
209 case 'B': i = 3; break;
210 case 'M': i = 4; break;
211 case 'C': i = 5; break;
212 case 'W': i = 6; break;
213 default:
214 Log::info("Error, invalid dye: %s", description.c_str());
215 return;
216 }
217 mDyePalettes[i] = new DyePalette(description.substr(pos + 2,
218 next_pos - pos - 2));
219 ++next_pos;
220 }
221 while (next_pos < length);
222}
223
225{
226 for (auto &dyePalette : mDyePalettes)
227 delete dyePalette;
228}
229
230void Dye::update(int color[3]) const
231{
232 int cmax = std::max(color[0], std::max(color[1], color[2]));
233 if (cmax == 0)
234 return;
235
236 int cmin = std::min(color[0], std::min(color[1], color[2]));
237 int intensity = color[0] + color[1] + color[2];
238
239 if (cmin != cmax &&
240 (cmin != 0 || (intensity != cmax && intensity != 2 * cmax)))
241 {
242 // not pure
243 return;
244 }
245
246 int i = (color[0] != 0) | ((color[1] != 0) << 1) | ((color[2] != 0) << 2);
247
248 if (mDyePalettes[i - 1])
249 mDyePalettes[i - 1]->getColor(cmax, color);
250}
251
252void Dye::instantiate(std::string &target, const std::string &palettes)
253{
254 std::string::size_type next_pos = target.find('|');
255
256 if (next_pos == std::string::npos || palettes.empty())
257 return;
258
259 ++next_pos;
260
261 std::ostringstream s;
262 s << target.substr(0, next_pos);
263 std::string::size_type last_pos = target.length(), pal_pos = 0;
264 do
265 {
266 std::string::size_type pos = next_pos;
267 next_pos = target.find(';', pos);
268
269 if (next_pos == std::string::npos)
270 next_pos = last_pos;
271
272 if (next_pos == pos + 1 && pal_pos != std::string::npos)
273 {
274 std::string::size_type pal_next_pos = palettes.find(';', pal_pos);
275 s << target[pos] << ':';
276 if (pal_next_pos == std::string::npos)
277 {
278 s << palettes.substr(pal_pos);
279 s << target.substr(next_pos);
280 pal_pos = std::string::npos;
281 break;
282 }
283 s << palettes.substr(pal_pos, pal_next_pos - pal_pos);
284 pal_pos = pal_next_pos + 1;
285 }
286 else if (next_pos > pos + 2)
287 {
288 s << target.substr(pos, next_pos - pos);
289 }
290 else
291 {
292 Log::info("Error, invalid dye placeholder: %s", target.c_str());
293 return;
294 }
295 s << target[next_pos];
296 ++next_pos;
297 }
298 while (next_pos < last_pos);
299
300 target = s.str();
301}
Class for performing a linear interpolation between colors.
Definition dye.h:31
DyePalette(const std::string &description)
Creates a palette based on the given string.
Definition dye.cpp:29
void getColor(int intensity, int color[3]) const
Gets a pixel color depending on its intensity.
Definition dye.cpp:88
std::vector< Color > mColors
Definition dye.h:59
void update(int color[3]) const
Modifies a pixel color.
Definition dye.cpp:230
Dye(const std::string &dye)
Creates a set of palettes based on the given string.
Definition dye.cpp:179
static void instantiate(std::string &target, const std::string &palettes)
Fills the blank in a dye placeholder with some palette names.
Definition dye.cpp:252
DyePalette * mDyePalettes[7]
The order of the palettes, as well as their uppercase letter, is:
Definition dye.h:99
~Dye()
Destroys the associated palettes.
Definition dye.cpp:224
void info(const char *log_text,...) LOG_PRINTF_ATTR