summaryrefslogtreecommitdiff
path: root/src/resources
diff options
context:
space:
mode:
Diffstat (limited to 'src/resources')
-rw-r--r--src/resources/db/palettedb.cpp7
-rw-r--r--src/resources/db/palettedb.h2
-rw-r--r--src/resources/dye/dyepalette.cpp40
-rw-r--r--src/resources/dye/dyepalette.h5
-rw-r--r--src/resources/dye/dyepalette_unittest.cc123
5 files changed, 157 insertions, 20 deletions
diff --git a/src/resources/db/palettedb.cpp b/src/resources/db/palettedb.cpp
index 4da058d2a..a3caf62dc 100644
--- a/src/resources/db/palettedb.cpp
+++ b/src/resources/db/palettedb.cpp
@@ -34,7 +34,6 @@ namespace
{
bool mLoaded = false;
std::map<std::string, DyeColor> mColors;
- DyeColor mEmpty(0, 0, 0, 0);
}
void PaletteDB::load()
@@ -113,11 +112,11 @@ void PaletteDB::unload()
mColors.clear();
}
-const DyeColor &PaletteDB::getColor(const std::string &name)
+const DyeColor *PaletteDB::getColor(const std::string &name)
{
std::map<std::string, DyeColor>::const_iterator it = mColors.find(name);
if (it != mColors.end())
- return (*it).second;
+ return &(*it).second;
else
- return mEmpty;
+ return nullptr;
}
diff --git a/src/resources/db/palettedb.h b/src/resources/db/palettedb.h
index 6043d14b7..349f863f5 100644
--- a/src/resources/db/palettedb.h
+++ b/src/resources/db/palettedb.h
@@ -32,7 +32,7 @@ namespace PaletteDB
void load();
void unload();
void loadPalette();
- const DyeColor &getColor(const std::string &name);
+ const DyeColor *getColor(const std::string &name);
} // namespace PaletteDB
diff --git a/src/resources/dye/dyepalette.cpp b/src/resources/dye/dyepalette.cpp
index a290805b6..048b350e3 100644
--- a/src/resources/dye/dyepalette.cpp
+++ b/src/resources/dye/dyepalette.cpp
@@ -51,16 +51,7 @@ DyePalette::DyePalette(const std::string &restrict description,
FOR_EACH (StringVectCIter, it, parts)
{
DyeColor color(0, 0, 0, 0);
- const std::string str = *it;
-
- for (size_t i = 0, colorIdx = 0;
- i < blockSize && colorIdx < 4;
- i += 2, colorIdx ++)
- {
- color.value[colorIdx] = static_cast<unsigned char>((
- hexDecode(str[i]) << 4)
- + hexDecode(str[i + 1]));
- }
+ hexToColor(*it, blockSize, color);
mColors.push_back(color);
}
return;
@@ -69,13 +60,40 @@ DyePalette::DyePalette(const std::string &restrict description,
else if (description[0] == '@')
{
FOR_EACH (StringVectCIter, it, parts)
- mColors.push_back(PaletteDB::getColor(*it));
+ {
+ const std::string str = *it;
+ const DyeColor *const color = PaletteDB::getColor(str);
+ if (color)
+ {
+ mColors.push_back(*color);
+ }
+ else
+ {
+ DyeColor color(0, 0, 0, 0);
+ hexToColor(str, blockSize, color);
+ mColors.push_back(color);
+ }
+ }
return;
}
#endif
logger->log("Error, invalid embedded palette: %s", description.c_str());
}
+void DyePalette::hexToColor(const std::string &hexStr,
+ const int blockSize,
+ DyeColor &color)
+{
+ for (size_t i = 0, colorIdx = 0;
+ i < blockSize && colorIdx < 4;
+ i += 2, colorIdx ++)
+ {
+ color.value[colorIdx] = static_cast<unsigned char>((
+ hexDecode(hexStr[i]) << 4)
+ + hexDecode(hexStr[i + 1]));
+ }
+}
+
unsigned int DyePalette::hexDecode(const signed char c)
{
if ('0' <= c && c <= '9')
diff --git a/src/resources/dye/dyepalette.h b/src/resources/dye/dyepalette.h
index d27fba095..d405aaae1 100644
--- a/src/resources/dye/dyepalette.h
+++ b/src/resources/dye/dyepalette.h
@@ -85,6 +85,11 @@ class DyePalette final
static unsigned int hexDecode(const signed char c)
A_CONST A_WARN_UNUSED;
+
+ static void hexToColor(const std::string &hexStr,
+ const int blockSize,
+ DyeColor &color);
+
#ifndef UNITTESTS
private:
#endif
diff --git a/src/resources/dye/dyepalette_unittest.cc b/src/resources/dye/dyepalette_unittest.cc
index 348f9124b..e0a8599ce 100644
--- a/src/resources/dye/dyepalette_unittest.cc
+++ b/src/resources/dye/dyepalette_unittest.cc
@@ -140,7 +140,7 @@ TEST_CASE("DyePalette tests")
REQUIRE(palette.mColors[1].value[3] == 0x33);
}
- SECTION("palette test 1")
+ SECTION("palette test 8")
{
DyePalette palette("@Untitled1", 6);
REQUIRE(palette.mColors.size() == 1);
@@ -150,7 +150,7 @@ TEST_CASE("DyePalette tests")
REQUIRE(palette.mColors[0].value[3] == 255);
}
- SECTION("palette test 2")
+ SECTION("palette test 9")
{
DyePalette palette("@Untitled1,Untitled8", 6);
REQUIRE(palette.mColors.size() == 2);
@@ -165,7 +165,7 @@ TEST_CASE("DyePalette tests")
REQUIRE(palette.mColors[1].value[3] == 255);
}
- SECTION("palette test 3")
+ SECTION("palette test 10")
{
DyePalette palette("@Untitled1,", 6);
REQUIRE(palette.mColors.size() == 1);
@@ -175,7 +175,7 @@ TEST_CASE("DyePalette tests")
REQUIRE(palette.mColors[0].value[3] == 255);
}
- SECTION("palette test 4")
+ SECTION("palette test 11")
{
DyePalette palette("@,,,Untitled1,,Untitled8", 6);
REQUIRE(palette.mColors.size() == 2);
@@ -189,4 +189,119 @@ TEST_CASE("DyePalette tests")
REQUIRE(palette.mColors[1].value[2] == 255);
REQUIRE(palette.mColors[1].value[3] == 255);
}
+
+ SECTION("palette test 12")
+ {
+ DyePalette palette("@12ff34", 6);
+ REQUIRE(palette.mColors.size() == 1);
+ REQUIRE(palette.mColors[0].value[0] == 0x12);
+ REQUIRE(palette.mColors[0].value[1] == 0xff);
+ REQUIRE(palette.mColors[0].value[2] == 0x34);
+ REQUIRE(palette.mColors[0].value[3] == 0x00);
+ }
+
+ SECTION("palette test 13")
+ {
+ DyePalette palette("@12ff3456", 8);
+ REQUIRE(palette.mColors.size() == 1);
+ REQUIRE(palette.mColors[0].value[0] == 0x12);
+ REQUIRE(palette.mColors[0].value[1] == 0xff);
+ REQUIRE(palette.mColors[0].value[2] == 0x34);
+ REQUIRE(palette.mColors[0].value[3] == 0x56);
+ }
+
+ SECTION("palette test 14")
+ {
+ DyePalette palette("@12ff34,002211", 6);
+ REQUIRE(palette.mColors.size() == 2);
+ REQUIRE(palette.mColors[0].value[0] == 0x12);
+ REQUIRE(palette.mColors[0].value[1] == 0xff);
+ REQUIRE(palette.mColors[0].value[2] == 0x34);
+ REQUIRE(palette.mColors[0].value[3] == 0x00);
+
+ REQUIRE(palette.mColors[1].value[0] == 0x00);
+ REQUIRE(palette.mColors[1].value[1] == 0x22);
+ REQUIRE(palette.mColors[1].value[2] == 0x11);
+ REQUIRE(palette.mColors[1].value[3] == 0x00);
+ }
+
+ SECTION("palette test 15")
+ {
+ DyePalette palette("@12ff3412,00221133", 8);
+ REQUIRE(palette.mColors.size() == 2);
+ REQUIRE(palette.mColors[0].value[0] == 0x12);
+ REQUIRE(palette.mColors[0].value[1] == 0xff);
+ REQUIRE(palette.mColors[0].value[2] == 0x34);
+ REQUIRE(palette.mColors[0].value[3] == 0x12);
+
+ REQUIRE(palette.mColors[1].value[0] == 0x00);
+ REQUIRE(palette.mColors[1].value[1] == 0x22);
+ REQUIRE(palette.mColors[1].value[2] == 0x11);
+ REQUIRE(palette.mColors[1].value[3] == 0x33);
+ }
+
+ SECTION("palette test 16")
+ {
+ DyePalette palette("@12ff34,", 6);
+ REQUIRE(palette.mColors.size() == 1);
+ REQUIRE(palette.mColors[0].value[0] == 0x12);
+ REQUIRE(palette.mColors[0].value[1] == 0xff);
+ REQUIRE(palette.mColors[0].value[2] == 0x34);
+ REQUIRE(palette.mColors[0].value[3] == 0x00);
+ }
+
+ SECTION("palette test 17")
+ {
+ DyePalette palette("@12ff3456,", 8);
+ REQUIRE(palette.mColors.size() == 1);
+ REQUIRE(palette.mColors[0].value[0] == 0x12);
+ REQUIRE(palette.mColors[0].value[1] == 0xff);
+ REQUIRE(palette.mColors[0].value[2] == 0x34);
+ REQUIRE(palette.mColors[0].value[3] == 0x56);
+ }
+
+ SECTION("palette test 18")
+ {
+ DyePalette palette("@,,,12ff3412,,00221133", 8);
+ REQUIRE(palette.mColors.size() == 2);
+ REQUIRE(palette.mColors[0].value[0] == 0x12);
+ REQUIRE(palette.mColors[0].value[1] == 0xff);
+ REQUIRE(palette.mColors[0].value[2] == 0x34);
+ REQUIRE(palette.mColors[0].value[3] == 0x12);
+
+ REQUIRE(palette.mColors[1].value[0] == 0x00);
+ REQUIRE(palette.mColors[1].value[1] == 0x22);
+ REQUIRE(palette.mColors[1].value[2] == 0x11);
+ REQUIRE(palette.mColors[1].value[3] == 0x33);
+ }
+
+ SECTION("palette test 19")
+ {
+ DyePalette palette("@Untitled1,334455", 6);
+ REQUIRE(palette.mColors.size() == 2);
+ REQUIRE(palette.mColors[0].value[0] == 47);
+ REQUIRE(palette.mColors[0].value[1] == 56);
+ REQUIRE(palette.mColors[0].value[2] == 46);
+ REQUIRE(palette.mColors[0].value[3] == 255);
+
+ REQUIRE(palette.mColors[1].value[0] == 0x33);
+ REQUIRE(palette.mColors[1].value[1] == 0x44);
+ REQUIRE(palette.mColors[1].value[2] == 0x55);
+ REQUIRE(palette.mColors[1].value[3] == 0x00);
+ }
+
+ SECTION("palette test 20")
+ {
+ DyePalette palette("@Untitled1,33445566", 8);
+ REQUIRE(palette.mColors.size() == 2);
+ REQUIRE(palette.mColors[0].value[0] == 47);
+ REQUIRE(palette.mColors[0].value[1] == 56);
+ REQUIRE(palette.mColors[0].value[2] == 46);
+ REQUIRE(palette.mColors[0].value[3] == 255);
+
+ REQUIRE(palette.mColors[1].value[0] == 0x33);
+ REQUIRE(palette.mColors[1].value[1] == 0x44);
+ REQUIRE(palette.mColors[1].value[2] == 0x55);
+ REQUIRE(palette.mColors[1].value[3] == 0x66);
+ }
}