forked from chroma-sdk/chroma-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColor.ts
86 lines (78 loc) · 2.49 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
export class Color {
public static Black: Color = new Color("000000");
public static Red: Color = new Color("ff0000");
public static Green: Color = new Color("00ff00");
public static Blue: Color = new Color("0000ff");
public static HotPink: Color = new Color(255, 105, 180);
public static Orange: Color = new Color("ffa500");
public static Pink: Color = new Color("ff00ff");
public static Purple: Color = new Color("800080");
public static While: Color = new Color(255, 255, 255);
public static Yellow: Color = new Color(255, 255, 0);
public r: number;
public g: number;
public b: number;
public isKey: boolean = false;
constructor(r: number | string, g: number= null, b: number= null) {
if (g === null && b === null && r !== null) {
if (typeof r === "string") {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(r);
this.r = parseInt(result[1], 16);
this.g = parseInt(result[2], 16);
this.b = parseInt(result[3], 16);
} else {
this.b = (r >> 16) & 0xff; // tslint:disable-line:no-bitwise
this.g = (r >> 8) & 0xff; // tslint:disable-line:no-bitwise
this.r = (r >> 0) & 0xff; // tslint:disable-line:no-bitwise
}
} else {
this.r = Math.round(r as number);
this.g = Math.round(g);
this.b = Math.round(b);
}
if (this.r > 255) {
this.r = 255;
}
if (this.g > 255) {
this.g = 255;
}
if (this.b > 255) {
this.b = 255;
}
if (this.r < 0) {
this.r = 0;
}
if (this.g < 0) {
this.g = 0;
}
if (this.b < 0) {
this.b = 0;
}
}
public toBGR() {
let rhex = this.r.toString(16);
if (rhex.length < 2) {
rhex = "0" + rhex;
}
let ghex = this.g.toString(16);
if (ghex.length < 2) {
ghex = "0" + ghex;
}
let bhex = this.b.toString(16);
if (bhex.length < 2) {
bhex = "0" + bhex;
}
let result = bhex + ghex + rhex;
if (this.isKey) {
result = "ff" + result;
}
return parseInt(result, 16);
}
public toJSON() {
return this.toBGR();
}
public toString() {
return this.r + " " + this.g + " " + this.b;
}
}
export default Color;