/
color32Utils.ts
220 lines (199 loc) · 6.46 KB
/
color32Utils.ts
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
/// <summary>
/// Helper static class that implements various color operations with the color information
/// being stored as an unsigned 32 bits value.
/// In related methods, the intensity is a byte value between 0 (black) and 255 (white).
/// </summary>
import Color from "./Color";
/// <summary>
/// Converts a (red, green, blue) bytes triplets to a 32 bits color value.
/// </summary>
/// <param name="red">The red component as a byte value.</param>
/// <param name="green">The green component as a byte value.</param>
/// <param name="blue">The blue component as a byte value.</param>
/// <returns>A 32 bits color value.</returns>
/**
* @category Color32
*/
export function toColor32(
redOrColor: number | Color,
green = 0,
blue = 0
): number {
if (typeof redOrColor !== "number") {
green = redOrColor.g * 255;
blue = redOrColor.b * 255;
redOrColor = redOrColor.r * 255;
}
return (redOrColor << 16) | (green << 8) | (blue & 0xff);
}
/// <summary>
/// Extracts the red component of a 32 bits color value.
/// </summary>
/// <param name="color32">The 32 bits color value.</param>
/// <returns>The red component of the color.</returns>
/**
* @category Color32
*/
export function getRed(color32: number): number {
return (color32 >> 16) & 0xff;
}
/// <summary>
/// Extracts the green component of a 32 bits color value.
/// </summary>
/// <param name="color32">The 32 bits color value.</param>
/// <returns>The green component of the color.</returns>
/**
* @category Color32
*/
export function getGreen(color32: number): number {
return (color32 >> 8) & 0xff;
}
/// <summary>
/// Extracts the blue component of a 32 bits color value.
/// </summary>
/// <param name="color32">The 32 bits color value.</param>
/// <returns>The blue component of the color.</returns>
/**
* @category Color32
*/
export function getBlue(color32: number): number {
return color32 & 0xff;
}
export function mulColors(a: number, b: number): number {
const red = (getRed(a) * getRed(b)) / 255;
const green = (getGreen(a) * getGreen(b)) / 255;
const blue = (getBlue(a) * getBlue(b)) / 255;
return toColor32(red, green, blue);
}
/// <summary>
/// Combines the two colors by selecting the highest value for each component.
/// </summary>
/// <param name="color32">The first color to combine.</param>
/// <param name="otherColor32">The second color to combine.</param>
/// <returns></returns>
/**
* @category Color32
*/
export function combineColors(color32: number, secondColor32: number): number {
const red = Math.max(getRed(color32), getRed(secondColor32));
const green = Math.max(getGreen(color32), getGreen(secondColor32));
const blue = Math.max(getBlue(color32), getBlue(secondColor32));
return toColor32(red, green, blue);
}
/// <summary>
/// Interpolates linearly between two colors each given for a specific timestamp.
/// </summary>
/// <param name="color32">The first color.</param>
/// <param name="timestamp">The timestamp for the first color.</param>
/// <param name="secondColor32">The second color.</param>
/// <param name="secondTimestamp">The timestamp for the second color.</param>
/// <param name="time">The time for which to calculate the color.</param>
/// <returns>The color for the given time.</returns>
/**
* @category Color32
*/
export function interpolateColors(
color32: number,
timestamp: number,
secondColor32: number,
secondTimestamp: number,
time: number
): number {
// To stick to integer math, we'll scale the values
const scaler = 1024;
const scaledPercent =
((time - timestamp) * scaler) / (secondTimestamp - timestamp);
const scaledRed =
getRed(color32) * (scaler - scaledPercent) +
getRed(secondColor32) * scaledPercent;
const scaledGreen =
getGreen(color32) * (scaler - scaledPercent) +
getGreen(secondColor32) * scaledPercent;
const scaledBlue =
getBlue(color32) * (scaler - scaledPercent) +
getBlue(secondColor32) * scaledPercent;
return toColor32(
scaledRed / scaler,
scaledGreen / scaler,
scaledBlue / scaler
);
}
/// <summary>
/// Interpolates linearly the two intensities each given for a specific timestamp.
/// </summary>
/// <param name="intensity1">The first intensity value.</param>
/// <param name="timestamp1">The timestamp for the first intensity.</param>
/// <param name="intensity2">The second intensity value.</param>
/// <param name="timestamp2">The timestamp for the second intensity.</param>
/// <param name="time">The time for which to calculate the intensity.</param>
/// <returns>The intensity for the given time.</returns>
/**
* @category Color32
*/
export function interpolateIntensity(
intensity1: number,
timestamp1: number,
intensity2: number,
timestamp2: number,
time: number
): number {
const scaler = 1024;
const scaledPercent =
((time - timestamp1) * scaler) / (timestamp2 - timestamp1);
return Math.floor(
(intensity1 * (scaler - scaledPercent) + intensity2 * scaledPercent) /
scaler
);
}
/// <summary>
/// Modulates the color with the given intensity. The later is a value
/// between 0 (black) and (white).
/// </summary>
/// <param name="color">The color to modulate.</param>
/// <param name="intensity">The intensity to apply.</param>
/// <returns></returns>
/**
* @category Color32
*/
export function modulateColor(color32: number, intensity: number): number {
const red = (getRed(color32) * intensity) / 255;
const green = (getGreen(color32) * intensity) / 255;
const blue = (getBlue(color32) * intensity) / 255;
return toColor32(red, green, blue);
}
/// <summary>
/// Returns a color along the following looped color blending:
/// [position = 0] red -> green -> blue -> red [position = 255].
/// </summary>
/// <param name="position">Position on the rainbow wheel.</param>
/// <param name="intensity">Intensity of the returned color.</param>
/// <returns>A color.</returns>
/**
* @category Color32
*/
export function rainbowWheel(position: number, intensity = 255): number {
if (position < 85) {
return toColor32(
(position * 3 * intensity) / 255,
((255 - position * 3) * intensity) / 255,
0
);
} else if (position < 170) {
position -= 85;
return toColor32(
((255 - position * 3) * intensity) / 255,
0,
(position * 3 * intensity) / 255
);
} else {
position -= 170;
return toColor32(
0,
(position * 3 * intensity) / 255,
((255 - position * 3) * intensity) / 255
);
}
}
export function faceWheel(face: number, count: number): number {
return rainbowWheel((face * 256) / count);
}