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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
/*************************************************************************
* Copyright (C) 2009-2010 Tavian Barnes <tavianator@tavianator.com> *
* *
* This file is part of The Dimension Library. *
* *
* The Dimension Library is free software; you can redistribute it and/ *
* or modify it under the terms of the GNU Lesser General Public License *
* as published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
* The Dimension 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
*************************************************************************/
/**
* @file
* Color handling.
*/
#include "dimension.h"
/* Standard colors */
const dmnsn_color dmnsn_black = {
.R = 0.0,
.G = 0.0,
.B = 0.0,
.trans = 0.0,
.filter = 0.0,
};
const dmnsn_color dmnsn_white = {
.R = 1.0,
.G = 1.0,
.B = 1.0,
.trans = 0.0,
.filter = 0.0,
};
const dmnsn_color dmnsn_clear = {
.R = 0.0,
.G = 0.0,
.B = 0.0,
.trans = 1.0,
.filter = 0.0,
};
const dmnsn_color dmnsn_red = {
.R = 1.0,
.G = 0.0,
.B = 0.0,
.trans = 0.0,
.filter = 0.0,
};
const dmnsn_color dmnsn_green = {
.R = 0.0,
.G = 1.0,
.B = 0.0,
.trans = 0.0,
.filter = 0.0,
};
const dmnsn_color dmnsn_blue = {
.R = 0.0,
.G = 0.0,
.B = 1.0,
.trans = 0.0,
.filter = 0.0,
};
const dmnsn_color dmnsn_magenta = {
.R = 1.0,
.G = 0.0,
.B = 1.0,
.trans = 0.0,
.filter = 0.0,
};
const dmnsn_color dmnsn_orange = {
.R = 1.0,
.G = 0.21404114048223255,
.B = 0.0,
.trans = 0.0,
.filter = 0.0,
};
const dmnsn_color dmnsn_yellow = {
.R = 1.0,
.G = 1.0,
.B = 0.0,
.trans = 0.0,
.filter = 0.0,
};
const dmnsn_color dmnsn_cyan = {
.R = 0.0,
.G = 1.0,
.B = 1.0,
.filter = 0.0,
.trans = 0.0,
};
/** Inverse function of sRGB's `C' function, for the reverse conversion. */
static double
dmnsn_sRGB_C_inv(double CsRGB)
{
/*
* If C represents R, G, and B, then the Clinear values are now found as
* follows:
*
* { Csrgb/12.92, Csrgb <= 0.04045
* Clinear = { 1/2.4
* { ((Csrgb + 0.055)/1.055) , Csrgb > 0.04045
*/
if (CsRGB == 1.0) {
return 1.0; /* Map 1.0 to 1.0 instead of 0.9999999999999999 */
} else if (CsRGB <= 0.040449936) {
return CsRGB/12.92;
} else {
return pow((CsRGB + 0.055)/1.055, 2.4);
}
}
/* Convert from sRGB space */
dmnsn_color
dmnsn_color_from_sRGB(dmnsn_color color)
{
dmnsn_color ret = {
.R = dmnsn_sRGB_C_inv(color.R),
.G = dmnsn_sRGB_C_inv(color.G),
.B = dmnsn_sRGB_C_inv(color.B),
.trans = color.trans,
.filter = color.filter,
};
return ret;
}
/** sRGB's `C' function. */
static double
dmnsn_sRGB_C(double Clinear)
{
/*
* If C represents R, G, and B, then the sRGB values are now found as follows:
*
* { 12.92*Clinear, Clinear <= 0.0031308
* Csrgb = { 1/2.4
* { (1.055)*Clinear - 0.055, Clinear > 0.0031308
*/
if (Clinear == 1.0) {
return 1.0; /* Map 1.0 to 1.0 instead of 0.9999999999999999 */
} else if (Clinear <= 0.0031308) {
return 12.92*Clinear;
} else {
return 1.055*pow(Clinear, 1.0/2.4) - 0.055;
}
}
/* Convert to sRGB space */
dmnsn_color
dmnsn_color_to_sRGB(dmnsn_color color)
{
dmnsn_color ret = {
.R = dmnsn_sRGB_C(color.R),
.G = dmnsn_sRGB_C(color.G),
.B = dmnsn_sRGB_C(color.B),
.trans = color.trans,
.filter = color.filter,
};
return ret;
}
/* Greyscale color intensity */
double
dmnsn_color_intensity(dmnsn_color color)
{
return 0.2126198631048975*color.R + 0.7151387878413206*color.G
+ 0.0721499433963131*color.B;
}
/* Add two colors */
dmnsn_color
dmnsn_color_add(dmnsn_color c1, dmnsn_color c2)
{
dmnsn_color ret = dmnsn_new_color(c1.R + c2.R, c1.G + c2.G, c1.B + c2.B);
/* Switch into absolute filter and transmittance space */
double n1 = dmnsn_color_intensity(c1), n2 = dmnsn_color_intensity(c2);
double f1 = c1.filter*c1.trans, f2 = c2.filter*c2.trans;
double t1 = c1.trans - f1, t2 = c2.trans - f2;
double f = 0.0;
if (n1 + n2 >= dmnsn_epsilon)
f = (n1*f1 + n2*f2)/(n1 + n2);
double t = t1 + t2;
/* Switch back */
ret.trans = f + t;
if (ret.trans >= dmnsn_epsilon)
ret.filter = f/ret.trans;
return ret;
}
/* Subtract two colors */
dmnsn_color
dmnsn_color_sub(dmnsn_color c1, dmnsn_color c2)
{
dmnsn_color ret = dmnsn_new_color(c1.R - c2.R, c1.G - c2.G, c1.B - c2.B);
/* Switch into absolute filter and transmittance space */
double n1 = dmnsn_color_intensity(c1), n2 = dmnsn_color_intensity(c2);
double f1 = c1.filter*c1.trans, f2 = c2.filter*c2.trans;
double t1 = c1.trans - f1, t2 = c2.trans - f2;
double f = 0.0;
if (n1 - n2 >= dmnsn_epsilon)
f = (n1*f1 - n2*f2)/(n1 - n2);
double t = t1 - t2;
/* Switch back */
ret.trans = f + t;
if (ret.trans >= dmnsn_epsilon)
ret.filter = f/ret.trans;
return ret;
}
/* Multiply a color by a scalar */
dmnsn_color
dmnsn_color_mul(double n, dmnsn_color color)
{
color.R *= n;
color.G *= n;
color.B *= n;
return color;
}
/* For n in [0, 1] get the color in a gradient between c1 and c2 */
dmnsn_color
dmnsn_color_gradient(dmnsn_color c1, dmnsn_color c2, double n)
{
dmnsn_color ret = dmnsn_new_color(
n*(c2.R - c1.R) + c1.R,
n*(c2.G - c1.G) + c1.G,
n*(c2.B - c1.B) + c1.B
);
/* Switch into absolute filter and transmittance space */
double f1 = c1.filter*c1.trans, f2 = c2.filter*c2.trans;
double t1 = c1.trans - f1, t2 = c2.trans - f2;
double f = n*(f2 - f1) + f1;
double t = n*(t2 - t1) + t1;
/* Switch back */
ret.trans = f + t;
ret.filter = 0.0;
if (ret.trans >= dmnsn_epsilon)
ret.filter = f/ret.trans;
return ret;
}
/* Filters `light' through `filter' */
dmnsn_color
dmnsn_filter_light(dmnsn_color light, dmnsn_color filter)
{
dmnsn_color transmitted = dmnsn_color_mul(
(1.0 - filter.filter)*filter.trans,
light
);
dmnsn_color filtered = dmnsn_color_mul(
filter.filter*filter.trans,
dmnsn_color_illuminate(filter, light)
);
dmnsn_color ret = dmnsn_color_add(transmitted, filtered);
/* Switch into absolute filter and transmittance space */
double lf = light.filter*light.trans, ff = filter.filter*filter.trans;
double lt = light.trans - lf, ft = filter.trans - ff;
double f = lf*(dmnsn_color_intensity(filtered) + ft) + lt*ff;
double t = ft*lt;
/* Switch back */
ret.trans = f + t;
ret.filter = 0.0;
if (ret.trans >= dmnsn_epsilon)
ret.filter = f/ret.trans;
return ret;
}
/* Adds the background contribution, `filtered', to `filter' */
dmnsn_color
dmnsn_apply_transparency(dmnsn_color filtered, dmnsn_color filter)
{
dmnsn_color ret = dmnsn_color_add(
dmnsn_color_mul(1.0 - filter.trans, filter),
filtered
);
ret.trans = filtered.trans;
ret.filter = filtered.filter;
return ret;
}
/* Adds the background contribution of `color' to `filter' */
dmnsn_color
dmnsn_apply_filter(dmnsn_color color, dmnsn_color filter)
{
return dmnsn_apply_transparency(dmnsn_filter_light(color, filter), filter);
}
/* Remove the filter channel */
dmnsn_color
dmnsn_remove_filter(dmnsn_color color)
{
double intensity = dmnsn_color_intensity(color);
double newtrans = (1.0 - (1.0 - intensity)*color.filter)*color.trans;
if (1.0 - newtrans >= dmnsn_epsilon)
color = dmnsn_color_mul((1.0 - color.trans)/(1.0 - newtrans), color);
color.trans = newtrans;
color.filter = 0.0;
return color;
}
/* Illuminates `color' with `light' */
dmnsn_color
dmnsn_color_illuminate(dmnsn_color light, dmnsn_color color)
{
return dmnsn_new_color5(light.R*color.R, light.G*color.G, light.B*color.B,
color.filter, color.trans);
}
|