Subversion Repositories tpanel

Rev

Rev 446 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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