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
|
/*************************************************************************
* 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,
.filter = 0.0,
.trans = 0.0
};
const dmnsn_color dmnsn_white = {
.R = 1.0,
.G = 1.0,
.B = 1.0,
.filter = 0.0,
.trans = 0.0
};
const dmnsn_color dmnsn_clear = {
.R = 0.0,
.G = 0.0,
.B = 0.0,
.filter = 0.0,
.trans = 1.0
};
const dmnsn_color dmnsn_red = {
.R = 1.0,
.G = 0.0,
.B = 0.0,
.filter = 0.0,
.trans = 0.0
};
const dmnsn_color dmnsn_green = {
.R = 0.0,
.G = 1.0,
.B = 0.0,
.filter = 0.0,
.trans = 0.0
};
const dmnsn_color dmnsn_blue = {
.R = 0.0,
.G = 0.0,
.B = 1.0,
.filter = 0.0,
.trans = 0.0
};
const dmnsn_color dmnsn_magenta = {
.R = 1.0,
.G = 0.0,
.B = 1.0,
.filter = 0.0,
.trans = 0.0
};
const dmnsn_color dmnsn_orange = {
.R = 1.0,
.G = 0.5,
.B = 0.0,
.filter = 0.0,
.trans = 0.0
};
const dmnsn_color dmnsn_yellow = {
.R = 1.0,
.G = 1.0,
.B = 0.0,
.filter = 0.0,
.trans = 0.0
};
const dmnsn_color dmnsn_cyan = {
.R = 0.0,
.G = 1.0,
.B = 1.0,
.filter = 0.0,
.trans = 0.0
};
/* 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);
double n1 = dmnsn_color_intensity(c1), n2 = dmnsn_color_intensity(c2);
if (n1 + n2 != 0.0) {
ret.filter = (n1*c1.filter + n2*c2.filter)/(n1 + n2);
}
ret.trans = c1.trans + c2.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)
{
return dmnsn_new_color5(
n*(c2.R - c1.R) + c1.R,
n*(c2.G - c1.G) + c1.G,
n*(c2.B - c1.B) + c1.B,
n*(c2.filter - c1.filter) + c1.filter,
n*(c2.trans - c1.trans) + c1.trans
);
}
/* Filters `light' through `filter' */
dmnsn_color
dmnsn_filter_light(dmnsn_color light, dmnsn_color filter)
{
dmnsn_color transmitted = dmnsn_color_mul(filter.trans, light);
dmnsn_color filtered = dmnsn_color_mul(
filter.filter,
dmnsn_color_illuminate(filter, light)
);
dmnsn_color ret = dmnsn_color_add(transmitted, filtered);
ret.filter = light.filter*dmnsn_color_intensity(filtered)
+ filter.filter*light.trans + filter.trans*light.filter;
ret.trans = filter.trans*light.trans;
return ret;
}
/* Adds the background contribution, `filtered', to `filter' */
dmnsn_color
dmnsn_apply_translucency(dmnsn_color filtered, dmnsn_color filter)
{
dmnsn_color ret = dmnsn_color_add(
dmnsn_color_mul(1.0 - (filter.filter + filter.trans), filter),
filtered
);
ret.filter = filtered.filter;
ret.trans = filtered.trans;
return ret;
}
/* Adds the background contribution of `color' to `filter' */
dmnsn_color
dmnsn_apply_filter(dmnsn_color color, dmnsn_color filter)
{
return dmnsn_apply_translucency(dmnsn_filter_light(color, filter), filter);
}
/* 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);
}
|