53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
/* eslint-disable no-magic-numbers */
|
|
import { num_to_hex } from '@sapphirecode/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)}`;
|
|
}
|
|
}
|