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;
4 andreas 104
 
77 andreas 105
    while ((index = xml.getNextElementFromIndex(index, &name, &content, &attrs)) != TExpat::npos)
4 andreas 106
    {
107
        PDATA_T pal;
108
 
77 andreas 109
        if (name.compare("color") != 0)
4 andreas 110
        {
111
            pal.clear();
112
            continue;
113
        }
114
 
77 andreas 115
        pal.index = xml.getAttributeInt("index", attrs);
116
        pal.name = xml.getAttribute("name", attrs);
117
        string color = content;
4 andreas 118
 
119
        if (color.at(0) == '#')     // Do we have a valid color value?
120
        {
121
            string sCol = "0x" + color.substr(1);
122
            pal.color = strtoul(sCol.c_str(), 0, 16);
123
        }
124
 
137 andreas 125
        if (pal.name.length() > 0)
4 andreas 126
        {
137 andreas 127
            if (mColors.find(pal.name) != mColors.end())    // Don't insert color if it's already in list
128
            {
129
                MSG_TRACE("Ignoring color " << pal.name << " because it was read before!");
130
                pal.clear();
131
                continue;
132
            }
133
 
134
            // Insert color into list and get next child if there is one.
135
            mColors.insert(pair<string, PDATA_T>(pal.name, pal));
4 andreas 136
        }
137
 
138
        pal.clear();
139
    }
137 andreas 140
 
141
    if (mColors.empty())
142
        addSystemColors();
143
 
144
    mPaletteNames.push_back(palName);
4 andreas 145
}
146
 
11 andreas 147
void TPalette::reset()
148
{
149
    DECL_TRACER("TPalette::reset()");
150
 
151
    mColors.clear();
152
    mPaletteNames.clear();
153
}
154
 
4 andreas 155
PDATA_T TPalette::findColor(const std::string& name)
156
{
157
    DECL_TRACER("TPalette::findColor(const std::string& name)");
158
 
137 andreas 159
    if (mColors.empty())
160
    {
161
        MSG_WARNING("Have no colors in internal table!");
162
        return PDATA_T();
163
    }
164
 
4 andreas 165
    map<string, PDATA_T>::iterator iter;
166
 
167
    if ((iter = mColors.find(name)) == mColors.end())
168
        return PDATA_T();
169
 
170
    return iter->second;
171
}
172
 
173
PDATA_T TPalette::findColor(int pID)
174
{
175
    DECL_TRACER("TPalette::findColor(int pID)");
176
 
177
    map<string, PDATA_T>::iterator iter;
178
 
179
    for (iter = mColors.begin(); iter != mColors.end(); iter++)
180
    {
181
        if (iter->second.index == pID)
182
            return iter->second;
183
    }
184
 
185
    return PDATA_T();
186
}
187
 
188
bool TPalette::havePalette(const std::string& name)
189
{
190
    DECL_TRACER("TPalette::havePalette(const std::string& name)");
191
 
192
    vector<string>::iterator iter;
193
 
194
    for (iter = mPaletteNames.begin(); iter != mPaletteNames.end(); iter++)
195
    {
196
        if (iter->compare(name) == 0)
197
            return true;
198
    }
199
 
200
    return false;
201
}
137 andreas 202
 
203
void TPalette::addSystemColors()
204
{
205
    DECL_TRACER("TPalette::addSystemColors()");
206
 
207
    vector<PDATA_T>::iterator iter;
208
    vector<PDATA_T> palArr = {
209
        {  0, "VeryLightRed",    0xff0000ff },
210
        {  1, "LightRed",        0xdf0000ff },
211
        {  2, "Red",             0xbf0000ff },
212
        {  3, "MediumRed",       0x9f0000ff },
213
        {  4, "DarkRed",         0x7f0000ff },
214
        {  5, "VeryDarkRed",     0x5f0000ff },
215
        {  6, "VeryLightOrange", 0xff8000ff },
216
        {  7, "LightOrange",     0xdf7000ff },
217
        {  8, "Orange",          0xbf6000ff },
218
        {  9, "MediumOrange",    0x9f5000ff },
219
        { 10, "DarkOrange",      0x7f4000ff },
220
        { 11, "VeryDarkOrange",  0x5f3000ff },
221
        { 12, "VeryLightYellow", 0xffff00ff },
222
        { 13, "LightYellow",     0xdfdf00ff },
223
        { 14, "Yellow",          0xbfbf00ff },
224
        { 15, "MediumYellow",    0x9f9f00ff },
225
        { 16, "DarkYellow",      0x7f7f00ff },
226
        { 17, "VeryDarkYellow",  0x5f5f00ff },
227
        { 18, "VeryLightLime",   0x80ff00ff },
228
        { 19, "LightLime",       0x70df00ff },
229
        { 20, "Lime",            0x60bf00ff },
230
        { 21, "MediumLime",      0x509f00ff },
231
        { 22, "DarkLime",        0x407f00ff },
232
        { 23, "VeryDarkLime",    0x304f00ff },
233
        { 24, "VeryLightGreen",  0x00ff00ff },
234
        { 25, "LightGreen",      0x00df00ff },
235
        { 26, "Green",           0x00bf00ff },
236
        { 27, "MediumGreen",     0x009f00ff },
237
        { 28, "DarkGreen",       0x007f00ff },
238
        { 29, "VeryDarkGreen",   0x005f00ff },
239
        { 30, "VeryLightMint",   0x00ff80ff },
240
        { 31, "LightMint",       0x00df70ff },
241
        { 32, "Mint",            0x00bf60ff },
242
        { 33, "MediumMint",      0x009f50ff },
243
        { 34, "DarkMint",        0x007f40ff },
244
        { 35, "VeryDarkMint",    0x005f10ff },
245
        { 36, "VeryLightCyan",   0x00ffffff },
246
        { 37, "LightCyan",       0x00dfdfff },
247
        { 38, "Cyan",            0x00bfbfff },
248
        { 39, "MediumCyan",      0x009f9fff },
249
        { 40, "DarkCyan",        0x007f7fff },
250
        { 41, "VeryDarkCyan",    0x005f5fff },
251
        { 42, "VeryLightAqua",   0x0080ffff },
252
        { 43, "LightAqua",       0x0070dfff },
253
        { 44, "Aqua",            0x0060bfff },
254
        { 45, "MediumAqua",      0x00509fff },
255
        { 46, "DarkAqua",        0x00407fff },
256
        { 47, "VeryDarkAqua",    0x00305fff },
257
        { 48, "VeryLightBlue",   0x0000ffff },
258
        { 49, "LightBlue",       0x0000dfff },
259
        { 50, "Blue",            0x0000bfff },
260
        { 51, "MediumBlue",      0x00009fff },
261
        { 52, "DarkBlue",        0x00007fff },
262
        { 53, "VeryDarkBlue",    0x00005fff },
263
        { 54, "VeryLightPurple", 0x8000ffff },
264
        { 55, "LightPurple",     0x7000dfff },
265
        { 56, "Purple",          0x6000bfff },
266
        { 57, "MediumPurple",    0x50009fff },
267
        { 58, "DarkPurple",      0x40007fff },
268
        { 59, "VeryDarkPurple",  0x30005fff },
269
        { 60, "VeryLightMagenta",0xff00ffff },
270
        { 61, "LightMagenta",    0xdf00dfff },
271
        { 62, "Magenta",         0xbf00bfff },
272
        { 63, "MediumMagenta",   0x9f009fff },
273
        { 64, "DarkMagenta",     0x7f007fff },
274
        { 65, "VeryDarkMagenta", 0x5f005fff },
275
        { 66, "VeryLightPink",   0xff0080ff },
276
        { 67, "LightPink",       0xdf0070ff },
277
        { 68, "Pink",            0xbf0060ff },
278
        { 69, "MediumPink",      0x9f0050ff },
279
        { 70, "DarkPink",        0x7f0040ff },
280
        { 71, "VeryDarkPink",    0x5f0030ff },
281
        { 72, "White",           0xffffffff },
282
        { 73, "Grey1",           0xeeeeeeff },
283
        { 74, "Grey3",           0xccccccff },
284
        { 75, "Grey5",           0xaaaaaaff },
285
        { 76, "Grey7",           0x888888ff },
286
        { 77, "Grey9",           0x666666ff },
287
        { 78, "Grey4",           0xbbbbbbff },
288
        { 79, "Grey6",           0x999999ff },
289
        { 80, "Grey8",           0x777777ff },
290
        { 81, "Grey10",          0x555555ff },
291
        { 82, "Grey12",          0x333333ff },
292
        { 83, "Grey13",          0x222222ff },
293
        { 84, "Grey2",           0xddddddff },
294
        { 85, "Grey11",          0x444444ff },
295
        { 86, "Grey14",          0x111111ff },
296
        { 87, "Black",           0x000000ff },
297
        { 255, "Transparent",    0x63356300 }
298
    };
299
 
300
    for (iter = palArr.begin(); iter != palArr.end(); ++iter)
301
        mColors.insert(pair<string, PDATA_T>(iter->name, *iter));
302
}