This repository has been archived by the owner on Oct 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
colour-manipulation.inc
250 lines (186 loc) · 7.67 KB
/
colour-manipulation.inc
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
#include <float>
#if defined _col_man_included
#endinput
#endif
#define _col_man_included
#define COLOR_MODE_RGBA COLOUR_MODE_RGBA
#define COLOR_MODE_ARGB COLOUR_MODE_ARGB
#define COLOR_MODE_RGB COLOUR_MODE_RGB
#define COLOR_COMPONENT_R COLOUR_COMPONENT_R
#define COLOR_COMPONENT_G COLOUR_COMPONENT_G
#define COLOR_COMPONENT_B COLOUR_COMPONENT_B
#define COLOR_COMPONENT_A COLOUR_COMPONENT_A
#define GetColorComponentFractValue GetColourComponentFractValue
#define GetColorComponentBinaryValue GetColourComponentBinaryValue
#define AddColorComponentGammaCor AddColourComponentGammaCor
#define RemoveColorComponentGammaCor RemoveColourComponentGammaCor
#define GetColorComponents GetColourComponents
#define GetColorComponent GetColourComponent
#define SetColorComponent SetColourComponent
#define GetColorCode GetColourCode
#define ConvertColor ConvertColour
#define InterpolateColors InterpolateColours
#define DarkenColor DarkenColour
#define LightenColor LightenColour
#define GrayscaleColor GrayscaleColour
#define GreyscaleColor GrayscaleColour
#define GreyscaleColour GrayscaleColour
enum ColourMode {
COLOUR_MODE_RGBA,
COLOUR_MODE_ARGB,
COLOUR_MODE_RGB
};
enum ColourComponent {
COLOUR_COMPONENT_R,
COLOUR_COMPONENT_G,
COLOUR_COMPONENT_B,
COLOUR_COMPONENT_A,
}
stock Float:GetColourComponentFractValue(value) {
return float(value) / 255.0;
}
stock GetColourComponentBinaryValue(Float:value) {
return floatround(value * 255.0);
}
stock Float:AddColourComponentGammaCor(Float:value) {
if (value <= 0.0031308) {
value *= 12.92;
} else {
value = (1.055 * floatpower(value, 1.0 / 2.4)) - 0.055;
}
return value;
}
stock Float:RemoveColourComponentGammaCor(Float:value) {
if (value <= 0.04045) {
value /= 12.92;
} else {
value = floatpower((value + 0.055) / 1.055, 2.4);
}
return value;
}
stock GetColourComponents(colour, &Float:r, &Float:g, &Float:b, &Float:a = 1.0, ColourMode:mode = COLOUR_MODE_RGBA) {
switch (mode) {
case COLOUR_MODE_RGBA: {
r = GetColourComponentFractValue((colour >>> 24) & 0xFF);
g = GetColourComponentFractValue((colour >>> 16) & 0xFF);
b = GetColourComponentFractValue((colour >>> 8) & 0xFF);
a = GetColourComponentFractValue(colour & 0xFF);
}
case COLOUR_MODE_ARGB: {
r = GetColourComponentFractValue((colour >>> 16) & 0xFF);
g = GetColourComponentFractValue((colour >>> 8) & 0xFF);
b = GetColourComponentFractValue(colour & 0xFF);
a = GetColourComponentFractValue((colour >>> 24) & 0xFF);
}
case COLOUR_MODE_RGB: {
r = GetColourComponentFractValue((colour >>> 16) & 0xFF);
g = GetColourComponentFractValue((colour >>> 8) & 0xFF);
b = GetColourComponentFractValue(colour & 0xFF);
a = 1.0;
}
}
}
stock Float:GetColourComponent(colour, ColourComponent:component, ColourMode:mode = COLOUR_MODE_RGBA) {
new Float:components[4];
GetColourComponents(colour, components[0], components[1], components[2], components[3], mode);
return components[_:component];
}
stock SetColourComponent(colour, ColourComponent:component, Float:value, ColourMode:mode = COLOUR_MODE_RGBA) {
new Float:components[4];
GetColourComponents(colour, components[0], components[1], components[2], components[3], mode);
if (0.0 <= value <= 1.0) {
components[_:component] = value;
}
return GetColourCode(components[0], components[1], components[2], components[3], mode);
}
stock Float:GetColourBrightness(colour, ColourMode:mode = COLOUR_MODE_RGBA) {
new Float:components[4];
GetColourComponents(colour, components[0], components[1], components[2], components[3], mode);
return AddColourComponentGammaCor(
0.212655 * RemoveColourComponentGammaCor(components[0]) +
0.715158 * RemoveColourComponentGammaCor(components[1]) +
0.072187 * RemoveColourComponentGammaCor(components[2])
);
}
stock GetColourCode(Float:r, Float:g, Float:b, Float:a = 1.0, ColourMode:mode = COLOUR_MODE_RGBA) {
new colour, components[4];
components[0] = GetColourComponentBinaryValue(r);
components[1] = GetColourComponentBinaryValue(g);
components[2] = GetColourComponentBinaryValue(b);
components[3] = GetColourComponentBinaryValue(a);
switch (mode) {
case COLOUR_MODE_RGBA: {
colour = (components[3] & 0xFF) | ((components[2] & 0xFF) << 8) | ((components[1] & 0xFF) << 16) | ((components[0] & 0xFF) << 24);
}
case COLOUR_MODE_ARGB: {
colour = (components[2] & 0xFF) | ((components[1] & 0xFF) << 8) | ((components[0] & 0xFF) << 16) | ((components[3] & 0xFF) << 24);
}
case COLOUR_MODE_RGB: {
colour = (components[2] & 0xFF) | ((components[1] & 0xFF) << 8) | ((components[0] & 0xFF) << 16);
}
}
return colour;
}
stock ConvertColour(colour, ColourMode:from, ColourMode:to, Float:alpha = 1.0) {
new a = GetColourComponentBinaryValue(alpha);
switch (from) {
case COLOUR_MODE_RGBA: {
switch (to) {
case COLOUR_MODE_ARGB: {
colour = ((colour << 24) & 0xFF000000) | ((colour >>> 8) & 0xFFFFFF);
}
case COLOUR_MODE_RGB: {
colour = (colour >>> 8) & 0xFFFFFF;
}
}
}
case COLOUR_MODE_ARGB: {
switch (to) {
case COLOUR_MODE_RGBA: {
colour = ((colour << 8) & 0xFFFFFF00) | ((colour >>> 24) & 0xFF);
}
case COLOUR_MODE_RGB: {
colour = colour & 0x00FFFFFF;
}
}
}
case COLOUR_MODE_RGB: {
switch (to) {
case COLOUR_MODE_RGBA: {
colour = (colour << 8) | a;
}
case COLOUR_MODE_ARGB: {
colour = colour | (a << 24);
}
}
}
}
return colour;
}
stock InterpolateColours(colour1, colour2, Float:amount, ColourMode:mode = COLOUR_MODE_RGBA) {
new Float:components[2][4];
GetColourComponents(colour1, components[0][0], components[0][1], components[0][2], components[0][3], mode);
GetColourComponents(colour2, components[1][0], components[1][1], components[1][2], components[1][3], mode);
for (new i; i < 4; i++) {
components[0][i] = RemoveColourComponentGammaCor(components[0][i]);
components[1][i] = RemoveColourComponentGammaCor(components[1][i]);
components[0][i] += (components[1][i] - components[0][i]) * amount;
components[0][i] = AddColourComponentGammaCor(components[0][i]);
}
return GetColourCode(components[0][0], components[0][1], components[0][2], components[0][3], mode);
}
stock DarkenColour(colour, Float:amount, ColourMode:mode = COLOUR_MODE_RGBA) {
new Float:alpha = GetColourComponent(colour, COLOUR_COMPONENT_A, mode),
black = ConvertColour(0x000000, COLOUR_MODE_RGB, mode, alpha);
return InterpolateColours(colour, black, amount, mode);
}
stock LightenColour(colour, Float:amount, ColourMode:mode = COLOUR_MODE_RGBA) {
new Float:alpha = GetColourComponent(colour, COLOUR_COMPONENT_A, mode),
white = ConvertColour(0xFFFFFF, COLOUR_MODE_RGB, mode, alpha);
return InterpolateColours(colour, white, amount, mode);
}
stock GrayscaleColour(colour, ColourMode:mode = COLOUR_MODE_RGBA) {
new Float:alpha = GetColourComponent(colour, COLOUR_COMPONENT_A, mode),
Float:brightness = GetColourBrightness(colour, mode);
return GetColourCode(brightness, brightness, brightness, alpha, mode);
}