-
Notifications
You must be signed in to change notification settings - Fork 46
/
color.ts
95 lines (81 loc) · 2.11 KB
/
color.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
/**
* Represents a color containing RGBA components.
*/
export class Color {
private _array4: Float32Array
private _array3: Float32Array
/**
* Creates a new color with the specified components (in range 0-1).
* @param r The R (red) component.
* @param g The G (green) component.
* @param b The B (blue) component.
* @param a The A (alpha) component.
*/
constructor(r = 0, g = 0, b = 0, a = 1) {
this._array4 = new Float32Array([r, g, b, a])
this._array3 = this._array4.subarray(0, 3)
}
/**
* Creates a new color with the specified components (in range 0-255).
* @param r The R (red) component.
* @param g The G (green) component.
* @param b The B (blue) component.
* @param a The A (alpha) component.
*/
static fromBytes(r = 0, g = 0, b = 0, a = 255) {
return new Color(r / 255, g / 255, b / 255, a / 255)
}
/**
* Creates a new color from the specified hex value.
* @param hex The hex value as a string or a number.
*/
static fromHex(hex: number | string) {
if (typeof hex === "string") {
hex = parseInt(hex.replace(/[^0-9A-F]/gi, ""), 16)
}
return Color.fromBytes((hex >> 16) & 255, (hex >> 8) & 255, hex & 255)
}
/** The color as an typed array containing RGB. */
get rgb() {
return this._array3
}
/** The color as an typed array containing RGBA. */
get rgba() {
return this._array4
}
/** The R (red) component. */
get r() {
return this._array4[0]
}
set r(value: number) {
this._array4[0] = value
}
/** The G (green) component. */
get g() {
return this._array4[1]
}
set g(value: number) {
this._array4[1] = value
}
/** The B (blue) component. */
get b() {
return this._array4[2]
}
set b(value: number) {
this._array4[2] = value
}
/** The A (alpha) component. */
get a() {
return this._array4[3]
}
set a(value: number) {
this._array4[3] = value
}
/**
* Creates a new color from the specified source.
* @param source The source to create the color from.
*/
static from(source: number[] | Float32Array) {
return new Color(...source)
}
}