const max = 255; const max2 = 255 * 2; class Color { r: number; g: number; b: number; constructor(hex) { this.r = hex >> 16 & max; this.g = hex >> 8 & max; this.b = hex & max; } mix(c, cross, max = 0): Color { if (max) { this.r = Math.floor(this.r + Math.max(-max, Math.min(max, (c.r - this.r) * cross))); this.g = Math.floor(this.g + Math.max(-max, Math.min(max, (c.g - this.g) * cross))); this.b = Math.floor(this.b + Math.max(-max, Math.min(max, (c.b - this.b) * cross))); } else { this.r = Math.floor(this.r + (c.r - this.r) * cross); this.g = Math.floor(this.g + (c.g - this.g) * cross); this.b = Math.floor(this.b + (c.b - this.b) * cross); } return this; }; randomize(p) { this.r = Math.max(0, Math.min(max, this.r + Math.floor(((Math.random() * max2 - max) * p)))); this.g = Math.max(0, Math.min(max, this.g + Math.floor(((Math.random() * max2 - max) * p)))); this.b = Math.max(0, Math.min(max, this.b + Math.floor(((Math.random() * max2 - max) * p)))); }; static mixHex(hex1, hex2, cross): number { let c1: Color = new Color(hex1); let c2: Color = new Color(hex2); return c1.mix(c2, cross).Hex; } get Hex(): number { return ((this.r) << 16 | (this.g) << 8 | (this.b)); } } export default Color;