arx-level-generator
Version:
A tool for creating Arx Fatalis maps
89 lines • 2.6 kB
JavaScript
import rgba from 'color-rgba';
import { MathUtils } from 'three';
import { percentOf } from './helpers.js';
export var Alpha;
(function (Alpha) {
Alpha[Alpha["Transparent"] = 0] = "Transparent";
Alpha[Alpha["Opaque"] = 1] = "Opaque";
})(Alpha || (Alpha = {}));
/**
* Three JS's color is not being used as it doesn't come with an alpha channel
*/
export class Color {
r;
g;
b;
a;
constructor(r, g, b, a = Alpha.Opaque) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
static fromCSS(color) {
const channels = rgba(color);
if (typeof channels === 'undefined') {
throw new Error(`failed to parse color "${color}"`);
}
const [r, g, b, a] = channels;
if (typeof r === 'undefined' || typeof g === 'undefined' || typeof b === 'undefined') {
throw new Error(`failed to parse color "${color}"`);
}
return new Color(r, g, b, a);
}
static fromArxColor({ r, g, b, a }) {
return new Color(r, g, b, a);
}
static fromThreeJsColor({ r, g, b }) {
return new Color(r * 255, g * 255, b * 255);
}
toArxColor() {
return { r: this.r, g: this.g, b: this.b, a: this.a };
}
toScriptColor() {
return `${this.r / 256} ${this.g / 256} ${this.b / 256}`;
}
clone() {
return new Color(this.r, this.g, this.b, this.a);
}
getHex() {
return (this.r << 16) + (this.g << 8) + this.b;
}
lighten(percent) {
const extra = percentOf(percent, 255);
this.r = MathUtils.clamp(this.r + extra, 0, 255);
this.g = MathUtils.clamp(this.g + extra, 0, 255);
this.b = MathUtils.clamp(this.b + extra, 0, 255);
return this;
}
darken(percent) {
const extra = percentOf(percent, 255);
this.r = MathUtils.clamp(this.r - extra, 0, 255);
this.g = MathUtils.clamp(this.g - extra, 0, 255);
this.b = MathUtils.clamp(this.b - extra, 0, 255);
return this;
}
// ----------------
static get red() {
return Color.fromCSS('red');
}
static get green() {
return Color.fromCSS('green');
}
static get blue() {
return Color.fromCSS('blue');
}
static get white() {
return Color.fromCSS('white');
}
static get black() {
return Color.fromCSS('black');
}
static get yellow() {
return Color.fromCSS('yellow');
}
static get transparent() {
return Color.fromCSS('transparent');
}
}
//# sourceMappingURL=Color.js.map