diff options
author | Andrei Karas <akaras@inbox.ru> | 2012-07-16 00:59:25 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2012-07-16 00:59:25 +0300 |
commit | 7cd117790755de302be9f750b95b5bd18a5636bd (patch) | |
tree | 7740d5c4ae3eb4830ae2a56c6a388f7c01a2a964 /src/resources/dye.cpp | |
parent | 0079782d58f97cf3a931a0d7e23677588834363e (diff) | |
download | ManaVerse-7cd117790755de302be9f750b95b5bd18a5636bd.tar.gz ManaVerse-7cd117790755de302be9f750b95b5bd18a5636bd.tar.bz2 ManaVerse-7cd117790755de302be9f750b95b5bd18a5636bd.tar.xz ManaVerse-7cd117790755de302be9f750b95b5bd18a5636bd.zip |
Simplify dye palette parsing.
Diffstat (limited to 'src/resources/dye.cpp')
-rw-r--r-- | src/resources/dye.cpp | 55 |
1 files changed, 21 insertions, 34 deletions
diff --git a/src/resources/dye.cpp b/src/resources/dye.cpp index 59d8f18df..954817103 100644 --- a/src/resources/dye.cpp +++ b/src/resources/dye.cpp @@ -31,7 +31,7 @@ DyePalette::DyePalette(const std::string &description) { - int size = static_cast<int>(description.length()); + const int size = static_cast<int>(description.length()); if (size == 0) return; if (description[0] != '#') @@ -46,42 +46,17 @@ DyePalette::DyePalette(const std::string &description) if (pos + 6 > size) break; - int v = 0; - for (int i = 0; i < 6; ++i) + Color color = { - char c = description[pos + i]; - int n; - - if ('0' <= c && c <= '9') - { - n = c - '0'; - } - else if ('A' <= c && c <= 'F') - { - n = c - 'A' + 10; - } - else if ('a' <= c && c <= 'f') - { - n = c - 'a' + 10; - } - else - { - logger->log("Error, invalid embedded palette: %s", - description.c_str()); - return; - } + {0, 0, 0} + }; - v = (v << 4) | n; - } - Color c = + for (int i = 0, colorIdx = 0; i < 6; i +=2, colorIdx ++) { - { - static_cast<unsigned char>(v >> 16), - static_cast<unsigned char>(v >> 8), - static_cast<unsigned char>(v) - } - }; - mColors.push_back(c); + color.value[colorIdx] = (hexDecode(description[pos + i]) << 4) + + hexDecode(description[pos + i + 1]); + } + mColors.push_back(color); pos += 6; if (pos == size) @@ -95,6 +70,18 @@ DyePalette::DyePalette(const std::string &description) logger->log("Error, invalid embedded palette: %s", description.c_str()); } +int DyePalette::hexDecode(char c) +{ + if ('0' <= c && c <= '9') + return c - '0'; + else if ('A' <= c && c <= 'F') + return c - 'A' + 10; + else if ('a' <= c && c <= 'f') + return c - 'a' + 10; + else + return 0; +} + /* void DyePalette::addFirstColor(const int color[3]) { |