ManaPlus
palettedb.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2013-2019 The ManaPlus Developers
4  *
5  * This file is part of The ManaPlus Client.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "resources/db/palettedb.h"
22 
23 #include "configuration.h"
24 
25 #include "fs/virtfs/tools.h"
26 
27 #include "utils/checkutils.h"
28 
29 #include "resources/dye/dyecolor.h"
30 
31 #include "debug.h"
32 
33 namespace
34 {
35  bool mLoaded = false;
36  std::map<std::string, DyeColor> mColors;
37 } // namespace
38 
40 {
41  if (mLoaded)
42  unload();
43 
44  logger->log1("Initializing palette database...");
45  loadPalette();
46 }
47 
49 {
50  mLoaded = true;
51  StringVect lines;
53  paths.getStringValue("defaultPaletteFile"),
54  lines);
55  StringVectCIter it = lines.begin();
56  if (it == lines.end())
57  {
58  logger->log("missing GIMP palette file");
59  return;
60  }
61  if (*it != "GIMP Palette")
62  {
63  reportAlways("wrong GIMP palette file")
64  return;
65  }
66  ++ it;
67  // skip header
68  while (it != lines.end())
69  {
70  const std::string line = *it;
71  if (!line.empty() && line[0] == '#')
72  break;
73  ++ it;
74  }
75 
76  char name[101];
77 
78  // process colors and ignore commets
79  while (it != lines.end())
80  {
81  const std::string line = *it;
82  ++ it;
83 
84  if (line.empty() || line[0] == '#')
85  continue;
86 
87  unsigned int r;
88  unsigned int g;
89  unsigned int b;
90 
91  if (sscanf(line.c_str(), "%10u %10u %10u\t%100s",
92  &r, &g, &b, name) == 4)
93  {
94  name[100] = 0;
95  mColors[name] = DyeColor(CAST_U8(r),
96  CAST_U8(g),
97  CAST_U8(b));
98  }
99  }
100 }
101 
103 {
104  logger->log1("Unloading palette database...");
105  mColors.clear();
106 }
107 
108 const DyeColor *PaletteDB::getColor(const std::string &name)
109 {
110  const std::map<std::string, DyeColor>::const_iterator it =
111  mColors.find(name);
112  if (it != mColors.end())
113  return &(*it).second;
114  return nullptr;
115 }
reportAlways
#define reportAlways(...)
Definition: checkutils.h:252
Net::unload
void unload()
Definition: net.cpp:179
anonymous_namespace{palettedb.cpp}::mColors
std::map< std::string, DyeColor > mColors
Definition: palettedb.cpp:36
paths
Configuration paths
Definition: configuration.cpp:55
palettedb.h
StringVect
std::vector< std::string > StringVect
Definition: stringvector.h:28
PaletteDB::loadPalette
void loadPalette()
Definition: palettedb.cpp:48
PaletteDB::unload
void unload()
Definition: palettedb.cpp:102
logger
Logger * logger
Definition: logger.cpp:88
CAST_U8
#define CAST_U8
Definition: cast.h:26
checkutils.h
VirtFs::loadTextFile
bool loadTextFile(const std::string &fileName, StringVect &lines)
Definition: tools.cpp:136
Configuration::getStringValue
std::string getStringValue(const std::string &key) const
Definition: configuration.cpp:487
PaletteDB::load
void load()
Definition: palettedb.cpp:39
dyecolor.h
DyeColor
Definition: dyecolor.h:28
Logger::log1
void log1(const char *const log_text)
Definition: logger.cpp:237
configuration.h
StringVectCIter
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:30
PaletteDB::getColor
const DyeColor * getColor(const std::string &name)
Definition: palettedb.cpp:108
anonymous_namespace{palettedb.cpp}::mLoaded
bool mLoaded
Definition: palettedb.cpp:35
tools.h
Logger::log
void log(const char *const log_text,...)
Definition: logger.cpp:268
debug.h