00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "resources/dye.h"
00023
00024 #include "log.h"
00025
00026 #include <sstream>
00027
00028 DyePalette::DyePalette(const std::string &description)
00029 {
00030 int size = description.length();
00031 if (size == 0)
00032 return;
00033 if (description[0] != '#')
00034 {
00035
00036 return;
00037 }
00038
00039 int pos = 1;
00040 for (;;)
00041 {
00042 if (pos + 6 > size)
00043 break;
00044
00045 int v = 0;
00046 for (int i = 0; i < 6; ++i)
00047 {
00048 char c = description[pos + i];
00049 int n;
00050
00051 if ('0' <= c && c <= '9')
00052 n = c - '0';
00053 else if ('A' <= c && c <= 'F')
00054 n = c - 'A' + 10;
00055 else if ('a' <= c && c <= 'f')
00056 n = c - 'a' + 10;
00057 else
00058 goto error;
00059
00060 v = (v << 4) | n;
00061 }
00062 Color c = { { v >> 16, v >> 8, v } };
00063 mColors.push_back(c);
00064 pos += 6;
00065
00066 if (pos == size)
00067 return;
00068 if (description[pos] != ',')
00069 break;
00070
00071 ++pos;
00072 }
00073
00074 error:
00075 logger->log("Error, invalid embedded palette: %s", description.c_str());
00076 }
00077
00078 void DyePalette::getColor(int intensity, int color[3]) const
00079 {
00080 if (intensity == 0)
00081 {
00082 color[0] = 0;
00083 color[1] = 0;
00084 color[2] = 0;
00085 return;
00086 }
00087
00088 int last = mColors.size();
00089 if (last == 0) return;
00090
00091 int i = intensity * last / 255;
00092 int t = intensity * last % 255;
00093
00094 int j = t != 0 ? i : i - 1;
00095
00096 int r2 = mColors[j].value[0],
00097 g2 = mColors[j].value[1],
00098 b2 = mColors[j].value[2];
00099
00100 if (t == 0)
00101 {
00102
00103 color[0] = r2;
00104 color[1] = g2;
00105 color[2] = b2;
00106 return;
00107 }
00108
00109
00110 int r1 = 0, g1 = 0, b1 = 0;
00111 if (i > 0)
00112 {
00113 r1 = mColors[i - 1].value[0];
00114 g1 = mColors[i - 1].value[1];
00115 b1 = mColors[i - 1].value[2];
00116 }
00117
00118
00119 color[0] = ((255 - t) * r1 + t * r2) / 255;
00120 color[1] = ((255 - t) * g1 + t * g2) / 255;
00121 color[2] = ((255 - t) * b1 + t * b2) / 255;
00122 }
00123
00124 Dye::Dye(const std::string &description)
00125 {
00126 for (int i = 0; i < 7; ++i)
00127 mDyePalettes[i] = 0;
00128
00129 if (description.empty())
00130 return;
00131
00132 std::string::size_type next_pos = 0, length = description.length();
00133 do
00134 {
00135 std::string::size_type pos = next_pos;
00136 next_pos = description.find(';', pos);
00137
00138 if (next_pos == std::string::npos)
00139 next_pos = length;
00140
00141 if (next_pos <= pos + 3 || description[pos + 1] != ':')
00142 {
00143 logger->log("Error, invalid dye: %s", description.c_str());
00144 return;
00145 }
00146
00147 int i = 0;
00148
00149 switch (description[pos])
00150 {
00151 case 'R': i = 0; break;
00152 case 'G': i = 1; break;
00153 case 'Y': i = 2; break;
00154 case 'B': i = 3; break;
00155 case 'M': i = 4; break;
00156 case 'C': i = 5; break;
00157 case 'W': i = 6; break;
00158 default:
00159 logger->log("Error, invalid dye: %s", description.c_str());
00160 return;
00161 }
00162 mDyePalettes[i] = new DyePalette(description.substr(pos + 2,
00163 next_pos - pos - 2));
00164 ++next_pos;
00165 }
00166 while (next_pos < length);
00167 }
00168
00169 Dye::~Dye()
00170 {
00171 for (int i = 0; i < 7; ++i)
00172 delete mDyePalettes[i];
00173 }
00174
00175 void Dye::update(int color[3]) const
00176 {
00177 int cmax = std::max(color[0], std::max(color[1], color[2]));
00178 if (cmax == 0)
00179 return;
00180
00181 int cmin = std::min(color[0], std::min(color[1], color[2]));
00182 int intensity = color[0] + color[1] + color[2];
00183
00184 if (cmin != cmax &&
00185 (cmin != 0 || (intensity != cmax && intensity != 2 * cmax)))
00186 {
00187
00188 return;
00189 }
00190
00191 int i = (color[0] != 0) | ((color[1] != 0) << 1) | ((color[2] != 0) << 2);
00192
00193 if (mDyePalettes[i - 1])
00194 mDyePalettes[i - 1]->getColor(cmax, color);
00195 }
00196
00197 void Dye::instantiate(std::string &target, const std::string &palettes)
00198 {
00199 std::string::size_type next_pos = target.find('|');
00200
00201 if (next_pos == std::string::npos || palettes.empty())
00202 return;
00203
00204 ++next_pos;
00205
00206 std::ostringstream s;
00207 s << target.substr(0, next_pos);
00208 std::string::size_type last_pos = target.length(), pal_pos = 0;
00209 do
00210 {
00211 std::string::size_type pos = next_pos;
00212 next_pos = target.find(';', pos);
00213
00214 if (next_pos == std::string::npos)
00215 next_pos = last_pos;
00216
00217 if (next_pos == pos + 1 && pal_pos != std::string::npos)
00218 {
00219 std::string::size_type pal_next_pos = palettes.find(';', pal_pos);
00220 s << target[pos] << ':';
00221 if (pal_next_pos == std::string::npos)
00222 {
00223 s << palettes.substr(pal_pos);
00224 s << target.substr(next_pos);
00225 pal_pos = std::string::npos;
00226 break;
00227 }
00228 s << palettes.substr(pal_pos, pal_next_pos - pal_pos);
00229 pal_pos = pal_next_pos + 1;
00230 }
00231 else if (next_pos > pos + 2)
00232 {
00233 s << target.substr(pos, next_pos - pos);
00234 }
00235 else
00236 {
00237 logger->log("Error, invalid dye placeholder: %s", target.c_str());
00238 return;
00239 }
00240 s << target[next_pos];
00241 ++next_pos;
00242 }
00243 while (next_pos < last_pos);
00244
00245 target = s.str();
00246 }