From 52f78717dae3cb02eb1539841dd2a6bd351f74c1 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 20 Apr 2013 23:00:42 +0300 Subject: add support for GIMP palettes for dye colors. Palette file must be named palette.gpl and contain colors with correct names. To use it in dye string can be used character @. Example: in all dye string it not tested. --- src/resources/dye.cpp | 100 +++++++++++++++++++++++------------------ src/resources/dye.h | 9 ++-- src/resources/palettedb.cpp | 107 ++++++++++++++++++++++++++++++++++++++++++++ src/resources/palettedb.h | 39 ++++++++++++++++ 4 files changed, 205 insertions(+), 50 deletions(-) create mode 100644 src/resources/palettedb.cpp create mode 100644 src/resources/palettedb.h (limited to 'src/resources') diff --git a/src/resources/dye.cpp b/src/resources/dye.cpp index 960ac36ae..9997fc6ae 100644 --- a/src/resources/dye.cpp +++ b/src/resources/dye.cpp @@ -24,6 +24,8 @@ #include "logger.h" +#include "resources/palettedb.h" + #include #include @@ -33,42 +35,52 @@ DyePalette::DyePalette(const std::string &description, const int8_t blockSize) : mColors() { - const int size = static_cast(description.length()); + const size_t size = static_cast(description.length()); if (size == 0) return; - if (description[0] != '#') - { - // TODO: load palette from file. - return; - } - int pos = 1; - for ( ; ; ) + if (description[0] == '#') { - if (pos + blockSize > size) - break; - - Color color = + size_t pos = 1; + for ( ; ; ) { - {0, 0, 0, 0} - }; + if (pos + blockSize > size) + break; - for (int 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])); - } - mColors.push_back(color); - pos += blockSize; + DyeColor color(0, 0, 0, 0); - if (pos == size) - return; - if (description[pos] != ',') - break; + for (int 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])); + } + mColors.push_back(color); + pos += blockSize; - ++pos; + if (pos == size) + return; + if (description[pos] != ',') + break; + + ++pos; + } + } + 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; + } } logger->log("Error, invalid embedded palette: %s", description.c_str()); @@ -185,15 +197,15 @@ void DyePalette::getColor(double intensity, int color[3]) const void DyePalette::replaceSColor(uint8_t *const color) const { - std::vector::const_iterator it = mColors.begin(); - const std::vector::const_iterator it_end = mColors.end(); + std::vector::const_iterator it = mColors.begin(); + const std::vector::const_iterator it_end = mColors.end(); while (it != it_end) { - const Color &col = *it; + const DyeColor &col = *it; ++ it; if (it == it_end) return; - const Color &col2 = *it; + const DyeColor &col2 = *it; if (color[0] == col.value[0] && color[1] == col.value[1] && color[2] == col.value[2]) { @@ -208,15 +220,15 @@ void DyePalette::replaceSColor(uint8_t *const color) const void DyePalette::replaceAColor(uint8_t *const color) const { - std::vector::const_iterator it = mColors.begin(); - const std::vector::const_iterator it_end = mColors.end(); + std::vector::const_iterator it = mColors.begin(); + const std::vector::const_iterator it_end = mColors.end(); while (it != it_end) { - const Color &col = *it; + const DyeColor &col = *it; ++ it; if (it == it_end) return; - const Color &col2 = *it; + const DyeColor &col2 = *it; if (color[1] == col.value[0] && color[2] == col.value[1] && color[3] == col.value[2] && color[0] == col.value[3]) { @@ -232,15 +244,15 @@ void DyePalette::replaceAColor(uint8_t *const color) const void DyePalette::replaceSOGLColor(uint8_t *const color) const { - std::vector::const_iterator it = mColors.begin(); - const std::vector::const_iterator it_end = mColors.end(); + std::vector::const_iterator it = mColors.begin(); + const std::vector::const_iterator it_end = mColors.end(); while (it != it_end) { - const Color &col = *it; + const DyeColor &col = *it; ++ it; if (it == it_end) return; - const Color &col2 = *it; + const DyeColor &col2 = *it; if (color[2] == col.value[0] && color[1] == col.value[1] && color[0] == col.value[2]) { @@ -255,15 +267,15 @@ void DyePalette::replaceSOGLColor(uint8_t *const color) const void DyePalette::replaceAOGLColor(uint8_t *const color) const { - std::vector::const_iterator it = mColors.begin(); - const std::vector::const_iterator it_end = mColors.end(); + std::vector::const_iterator it = mColors.begin(); + const std::vector::const_iterator it_end = mColors.end(); while (it != it_end) { - const Color &col = *it; + const DyeColor &col = *it; ++ it; if (it == it_end) return; - const Color &col2 = *it; + const DyeColor &col2 = *it; if (color[2] == col.value[0] && color[1] == col.value[1] && color[0] == col.value[2] && color[3] == col.value[3]) { diff --git a/src/resources/dye.h b/src/resources/dye.h index 442a514d7..efcf95420 100644 --- a/src/resources/dye.h +++ b/src/resources/dye.h @@ -23,11 +23,11 @@ #ifndef DYE_H #define DYE_H +#include "resources/dyecolor.h" + #include #include -#include - #include "localconsts.h" const int dyePalateSize = 9; @@ -83,10 +83,7 @@ class DyePalette final static int hexDecode(const signed char c) A_WARN_UNUSED; private: - struct Color - { unsigned char value[4]; }; - - std::vector mColors; + std::vector mColors; }; /** diff --git a/src/resources/palettedb.cpp b/src/resources/palettedb.cpp new file mode 100644 index 000000000..c770a9cdc --- /dev/null +++ b/src/resources/palettedb.cpp @@ -0,0 +1,107 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "resources/palettedb.h" + +#include "logger.h" + +#include "resources/dyecolor.h" +#include "resources/resourcemanager.h" + +#include "utils/stringvector.h" + +#include + +#include "debug.h" + +namespace +{ + bool mLoaded = false; + std::map mColors; + DyeColor mEmpty(0, 0, 0, 0); +} + +void PaletteDB::load() +{ + if (mLoaded) + unload(); + + loadPalette(); +} + +void PaletteDB::loadPalette() +{ + mLoaded = true; + StringVect lines; + ResourceManager::loadTextFile("palette.gpl", lines); + StringVectCIter it = lines.begin(); + if (it == lines.end()) + { + logger->log("missing GIMP palette file"); + return; + } + if (*it != "GIMP Palette") + { + logger->log("wrong GIMP palette file"); + return; + } + ++ it; + // skip header + while (it != lines.end()) + { + const std::string line = *it; + if (!line.empty() && line[0] == '#') + break; + ++ it; + } + + char name[101]; + + // process colors and ignore commets + while (it != lines.end()) + { + const std::string line = *it; + ++ it; + + if (line.empty() || line[0] == '#') + continue; + + unsigned int r; + unsigned int g; + unsigned int b; + + if (sscanf(line.c_str(), "%u %u %u\t%s", &r, &g, &b, name) == 4) + mColors[name] = DyeColor(r, g, b); + } +} + +void PaletteDB::unload() +{ + mColors.clear(); +} + +const DyeColor &PaletteDB::getColor(const std::string &name) +{ + std::map::const_iterator it = mColors.find(name); + if (it != mColors.end()) + return (*it).second; + else + return mEmpty; +} diff --git a/src/resources/palettedb.h b/src/resources/palettedb.h new file mode 100644 index 000000000..28b901799 --- /dev/null +++ b/src/resources/palettedb.h @@ -0,0 +1,39 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef PALETTEDB_H +#define PALETTEDB_H + +#include "localconsts.h" + +#include + +struct DyeColor; + +namespace PaletteDB +{ + void load(); + void unload(); + void loadPalette(); + const DyeColor &getColor(const std::string &name); + +} // namespace PaletteDB + +#endif -- cgit v1.2.3-70-g09d2