UNPKG

spaceship-sprites

Version:

Spaceship Sprite is a random ship sprite generator in typescript.

134 lines (133 loc) 4.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const validator_1 = require("./validator"); class Color { constructor(r, g, b, a = 1.0) { this._color = [ Color.toPct(r, 'Red'), Color.toPct(g, 'Green'), Color.toPct(b, 'Blue'), Color.toPct(a, 'Alpha') ]; } copy() { return new Color(this._color[0], this._color[1], this._color[2], this._color[3]); } equals(other) { return (this.alpha === other.alpha && this.red === other.red && this.green === other.green && this.blue === other.blue); } equalsAny(others) { return others.some(color => { return this.equals(color); }); } static random(randomAlpha = true) { return new Color(Math.random(), Math.random(), Math.random(), randomAlpha ? Math.random() : 1); } get red() { return this._color[0]; } get green() { return this._color[1]; } get blue() { return this._color[2]; } get alpha() { return this._color[3]; } set red(value) { this._color[0] = Color.toPct(value, 'Red'); } set green(value) { this._color[1] = Color.toPct(value, 'Green'); } set blue(value) { this._color[2] = Color.toPct(value, 'Blue'); } set alpha(value) { this._color[3] = Color.toPct(value, 'Alpha'); } get redByte() { return this._color[0] * 255; } get greenByte() { return this._color[1] * 255; } get blueByte() { return this._color[2] * 255; } get alphaByte() { return this._color[3] * 255; } toArray() { return [...this._color]; } toByteArray() { return [this.redByte, this.greenByte, this.blueByte, this.alphaByte]; } toInt() { let color = this.toByteArray(); return ((((color[3] * 255) & 0xff) << 24) | (((color[0] * 255) & 0xff) << 16) | (((color[1] * 255) & 0xff) << 8) | ((color[2] & 0xff) << 0)); } toHexa() { return (this.alphaByte.toString(16) + this.redByte.toString(16) + this.greenByte.toString(16) + this.blueByte.toString(16)); } toRgb() { return `rgb(${Math.round(this.red * 255)}, ${Math.round(this.green * 255)}, ${Math.round(this.blue * 255)})`; } toRgba() { return `rgba(${Math.round(this.red * 255)}, ${Math.round(this.green * 255)}, ${Math.round(this.blue * 255)}, ${this.alpha})`; } static fromInt(colorValue) { validator_1.default.positiveInteger(colorValue, 'colorValue'); let a = (colorValue & 0xff000000) >>> 24; let r = (colorValue & 0x00ff0000) >>> 16; let g = (colorValue & 0x0000ff00) >>> 8; let b = (colorValue & 0x000000ff) >>> 0; return new Color(r, g, b, a); } static fromHexa(colorString) { if (colorString.startsWith('#')) colorString = colorString.slice(1); if (!colorString.startsWith('0x')) colorString = '0x' + colorString; return Color.fromInt(parseInt(colorString)); } static withinPct(n) { return n >= 0.0 && n <= 1.0; } static withinByte(n) { return n >= 0.0 && n <= 255.0; } static toPct(n, valueName = 'value') { if (Color.withinPct(n)) return n; else if (Color.withinByte(n)) return n / 255; else throw new Error(`Invalid ${valueName}: ${n} when converting color. Value N must be either 0 <= N <= 1.0 or 0 <= N <= 255.`); } mix(other) { return new Color((this.red + other.red) / 2, (this.green + other.green) / 2, (this.blue + other.blue) / 2, (this.alpha + other.alpha) / 2); } mixWeighed(other, w) { validator_1.default.percentage(w, 'Color Weight'); let w1 = 1 - w; let w2 = w; return new Color(this.red * w1 + other.red * w2, this.green * w1 + other.green * w2, this.blue * w1 + other.blue * w2, this.alpha * w1 + other.alpha * w2); } } Color.BLACK = new Color(0, 0, 0, 1); Color.WHITE = new Color(1, 1, 1, 1); Color.TRANSPARENT = new Color(0, 0, 0, 0); exports.default = Color;