Timo Hocker 3b67053b3e styles
2020-04-24 17:01:26 +02:00

53 lines
1.5 KiB
TypeScript

/* eslint-disable no-magic-numbers */
import { num_to_hex } from '@scode/encoding-helper';
export class Color {
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 yellow = new Color (255, 255, 0);
public static readonly blue = new Color (0, 0, 255);
public static readonly magenta = new Color (255, 0, 255);
public static readonly cyan = new Color (0, 255, 255);
public static readonly white = new Color (255, 255, 255);
public static readonly transparent = new Color (0, 0, 0, 0);
public static readonly gray = new Color (128, 128, 128);
private red: number;
private green: number;
private blue: number;
private alpha: number;
private check_range (n: number): void {
if (n < 0 || n > 255)
throw new Error ('number out of range');
}
public constructor (red: number, green: number, blue: number, alpha = 255) {
this.check_range (red);
this.check_range (green);
this.check_range (blue);
this.check_range (alpha);
this.red = red;
this.green = green;
this.blue = blue;
this.alpha = alpha;
}
// eslint-disable-next-line @typescript-eslint/naming-convention
public toString (): string {
return `#${num_to_hex (
this.red,
2
)}${num_to_hex (
this.green,
2
)}${num_to_hex (
this.blue,
2
)}${this.alpha === 255
? ''
: num_to_hex (this.alpha, 2)}`;
}
}