From 7ba370f2311ab2e36aa00408e5cfae4c25aa0bca Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 12 Jan 2016 03:35:31 +0300 Subject: Simplify dyepalette parsing and fix issue with parsing gimp colors in palette. --- src/resources/dye/dyepalette.cpp | 42 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/resources/dye/dyepalette.cpp b/src/resources/dye/dyepalette.cpp index a35d28437..a290805b6 100644 --- a/src/resources/dye/dyepalette.cpp +++ b/src/resources/dye/dyepalette.cpp @@ -28,6 +28,8 @@ #include "resources/db/palettedb.h" #endif +#include "utils/stringutils.h" + #include #include @@ -42,49 +44,33 @@ DyePalette::DyePalette(const std::string &restrict description, if (size == 0) return; + StringVect parts; + splitToStringVector(parts, description.substr(1), ','); if (description[0] == '#') { - size_t pos = 1; - for ( ; ; ) + FOR_EACH (StringVectCIter, it, parts) { - if (pos + blockSize > size) - break; - DyeColor color(0, 0, 0, 0); + const std::string str = *it; - for (size_t i = 0, colorIdx = 0; i < blockSize && colorIdx < 4; + for (size_t i = 0, colorIdx = 0; + i < blockSize && colorIdx < 4; i += 2, colorIdx ++) { color.value[colorIdx] = static_cast(( - hexDecode(description[pos + i]) << 4) - + hexDecode(description[pos + i + 1])); + hexDecode(str[i]) << 4) + + hexDecode(str[i + 1])); } mColors.push_back(color); - pos += blockSize; - - if (pos == size) - return; - if (description[pos] != ',') - break; - - ++pos; } + return; } #ifndef DYECMD else if (description[0] == '@') { - size_t pos = 1; - for ( ; pos < size ; ) - { - const size_t idx = description.find(',', pos); - if (idx == std::string::npos) - return; - if (idx == pos) - break; - mColors.push_back(PaletteDB::getColor( - description.substr(pos, idx - pos))); - pos = idx + 1; - } + FOR_EACH (StringVectCIter, it, parts) + mColors.push_back(PaletteDB::getColor(*it)); + return; } #endif logger->log("Error, invalid embedded palette: %s", description.c_str()); -- cgit v1.2.3-70-g09d2