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
 
5 andreas 19
#include <iostream>
20
#include <iomanip>
21
 
4 andreas 22
#include "tcolor.h"
23
#include "terror.h"
24
 
25
TPalette *TColor::mPalette = nullptr;
26
 
27
using std::string;
5 andreas 28
using std::setw;
29
using std::setfill;
30
using std::hex;
31
using std::dec;
32
using std::resetiosflags;
4 andreas 33
 
34
TColor::COLOR_T TColor::getAMXColor(const string& color)
35
{
36
    DECL_TRACER("TColor::getAMXColor(const string& color)");
37
 
38
    if (color.empty())
39
        return TColor::COLOR_T();
40
 
41
    size_t pos = color.find('#');
42
 
43
    if (pos == string::npos)
44
    {
45
        if (!mPalette)
46
        {
47
            MSG_ERROR("No palette was set! First set a palette to be able to get any color!");
48
            TError::setError();
49
            return TColor::COLOR_T();
50
        }
51
 
52
        PDATA_T pd = mPalette->findColor(color);
53
 
54
        if (!pd.name.empty())
5 andreas 55
        {
56
            MSG_DEBUG("Found color " << color << ": #" << setw(8) << setfill('0') << hex << pd.color);
4 andreas 57
            return splitColors(pd);
5 andreas 58
        }
4 andreas 59
 
5 andreas 60
        MSG_DEBUG("Color " << color << " not found!");
4 andreas 61
        return TColor::COLOR_T();
62
    }
63
 
64
    TColor::COLOR_T ct;
65
    ct.red = (int)strtol(color.substr(pos+1, 2).c_str(), NULL, 16);
66
    ct.green = (int)strtol(color.substr(pos+3, 2).c_str(), NULL, 16);
67
    ct.blue = (int)strtol(color.substr(pos+5, 2).c_str(), NULL, 16);
68
 
69
    if (color.length() > 7)
70
        ct.alpha = (int)strtol(color.substr(pos+7).c_str(), NULL, 16);
71
    else
72
        ct.alpha = 0x00ff;
73
 
5 andreas 74
    MSG_DEBUG("Splitted value " << color << " into "
75
                << setw(2) << setfill('0') << hex << ct.red << ":"
76
                << setw(2) << setfill('0') << hex << ct.green  << ":"
77
                << setw(2) << setfill('0') << hex << ct.blue  << ":"
78
                << setw(2) << setfill('0') << hex << ct.alpha);
79
 
4 andreas 80
    return ct;
81
}
82
 
83
TColor::COLOR_T TColor::splitColors(PDATA_T& pd)
84
{
85
    DECL_TRACER("TColor::splitColors(PDATA_T& pd)");
86
 
87
    TColor::COLOR_T ct;
88
 
89
    if (pd.color > 0x00ffffff)
90
    {
91
        ct.red = (pd.color & 0xff000000) >> 24;
92
        ct.green = (pd.color & 0x00ff0000) >> 16;
93
        ct.blue = (pd.color & 0x0000ff00) >> 8;
94
        ct.alpha = (pd.color & 0x000000ff);
95
    }
96
    else
97
    {
98
        ct.red = (pd.color & 0xff000000) >> 24;
99
        ct.green = (pd.color & 0x00ff0000) >> 16;
100
        ct.blue = (pd.color & 0x0000ff00) >> 8;
101
        ct.alpha = 0x00ff;
102
    }
103
 
104
    return ct;
105
}
106
 
107
SkColor TColor::getSkiaColor(const std::string& color)
108
{
109
    DECL_TRACER("TColor::getSkiaColor(const std::string& color)");
110
 
111
    COLOR_T col = getAMXColor(color);
112
    return SkColorSetARGB(col.alpha, col.red, col.green, col.blue);
113
}
5 andreas 114
 
115
ulong TColor::getColor(const std::string& color)
116
{
117
    DECL_TRACER("TColor::getColor(const std::string& color)");
118
 
119
    TColor::COLOR_T ct = getAMXColor(color);
120
    ulong col = ((ct.red << 24) & 0xff000000) | ((ct.green << 16) & 0x00ff0000) | ((ct.blue << 8) & 0x0000ff00) | (ct.alpha & 0x000000ff);
121
    return col;
122
}
7 andreas 123
 
124
std::string TColor::colorToString(ulong color)
125
{
126
    PDATA_T pd;
127
    pd.color = color;
128
    TColor::COLOR_T col = splitColors(pd);
129
    std::stringstream s;
130
    s << std::setw(2) << std::setfill('0') << std::hex << col.red << ":";
131
    s << std::setw(2) << std::setfill('0') << std::hex << col.green << ":";
132
    s << std::setw(2) << std::setfill('0') << std::hex << col.blue << ":";
133
    s << std::setw(2) << std::setfill('0') << std::hex << col.alpha;
134
    std::string ret = s.str();
135
    return ret;
136
}
137
 
138
std::string TColor::colorToString(SkColor color)
139
{
140
    std::stringstream s;
141
    s << std::setw(2) << std::setfill('0') << std::hex << SkColorGetR(color) << ":";
142
    s << std::setw(2) << std::setfill('0') << std::hex << SkColorGetG(color) << ":";
143
    s << std::setw(2) << std::setfill('0') << std::hex << SkColorGetB(color) << ":";
144
    s << std::setw(2) << std::setfill('0') << std::hex << SkColorGetA(color);
145
    std::string ret = s.str();
146
    return ret;
147
}
8 andreas 148
 
149
int TColor::setAlpha(ulong color, int alpha)
150
{
151
    DECL_TRACER("TColor::setAlpha(ulong color, int alpha)");
152
 
153
    if (alpha > 0x00ff)
154
        alpha = 0x00ff;
155
    else if (alpha < 0)
156
        alpha = 0;
157
 
158
    return ((color & 0xffffff00) | (alpha & 0x00ff));
159
}
160
 
161
SkColor TColor::setAlpha(SkColor color, int alpha)
162
{
163
    DECL_TRACER("TColor::setAlpha(SkColor color, int alpha)");
164
 
165
    if (alpha > 0x00ff)
166
        alpha = 0x00ff;
167
    else if (alpha < 0)
168
        alpha = 0;
169
 
170
    SkColor red = SkColorGetR(color);
171
    SkColor green = SkColorGetG(color);
172
    SkColor blue = SkColorGetB(color);
173
    return SkColorSetARGB(alpha, red, green, blue);
174
}
175
 
176
int TColor::calcAlpha(int alpha1, int alpha2)
177
{
178
    DECL_TRACER("TColor::calcAlpha(int alpha1, int alpha2)");
179
 
180
    return (alpha1 + alpha2) / 2;
181
}
182
 
183
ulong TColor::setAlphaTreshold(ulong color, int alpha)
184
{
185
    DECL_TRACER("TColor::setAlphaTreshold(ulong color, int alpha)");
186
 
187
    uint al = color & 0x000000ff;
188
 
189
    if (al > (uint)alpha)
190
        return (color & 0xffffff00) | (alpha & 0x00ff);
191
 
192
    return color;
193
}
194
 
195
SkColor TColor::setAlphaTreshold(SkColor color, int alpha)
196
{
197
    DECL_TRACER("TColor::setAlphaTreshold(SkColor color, int alpha)");
198
 
199
    int al = SkColorGetA(color);
200
 
201
    if (al > alpha)
202
        return SkColorSetA(color, alpha);
203
 
204
    return color;
205
}