diff options
author | BlockMen <nmuelll@web.de> | 2014-10-03 06:11:21 +0200 |
---|---|---|
committer | BlockMen <nmuelll@web.de> | 2014-10-05 16:49:52 +0200 |
commit | 28438bba27168289be59a26d3ae55e3f3658d8d3 (patch) | |
tree | cba018e33e53789aa667eb892aeb82a8451737cb /src/util | |
parent | 173beeee65289464d2541c44f753d21cd1fa3155 (diff) | |
download | minetest-28438bba27168289be59a26d3ae55e3f3658d8d3.tar.gz minetest-28438bba27168289be59a26d3ae55e3f3658d8d3.tar.bz2 minetest-28438bba27168289be59a26d3ae55e3f3658d8d3.zip |
Add [colorize modifier
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/string.cpp | 63 | ||||
-rw-r--r-- | src/util/string.h | 3 |
2 files changed, 65 insertions, 1 deletions
diff --git a/src/util/string.cpp b/src/util/string.cpp index 5ba97afd5..57545aa20 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "string.h" #include "pointer.h" #include "numeric.h" +#include "log.h" #include <sstream> #include <iomanip> @@ -303,3 +304,65 @@ u64 read_seed(const char *str) return num; } + +bool parseColorString(const std::string &value, video::SColor &color, bool quiet) +{ + const char *hexpattern = NULL; + video::SColor outcolor(255, 255, 255, 255); + + if (value[0] == '#') { + if (value.size() == 9) + hexpattern = "#RRGGBBAA"; + else if (value.size() == 7) + hexpattern = "#RRGGBB"; + else if (value.size() == 5) + hexpattern = "#RGBA"; + else if (value.size() == 4) + hexpattern = "#RGB"; + } + + if (!hexpattern) + goto fail; + + assert(strlen(hexpattern) == value.size()); + for (size_t pos = 0; pos < value.size(); ++pos) { + // '#' in the pattern means skip that character + if (hexpattern[pos] == '#') + continue; + + // Else assume hexpattern[pos] is one of 'R' 'G' 'B' 'A' + // Read one or two digits, depending on hexpattern + unsigned char c1, c2; + if (hexpattern[pos+1] == hexpattern[pos]) { + // Two digits, e.g. hexpattern == "#RRGGBB" + if (!hex_digit_decode(value[pos], c1) || + !hex_digit_decode(value[pos+1], c2)) + goto fail; + ++pos; + } else { + // One digit, e.g. hexpattern == "#RGB" + if (!hex_digit_decode(value[pos], c1)) + goto fail; + c2 = c1; + } + u32 colorpart = ((c1 & 0x0f) << 4) | (c2 & 0x0f); + + // Update outcolor with newly read color part + if (hexpattern[pos] == 'R') + outcolor.setRed(colorpart); + else if (hexpattern[pos] == 'G') + outcolor.setGreen(colorpart); + else if (hexpattern[pos] == 'B') + outcolor.setBlue(colorpart); + else if (hexpattern[pos] == 'A') + outcolor.setAlpha(colorpart); + } + + color = outcolor; + return true; + +fail: + if (!quiet) + errorstream << "Invalid color: \"" << value << "\"" << std::endl; + return false; +} diff --git a/src/util/string.h b/src/util/string.h index 54a5a458e..e46fbf4e9 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef UTIL_STRING_HEADER #define UTIL_STRING_HEADER -#include "../irrlichttypes.h" +#include "irrlichttypes_bloated.h" #include <stdlib.h> #include <string> #include <cstring> @@ -349,6 +349,7 @@ std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask); size_t mystrlcpy(char *dst, const char *src, size_t size); char *mystrtok_r(char *s, const char *sep, char **lasts); u64 read_seed(const char *str); +bool parseColorString(const std::string &value, video::SColor &color, bool quiet); #endif |