Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 andreas 1
/*
21 andreas 2
 * Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
4 andreas 3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include "tpalette.h"
20
#include "terror.h"
21
#include "tconfig.h"
137 andreas 22
#include "tresources.h"
77 andreas 23
#include "texpat++.h"
4 andreas 24
 
186 andreas 25
#if __cplusplus < 201402L
26
#   error "This module requires at least C++14 standard!"
27
#else
28
#   if __cplusplus < 201703L
29
#       include <experimental/filesystem>
30
namespace fs = std::experimental::filesystem;
31
#       warning "Support for C++14 and experimental filesystem will be removed in a future version!"
32
#   else
33
#       include <filesystem>
34
#       ifdef __ANDROID__
35
namespace fs = std::__fs::filesystem;
36
#       else
37
namespace fs = std::filesystem;
38
#       endif
39
#   endif
40
#endif
41
 
4 andreas 42
using std::string;
43
using std::vector;
44
using std::map;
45
using std::pair;
77 andreas 46
using namespace Expat;
4 andreas 47
 
48
TPalette::TPalette()
49
{
50
    DECL_TRACER("TPalette::TPalette()");
186 andreas 51
 
52
    mPath = TConfig::getProjectPath();
4 andreas 53
}
54
 
55
TPalette::TPalette(const std::string& file)
56
{
57
    DECL_TRACER("TPalette::TPalette(const std::string& file)");
186 andreas 58
 
59
    mPath = TConfig::getProjectPath();
4 andreas 60
    initialize(file);
61
}
62
 
63
TPalette::~TPalette()
64
{
65
    DECL_TRACER("TPalette::~TPalette()");
66
}
67
 
68
void TPalette::initialize(const std::string& file)
69
{
70
    DECL_TRACER("TPalette::initialize(const std::string& file)");
71
 
186 andreas 72
    if (!fs::exists(mPath + "/prj.xma"))
73
        mPath += "/__system";
74
 
75
    makeFileName(mPath, file);
4 andreas 76
    string path;
77
 
78
    if (isValidFile())
79
        path = getFileName();
80
 
77 andreas 81
    TExpat xml(path);
82
    xml.setEncoding(ENC_CP1250);
4 andreas 83
 
137 andreas 84
    if (!xml.parse(false))
4 andreas 85
        return;
86
 
77 andreas 87
    int depth = 0;
88
    size_t index = 0;
4 andreas 89
 
77 andreas 90
    if ((index = xml.getElementIndex("paletteData", &depth)) == TExpat::npos)
4 andreas 91
    {
77 andreas 92
        MSG_ERROR("Element \"paletteData\" was not found!");
4 andreas 93
        TError::setError();
94
        return;
95
    }
96
 
77 andreas 97
    vector<ATTRIBUTE_t> attrs = xml.getAttributes();
98
    string palName = xml.getAttribute("name", attrs);
4 andreas 99
 
100
    if (havePalette(palName))
101
        return;
102
 
77 andreas 103
    string name, content;
442 andreas 104
    PDATA_T pal;
4 andreas 105
 
77 andreas 106
    while ((index = xml.getNextElementFromIndex(index, &name, &content, &attrs)) != TExpat::npos)
4 andreas 107
    {
77 andreas 108
        if (name.compare("color") != 0)
4 andreas 109
        {
110
            pal.clear();
111
            continue;
112
        }
113
 
77 andreas 114
        pal.index = xml.getAttributeInt("index", attrs);
115
        pal.name = xml.getAttribute("name", attrs);
116
        string color = content;
4 andreas 117
 
118
        if (color.at(0) == '#')     // Do we have a valid color value?
119
        {
120
            string sCol = "0x" + color.substr(1);
121
            pal.color = strtoul(sCol.c_str(), 0, 16);
442 andreas 122
 
123
            if (color.length() <= 7)
124
                pal.color = (pal.color << 8) | 0x000000ff;
4 andreas 125
        }
126
 
137 andreas 127
        if (pal.name.length() > 0)
4 andreas 128
        {
137 andreas 129
            if (mColors.find(pal.name) != mColors.end())    // Don't insert color if it's already in list
130
            {
131
                MSG_TRACE("Ignoring color " << pal.name << " because it was read before!");
132
                pal.clear();
133
                continue;
134
            }
135
 
136
            // Insert color into list and get next child if there is one.
137
            mColors.insert(pair<string, PDATA_T>(pal.name, pal));
4 andreas 138
        }
139
 
140
        pal.clear();
141
    }
137 andreas 142
 
143
    if (mColors.empty())
144
        addSystemColors();
145
 
146
    mPaletteNames.push_back(palName);
4 andreas 147
}
148
 
11 andreas 149
void TPalette::reset()
150
{
151
    DECL_TRACER("TPalette::reset()");
152
 
153
    mColors.clear();
154
    mPaletteNames.clear();
155
}
156
 
4 andreas 157
PDATA_T TPalette::findColor(const std::string& name)
158
{
159
    DECL_TRACER("TPalette::findColor(const std::string& name)");
160
 
137 andreas 161
    if (mColors.empty())
162
    {
163
        MSG_WARNING("Have no colors in internal table!");
164
        return PDATA_T();
165
    }
166
 
4 andreas 167
    map<string, PDATA_T>::iterator iter;
168
 
169
    if ((iter = mColors.find(name)) == mColors.end())
170
        return PDATA_T();
171
 
172
    return iter->second;
173
}
174
 
175
PDATA_T TPalette::findColor(int pID)
176
{
177
    DECL_TRACER("TPalette::findColor(int pID)");
178
 
179
    map<string, PDATA_T>::iterator iter;
180
 
442 andreas 181
    for (iter = mColors.begin(); iter != mColors.end(); ++iter)
4 andreas 182
    {
183
        if (iter->second.index == pID)
184
            return iter->second;
185
    }
186
 
187
    return PDATA_T();
188
}
189
 
190
bool TPalette::havePalette(const std::string& name)
191
{
192
    DECL_TRACER("TPalette::havePalette(const std::string& name)");
193
 
194
    vector<string>::iterator iter;
195
 
196
    for (iter = mPaletteNames.begin(); iter != mPaletteNames.end(); iter++)
197
    {
198
        if (iter->compare(name) == 0)
199
            return true;
200
    }
201
 
202
    return false;
203
}
137 andreas 204
 
205
void TPalette::addSystemColors()
206
{
207
    DECL_TRACER("TPalette::addSystemColors()");
208
 
209
    vector<PDATA_T>::iterator iter;
210
    vector<PDATA_T> palArr = {
211
        {  0, "VeryLightRed",    0xff0000ff },
212
        {  1, "LightRed",        0xdf0000ff },
213
        {  2, "Red",             0xbf0000ff },
214
        {  3, "MediumRed",       0x9f0000ff },
215
        {  4, "DarkRed",         0x7f0000ff },
216
        {  5, "VeryDarkRed",     0x5f0000ff },
217
        {  6, "VeryLightOrange", 0xff8000ff },
218
        {  7, "LightOrange",     0xdf7000ff },
219
        {  8, "Orange",          0xbf6000ff },
220
        {  9, "MediumOrange",    0x9f5000ff },
221
        { 10, "DarkOrange",      0x7f4000ff },
222
        { 11, "VeryDarkOrange",  0x5f3000ff },
223
        { 12, "VeryLightYellow", 0xffff00ff },
224
        { 13, "LightYellow",     0xdfdf00ff },
225
        { 14, "Yellow",          0xbfbf00ff },
226
        { 15, "MediumYellow",    0x9f9f00ff },
227
        { 16, "DarkYellow",      0x7f7f00ff },
228
        { 17, "VeryDarkYellow",  0x5f5f00ff },
229
        { 18, "VeryLightLime",   0x80ff00ff },
230
        { 19, "LightLime",       0x70df00ff },
231
        { 20, "Lime",            0x60bf00ff },
232
        { 21, "MediumLime",      0x509f00ff },
233
        { 22, "DarkLime",        0x407f00ff },
234
        { 23, "VeryDarkLime",    0x304f00ff },
235
        { 24, "VeryLightGreen",  0x00ff00ff },
236
        { 25, "LightGreen",      0x00df00ff },
237
        { 26, "Green",           0x00bf00ff },
238
        { 27, "MediumGreen",     0x009f00ff },
239
        { 28, "DarkGreen",       0x007f00ff },
240
        { 29, "VeryDarkGreen",   0x005f00ff },
241
        { 30, "VeryLightMint",   0x00ff80ff },
242
        { 31, "LightMint",       0x00df70ff },
243
        { 32, "Mint",            0x00bf60ff },
244
        { 33, "MediumMint",      0x009f50ff },
245
        { 34, "DarkMint",        0x007f40ff },
246
        { 35, "VeryDarkMint",    0x005f10ff },
247
        { 36, "VeryLightCyan",   0x00ffffff },
248
        { 37, "LightCyan",       0x00dfdfff },
249
        { 38, "Cyan",            0x00bfbfff },
250
        { 39, "MediumCyan",      0x009f9fff },
251
        { 40, "DarkCyan",        0x007f7fff },
252
        { 41, "VeryDarkCyan",    0x005f5fff },
253
        { 42, "VeryLightAqua",   0x0080ffff },
254
        { 43, "LightAqua",       0x0070dfff },
255
        { 44, "Aqua",            0x0060bfff },
256
        { 45, "MediumAqua",      0x00509fff },
257
        { 46, "DarkAqua",        0x00407fff },
258
        { 47, "VeryDarkAqua",    0x00305fff },
259
        { 48, "VeryLightBlue",   0x0000ffff },
260
        { 49, "LightBlue",       0x0000dfff },
261
        { 50, "Blue",            0x0000bfff },
262
        { 51, "MediumBlue",      0x00009fff },
263
        { 52, "DarkBlue",        0x00007fff },
264
        { 53, "VeryDarkBlue",    0x00005fff },
265
        { 54, "VeryLightPurple", 0x8000ffff },
266
        { 55, "LightPurple",     0x7000dfff },
267
        { 56, "Purple",          0x6000bfff },
268
        { 57, "MediumPurple",    0x50009fff },
269
        { 58, "DarkPurple",      0x40007fff },
270
        { 59, "VeryDarkPurple",  0x30005fff },
271
        { 60, "VeryLightMagenta",0xff00ffff },
272
        { 61, "LightMagenta",    0xdf00dfff },
273
        { 62, "Magenta",         0xbf00bfff },
274
        { 63, "MediumMagenta",   0x9f009fff },
275
        { 64, "DarkMagenta",     0x7f007fff },
276
        { 65, "VeryDarkMagenta", 0x5f005fff },
277
        { 66, "VeryLightPink",   0xff0080ff },
278
        { 67, "LightPink",       0xdf0070ff },
279
        { 68, "Pink",            0xbf0060ff },
280
        { 69, "MediumPink",      0x9f0050ff },
281
        { 70, "DarkPink",        0x7f0040ff },
282
        { 71, "VeryDarkPink",    0x5f0030ff },
283
        { 72, "White",           0xffffffff },
284
        { 73, "Grey1",           0xeeeeeeff },
285
        { 74, "Grey3",           0xccccccff },
286
        { 75, "Grey5",           0xaaaaaaff },
287
        { 76, "Grey7",           0x888888ff },
288
        { 77, "Grey9",           0x666666ff },
289
        { 78, "Grey4",           0xbbbbbbff },
290
        { 79, "Grey6",           0x999999ff },
291
        { 80, "Grey8",           0x777777ff },
292
        { 81, "Grey10",          0x555555ff },
293
        { 82, "Grey12",          0x333333ff },
294
        { 83, "Grey13",          0x222222ff },
295
        { 84, "Grey2",           0xddddddff },
296
        { 85, "Grey11",          0x444444ff },
297
        { 86, "Grey14",          0x111111ff },
298
        { 87, "Black",           0x000000ff },
299
        { 255, "Transparent",    0x63356300 }
300
    };
301
 
302
    for (iter = palArr.begin(); iter != palArr.end(); ++iter)
303
        mColors.insert(pair<string, PDATA_T>(iter->name, *iter));
304
}