| 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 |
4x
4x
38344x
38344x
38344x
74201x
74201x
74201x
74201x
296x
148x
148x
148x
148x
296x
| // tslint:disable:member-ordering
import {
clamp,
denormalize,
getHexStringForByte,
roundToPrecisionSmall,
} from "./mathUtils";
export class ColorRGBA64 {
public static fromObject(data: {
r: number;
g: number;
b: number;
a: number;
}): ColorRGBA64 | null {
Eif (data && !isNaN(data.r) && !isNaN(data.g) && !isNaN(data.b)) {
Eif (!isNaN(data.a)) {
return new ColorRGBA64(data.r, data.g, data.b, data.a);
} else {
return new ColorRGBA64(data.r, data.g, data.b, 1);
}
}
return null;
}
constructor(red: number, green: number, blue: number, alpha: number) {
this.r = red;
this.g = green;
this.b = blue;
this.a = alpha;
}
// Scaled to the range [0.0 , 1.0]. Values outside this range are allowed but any methods that convert or tostring the values will also be clamped
public readonly r: number;
public readonly g: number;
public readonly b: number;
public readonly a: number;
public equalValue(rhs: ColorRGBA64): boolean {
return (
this.r === rhs.r && this.g === rhs.g && this.b === rhs.b && this.a === rhs.a
);
}
// #RRGGBB
public toStringHexRGB(): string {
return (
"#" +
getHexStringForByte(denormalize(this.r, 0.0, 255.0)) +
getHexStringForByte(denormalize(this.g, 0.0, 255.0)) +
getHexStringForByte(denormalize(this.b, 0.0, 255.0))
);
}
// #RRGGBBAA
public toStringHexRGBA(): string {
return (
this.toStringHexRGB() + getHexStringForByte(denormalize(this.a, 0.0, 255.0))
);
}
// #AARRGGBB
public toStringHexARGB(): string {
return (
"#" +
getHexStringForByte(denormalize(this.a, 0.0, 255.0)) +
getHexStringForByte(denormalize(this.r, 0.0, 255.0)) +
getHexStringForByte(denormalize(this.g, 0.0, 255.0)) +
getHexStringForByte(denormalize(this.b, 0.0, 255.0))
);
}
// rgb(0xRR, 0xGG, 0xBB)
public toStringWebRGB(): string {
return `rgb(${Math.round(denormalize(this.r, 0.0, 255.0))},${Math.round(
denormalize(this.g, 0.0, 255.0)
)},${Math.round(denormalize(this.b, 0.0, 255.0))})`;
}
// rgba(0xRR, 0xGG, 0xBB, a)
// Note that this follows the convention of putting alpha in the range [0.0,1.0] while the other three channels are [0,255]
public toStringWebRGBA(): string {
return `rgba(${Math.round(denormalize(this.r, 0.0, 255.0))},${Math.round(
denormalize(this.g, 0.0, 255.0)
)},${Math.round(denormalize(this.b, 0.0, 255.0))},${clamp(this.a, 0, 1)})`;
}
public roundToPrecision(precision: number): ColorRGBA64 {
return new ColorRGBA64(
roundToPrecisionSmall(this.r, precision),
roundToPrecisionSmall(this.g, precision),
roundToPrecisionSmall(this.b, precision),
roundToPrecisionSmall(this.a, precision)
);
}
public clamp(): ColorRGBA64 {
return new ColorRGBA64(
clamp(this.r, 0, 1),
clamp(this.g, 0, 1),
clamp(this.b, 0, 1),
clamp(this.a, 0, 1)
);
}
public toObject(): { r: number; g: number; b: number; a: number } {
return { r: this.r, g: this.g, b: this.b, a: this.a };
}
}
|