-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.model.ts
70 lines (54 loc) · 2.12 KB
/
color.model.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
import { hex2rgb, int2rgb, rgb2hex, rgb2int } from "../utils";
function checkColorValue(value: number): void {
console.assert(value >= 0);
console.assert(value <= 255);
}
export class Color {
public static readonly WHITE = new Color(255, 255, 255);
public static readonly YELLOW = new Color(255, 255, 0);
public static readonly MAGENTA = new Color(255, 0, 255);
public static readonly AQUA = new Color(0, 255, 255);
public static readonly BLACK = new Color(0, 0, 0);
public static readonly RED = new Color(255, 0, 0);
public static readonly GREEN = new Color(0, 255, 0);
public static readonly BLUE = new Color(0, 0, 255);
public static readonly GRAY = new Color(128, 128, 128);
public constructor(public readonly red: number,
public readonly green: number,
public readonly blue: number,
public readonly alpha = 255) {
checkColorValue(red);
checkColorValue(green);
checkColorValue(blue);
checkColorValue(alpha);
}
public get rgb(): [number, number, number] {
return [this.red, this.green, this.blue];
}
public get rgbString(): string {
return `rgb(${this.red}, ${this.green}, ${this.blue})`;
}
public get rgba(): [number, number, number, number] {
return [this.red, this.green, this.blue, this.alpha];
}
public get hex(): string {
return rgb2hex(Math.floor(this.red), Math.floor(this.green), Math.floor(this.blue));
}
public get int(): number {
return rgb2int(this.red, this.green, this.blue);
}
public static fromHex(color: string): Color {
const value = hex2rgb(color);
return new Color(...value);
}
public static fromInt(color: number): Color {
const value = int2rgb(color);
return new Color(...value);
}
public normalized(): Color {
if (this.red > 1 || this.green > 1 || this.blue > 1 || this.alpha > 1) {
return new Color(this.red / 255, this.green / 255, this.blue / 255, this.alpha / 255);
}
return this;
}
}