summaryrefslogtreecommitdiffstats
path: root/libdimension/color.c
blob: 6de8d0a039643a27cc6ad86f33a9fada227ca26e (plain)
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
/*************************************************************************
 * 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.5,
  .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,
};

/* 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 back 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)
{
  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.trans  - c1.trans)  + c1.trans,
    n*(c2.filter - c1.filter) + c1.filter
  );
}

/* 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 back 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_translucency(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_translucency(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);
}