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