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
|
=encoding UTF-8
=head1 NAME
BH_Color - color manipulation utility
=head1 SYNTAX
#include <BH/Color.h>
cc prog.c -o prog -lbh
=head1 DESCRIPTION
The BH_Color module provides mechanism for color representation and translation
between different color spaces color spaces (RGB, HSL and HSV). Additionally,
this module provides mechanism for blending two colors.
=head1 NOTES
Internally, color is stored in four 16-bit integers.
When converting between RGB and HSV/HSL there is ~0.009% error. While this is
not an issue, if you are using 8-bit RGB value, this may become a problem for
the 16-bit RGB values (not every RGB value can be represented in unique HSV/HSL
value). If you need this kind of precision, then this module is not for you.
Almost all color blending modes work like in other software packages. The
exception is I<BH_COLOR_MODE_SOFT_LIGHT>. This library opted-in for the Pegtop's
version of the formula (which might yield different results). This might be an
issue if you are trying to implement standard complient SVG or PDF renderer.
The I<BH_COLOR_MODE_HUE>, I<BH_COLOR_MODE_SATURATION>, I<BH_COLOR_MODE_COLOR>
and I<BH_COLOR_MODE_LUMINOSITY> are implemented in HSL color space.
=head1 API CALLS
=head2 BH_ColorRGBA8
void BH_ColorRGBA8(const BH_Color *color,
uint8_t *r,
uint8_t *g,
uint8_t *b,
uint8_t *a);
Gets 8-bit RGBA representation of the I<color>.
Parameters I<r>, I<g>, I<b>, I<a> represent color components in RGB color space.
=head2 BH_ColorRGBA16
void BH_ColorRGBA16(const BH_Color *color,
uint16_t *r,
uint16_t *g,
uint16_t *b,
uint16_t *a);
Gets 16-bit RGBA representation of the I<color>.
Parameters I<r>, I<g>, I<b>, I<a> represent color components in RGB color space.
=head2 BH_ColorRGBAf
void BH_ColorRGBAf(const BH_Color *color,
float *r,
float *g,
float *b,
float *a);
Gets floating-point RGBA representation of the I<color>.
Parameters I<r>, I<g>, I<b>, I<a> represent color components in RGB color space.
=head2 BH_ColorSetRGBA8
void BH_ColorSetRGBA8(BH_Color *color,
uint8_t r,
uint8_t g,
uint8_t b,
uint8_t a);
Sets 8-bit I<r>, I<g>, I<b>, I<a> components of the color.
=head2 BH_ColorSetRGBA16
void BH_ColorSetRGBA16(BH_Color *color,
uint16_t r,
uint16_t g,
uint16_t b,
uint16_t a);
Sets 16-bit I<r>, I<g>, I<b>, I<a> components of the color.
=head2 BH_ColorSetRGBAf
void BH_ColorSetRGBAf(BH_Color *color,
float r,
float g,
float b,
float a);
Sets floating-point I<r>, I<g>, I<b>, I<a> components of the color.
=head2 BH_ColorHSVAf
void BH_ColorHSVAf(const BH_Color *color,
float *h,
float *s,
float *v,
float *a);
Gets floating-point HSV representation of the I<color>.
Parameters I<h>, I<s>, I<v>, I<a> represent color components in HSV color space.
=head2 BH_ColorSetHSVAf
void BH_ColorSetHSVAf(BH_Color *color,
float h,
float s,
float v,
float a);
Sets floating-point I<h>, I<s>, I<v>, I<a> components of the color.
=head2 BH_ColorHSLAf
void BH_ColorHSLAf(const BH_Color *color,
float *h,
float *s,
float *l,
float *a);
Gets floating-point HSL representation of the I<color>.
Parameters I<h>, I<s>, I<l>, I<a> represent color components in HSV color space.
=head2 BH_ColorSetHSLAf
void BH_ColorSetHSLAf(BH_Color *color,
float h,
float s,
float l,
float a);
Sets floating-point I<h>, I<s>, I<l>, I<a> components of the color.
=head2 BH_ColorToRGBA
void BH_ColorToRGBA(const BH_Color *color,
BH_Color *out);
Converts I<color> to RGB color space.
=head2 BH_ColorToHSVA
void BH_ColorToHSVA(const BH_Color *color,
BH_Color *out);
Converts I<color> to HSV color space.
=head2 BH_ColorToHSLA
void BH_ColorToHSLA(const BH_Color *color,
BH_Color *out);
Converts I<color> to HSL color space.
=head2 BH_ColorBlend
void BH_ColorBlend(const BH_Color *foreground,
const BH_Color *background,
int mode,
BH_Color *dest);
Blends I<foreground> and I<background> color, using specified blending I<mode>.
The following modes can be used:
=over
=item B<BH_COLOR_MODE_NORMAL>
r = f
=item B<BH_COLOR_MODE_MULTIPLY>
r = f * b
=item B<BH_COLOR_MODE_SCREEN>
r = 1 - (1 - f) * (1 - b)
=item B<BH_COLOR_MODE_OVERLAY>
r = HardLight(b, f)
=item B<BH_COLOR_MODE_DARKEN>
r = min(f, b)
=item B<BH_COLOR_MODE_LIGHTEN>
r = max(f, b)
=item B<BH_COLOR_MODE_COLOR_DODGE>
r = min(1, b / (1 - f))
=item B<BH_COLOR_MODE_COLOR_BURN>
r = 1 - min(1, (1 - b) / f)
=item B<BH_COLOR_MODE_LINEAR_DODGE>
r = f + b
=item B<BH_COLOR_MODE_LINEAR_BURN>
r = f + b - 1
=item B<BH_COLOR_MODE_HARD_LIGHT>
if (f < 0.5)
r = Multipy(2 * f, b)
else
r = Screen(2 * f - 1, b)
=item B<BH_COLOR_MODE_SOFT_LIGHT>
r = (1 - 2 * f) * b^2 + 2 * f * b
=item B<BH_COLOR_MODE_DIFFERENCE>
r = abs(f - b)
=item B<BH_COLOR_MODE_EXCLUSION>
r = f + b - (2 * f * b)
=item B<BH_COLOR_MODE_HUE>
r = HSL(H(f), S(b), L(b))
=item B<BH_COLOR_MODE_SATURATION>
r = HSL(H(b), S(f), L(b))
=item B<BH_COLOR_MODE_COLOR>
r = HSL(H(f), S(f), L(b))
=item B<BH_COLOR_MODE_LUMINOSITY>
r = HSL(H(b), S(b), L(f))
=back
=head1 STRUCTURES
=head2 BH_Color
typedef struct BH_Color
{
int type;
union
{
struct
{
uint16_t r;
uint16_t g;
uint16_t b;
uint16_t a;
} rgba;
struct
{
uint16_t h;
uint16_t s;
uint16_t v;
uint16_t a;
} hsva;
struct
{
uint16_t h;
uint16_t s;
uint16_t l;
uint16_t a;
} hsla;
} data;
} BH_Color;
=head1 SEE ALSO
L<BH>
|