-
Notifications
You must be signed in to change notification settings - Fork 3
/
Color.c
74 lines (51 loc) · 1.27 KB
/
Color.c
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
#include "Color.h"
#include <math.h>
const float cEpsilon = 1/10000.0;
Color makeColorWhite() {
return makeColorLightness(1);
}
Color makeColorBlack() {
return makeColorLightness(0);
}
Color makeColorLightness(const float l) {
return (Color) {l, l, l};
}
Color makeColor(const float red, const float green, const float blue) {
return (Color) {red, green, blue};
}
bool cEqual(const Color a, const Color b) {
return
a.red + cEpsilon > b.red && b.red + cEpsilon > a.red &&
a.green + cEpsilon > b.green && b.green + cEpsilon > a.green &&
a.blue + cEpsilon > b.blue && b.blue + cEpsilon > a.blue
;
}
Color cAdd(const Color a, const Color b) {
return (Color) {
a.red + b.red,
a.green + b.green,
a.blue + b.blue
};
}
Color cMul(const Color a, const Color b) {
return (Color) {
a.red * b.red,
a.green * b.green,
a.blue * b.blue
};
}
Color csMul(const Color c, const float s) {
return (Color) {
c.red * s,
c.green * s,
c.blue * s
};
}
float cBrightness(const Color c) {
return (c.red + c.green + c.blue) / 3;
}
unsigned char linearFloatToGammaEncodedUint8(const float channel, const float gamma) {
float gammaEncoded = powf(channel, 1/gamma);
float clipped = fmin(fmax(gammaEncoded, 0), 1);
return clipped * 255 + 0.5;
}