1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/*
This file is taken from kcolorspaces.cpp and kcolorutils.cpp from kdelibs
The code has been modified to work with TQColor (TQt3 &TQt4) and GdkColor
*/
/* This file is part of the KDE project
* Copyright (C) 2007 Matthew Woehlke <[email protected]>
* Copyright (C) 2007 Olaf Schmidt <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <math.h>
#ifdef __cplusplus
#if (0x039999 >= 0x040000)
#define FLOAT_COLOR(VAL, COL) (VAL).COL##F()
#define TO_COLOR(R, G, B) TQColor::fromRgbF(R, G, B)
#else
#define FLOAT_COLOR(VAL, COL) ((double)(((VAL).COL()*1.0)/255.0))
#define TO_COLOR(R, G, B) TQColor(limit(R*255.0), limit(G*255.0), limit(B*255.0))
#endif
#else
#define inline
#define FLOAT_COLOR(VAL, COL) ((double)(((VAL).COL*1.0)/65535.0))
static GdkColor qtcGdkColor(double r, double g, double b)
{
GdkColor col;
col.red=limit(r*65535);
col.green=limit(g*65535);
col.blue=limit(b*65535);
return col;
}
#define TO_COLOR(R, G, B) qtcGdkColor(R, G, B)
#endif
static inline double ColorUtils_normalize(double a)
{
return (a < 1.0 ? (a > 0.0 ? a : 0.0) : 1.0);
}
static inline double ColorUtils_wrap(double a)
{
static double d = 1.0;
double r = fmod(a, d);
return (r < 0.0 ? d + r : (r > 0.0 ? r : 0.0));
}
#define HCY_REC 709 // use 709 for now
#if HCY_REC == 601
static const double yc[3] = { 0.299, 0.587, 0.114 };
#elif HCY_REC == 709
static const double yc[3] = {0.2126, 0.7152, 0.0722};
#else // use TQt values
static const double yc[3] = { 0.34375, 0.5, 0.15625 };
#endif
static inline double ColorUtils_HCY_gamma(double n)
{
return pow(ColorUtils_normalize(n), 2.2);
}
static inline double ColorUtils_HCY_igamma(double n)
{
return pow(ColorUtils_normalize(n), 1.0/2.2);
}
static inline double ColorUtils_HCY_lumag(double r, double g, double b)
{
return r*yc[0] + g*yc[1] + b*yc[2];
}
typedef struct
{
double h, c, y;
} ColorUtils_HCY;
// static ColorUtils_HCY ColorUtils_HCY_fromValues(double h_, double c_, double y_/*, double a_*/)
// {
// h = h_;
// c = c_;
// y = y_;
// // a = a_;
// }
static ColorUtils_HCY ColorUtils_HCY_fromColor(const color *color)
{
ColorUtils_HCY hcy;
double r = ColorUtils_HCY_gamma(FLOAT_COLOR(*color, red));
double g = ColorUtils_HCY_gamma(FLOAT_COLOR(*color, green));
double b = ColorUtils_HCY_gamma(FLOAT_COLOR(*color, blue));
// a = color.alphaF();
// luma component
hcy.y = ColorUtils_HCY_lumag(r, g, b);
// hue component
double p = MAX(MAX(r, g), b);
double n = MIN(MIN(r, g), b);
double d = 6.0 * (p - n);
if (n == p)
hcy.h = 0.0;
else if (r == p)
hcy.h = ((g - b) / d);
else if (g == p)
hcy.h = ((b - r) / d) + (1.0 / 3.0);
else
hcy.h = ((r - g) / d) + (2.0 / 3.0);
// chroma component
if (0.0 == hcy.y || 1.0 == hcy.y)
hcy.c = 0.0;
else
hcy.c = MAX( (hcy.y - n) / hcy.y, (p - hcy.y) / (1 - hcy.y) );
return hcy;
}
static color ColorUtils_HCY_toColor(ColorUtils_HCY *hcy)
{
// start with sane component values
double _h = ColorUtils_wrap(hcy->h);
double _c = ColorUtils_normalize(hcy->c);
double _y = ColorUtils_normalize(hcy->y);
// calculate some needed variables
double _hs = _h * 6.0, th, tm;
if (_hs < 1.0) {
th = _hs;
tm = yc[0] + yc[1] * th;
}
else if (_hs < 2.0) {
th = 2.0 - _hs;
tm = yc[1] + yc[0] * th;
}
else if (_hs < 3.0) {
th = _hs - 2.0;
tm = yc[1] + yc[2] * th;
}
else if (_hs < 4.0) {
th = 4.0 - _hs;
tm = yc[2] + yc[1] * th;
}
else if (_hs < 5.0) {
th = _hs - 4.0;
tm = yc[2] + yc[0] * th;
}
else {
th = 6.0 - _hs;
tm = yc[0] + yc[2] * th;
}
// calculate RGB channels in sorted order
double tn, to, tp;
if (tm >= _y) {
tp = _y + _y * _c * (1.0 - tm) / tm;
to = _y + _y * _c * (th - tm) / tm;
tn = _y - (_y * _c);
}
else {
tp = _y + (1.0 - _y) * _c;
to = _y + (1.0 - _y) * _c * (th - tm) / (1.0 - tm);
tn = _y - (1.0 - _y) * _c * tm / (1.0 - tm);
}
// return RGB channels in appropriate order
if (_hs < 1.0)
return TO_COLOR(ColorUtils_HCY_igamma(tp), ColorUtils_HCY_igamma(to), ColorUtils_HCY_igamma(tn));
else if (_hs < 2.0)
return TO_COLOR(ColorUtils_HCY_igamma(to), ColorUtils_HCY_igamma(tp), ColorUtils_HCY_igamma(tn));
else if (_hs < 3.0)
return TO_COLOR(ColorUtils_HCY_igamma(tn), ColorUtils_HCY_igamma(tp), ColorUtils_HCY_igamma(to));
else if (_hs < 4.0)
return TO_COLOR(ColorUtils_HCY_igamma(tn), ColorUtils_HCY_igamma(to), ColorUtils_HCY_igamma(tp));
else if (_hs < 5.0)
return TO_COLOR(ColorUtils_HCY_igamma(to), ColorUtils_HCY_igamma(tn), ColorUtils_HCY_igamma(tp));
else
return TO_COLOR(ColorUtils_HCY_igamma(tp), ColorUtils_HCY_igamma(tn), ColorUtils_HCY_igamma(to));
}
// #ifndef __cplusplus
static inline double ColorUtils_HCY_luma(const color *color)
{
return ColorUtils_HCY_lumag(ColorUtils_HCY_gamma(FLOAT_COLOR(*color, red)),
ColorUtils_HCY_gamma(FLOAT_COLOR(*color, green)),
ColorUtils_HCY_gamma(FLOAT_COLOR(*color, blue)));
}
static inline double ColorUtils_mixQreal(double a, double b, double bias)
{
return a + (b - a) * bias;
}
static inline double ColorUtils_luma(const color *color)
{
return ColorUtils_HCY_luma(color);
}
static double ColorUtils_contrastRatio(const color *c1, const color *c2)
{
double y1 = ColorUtils_luma(c1), y2 = ColorUtils_luma(c2);
if (y1 > y2)
return (y1 + 0.05) / (y2 + 0.05);
else
return (y2 + 0.05) / (y1 + 0.05);
}
static color ColorUtils_lighten(const color *color, double ky, double kc)
{
ColorUtils_HCY c=ColorUtils_HCY_fromColor(color);
c.y = 1.0 - ColorUtils_normalize((1.0 - c.y) * (1.0 - ky));
c.c = 1.0 - ColorUtils_normalize((1.0 - c.c) * kc);
return ColorUtils_HCY_toColor(&c);
}
static color ColorUtils_darken(const color *color, double ky, double kc)
{
ColorUtils_HCY c=ColorUtils_HCY_fromColor(color);
c.y = ColorUtils_normalize(c.y * (1.0 - ky));
c.c = ColorUtils_normalize(c.c * kc);
return ColorUtils_HCY_toColor(&c);
}
static color ColorUtils_shade(const color *color, double ky, double kc)
{
ColorUtils_HCY c=ColorUtils_HCY_fromColor(color);
c.y = ColorUtils_normalize(c.y + ky);
c.c = ColorUtils_normalize(c.c + kc);
return ColorUtils_HCY_toColor(&c);
}
static color ColorUtils_mix(const color *c1, const color *c2, double bias);
static color ColorUtils_tintHelper(const color *base, const color *col, double amount)
{
color mixed=ColorUtils_mix(base, col, pow(amount, 0.3));
ColorUtils_HCY c=ColorUtils_HCY_fromColor(&mixed);
c.y = ColorUtils_mixQreal(ColorUtils_luma(base), c.y, amount);
return ColorUtils_HCY_toColor(&c);
}
static color ColorUtils_tint(const color *base, const color *col, double amount)
{
if (amount <= 0.0) return *base;
if (amount >= 1.0) return *col;
if (isnan(amount)) return *base;
double ri = ColorUtils_contrastRatio(base, col);
double rg = 1.0 + ((ri + 1.0) * amount * amount * amount);
double u = 1.0, l = 0.0;
color result;
int i;
for (i = 12 ; i ; --i) {
double a = 0.5 * (l+u);
result = ColorUtils_tintHelper(base, col, a);
double ra = ColorUtils_contrastRatio(base, &result);
if (ra > rg)
u = a;
else
l = a;
}
return result;
}
static color ColorUtils_mix(const color *c1, const color *c2, double bias)
{
if (bias <= 0.0) return *c1;
if (bias >= 1.0) return *c2;
if (isnan(bias)) return *c1;
{
double r = ColorUtils_mixQreal(FLOAT_COLOR(*c1, red), FLOAT_COLOR(*c2, red), bias);
double g = ColorUtils_mixQreal(FLOAT_COLOR(*c1, green), FLOAT_COLOR(*c2, green), bias);
double b = ColorUtils_mixQreal(FLOAT_COLOR(*c1, blue), FLOAT_COLOR(*c2, blue), bias);
/*double a = ColorUtils_mixQreal(FLOAT_COLOR(*c1, alpha), FLOAT_COLOR(*c2, alpha), bias);*/
return TO_COLOR(r, g, b);
}
}
// #endif
/* Added!!! */
// static color ColorUtils_shade_qtc(const color *color, double k)
// {
// ColorUtils_HCY c=ColorUtils_HCY_fromColor(color);
// c.y = ColorUtils_normalize(c.y * (k>1.0 ? (k*1.1) : (k<1.0 ? (k*0.9) : k)));
// return ColorUtils_HCY_toColor(&c);
// }
|