From 7cd117790755de302be9f750b95b5bd18a5636bd Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 16 Jul 2012 00:59:25 +0300 Subject: Simplify dye palette parsing. --- src/resources/dye.cpp | 55 ++++++++++++++++++++------------------------------- src/resources/dye.h | 2 ++ 2 files changed, 23 insertions(+), 34 deletions(-) (limited to 'src') 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(description.length()); + const int size = static_cast(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(v >> 16), - static_cast(v >> 8), - static_cast(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]) { diff --git a/src/resources/dye.h b/src/resources/dye.h index 26135d82e..7fe4869ef 100644 --- a/src/resources/dye.h +++ b/src/resources/dye.h @@ -65,6 +65,8 @@ class DyePalette void replaceOGLColor(uint8_t *color) const; + int hexDecode(char c); + private: struct Color { unsigned char value[3]; }; -- cgit v1.2.3-60-g2f50