UNPKG

the-world-engine

Version:

three.js based, unity like game engine for browser

193 lines (189 loc) 6.34 kB
import { Matrix3 } from "three/src/Three"; export class Color { Is; $s; Js; Ks; constructor(t = 0, s = 0, i = 0, e = 1) { if (1 < t) t = 1; else if (0 > t) t = 0; if (1 < s) s = 1; else if (0 > s) s = 0; if (1 < i) i = 1; else if (0 > i) i = 0; if (1 < e) e = 1; else if (0 > e) e = 0; this.Is = t; this.$s = s; this.Js = i; this.Ks = e; } set(t, s, i, e = 1) { if (1 < t) t = 1; else if (0 > t) t = 0; if (1 < s) s = 1; else if (0 > s) s = 0; if (1 < i) i = 1; else if (0 > i) i = 0; if (1 < e) e = 1; else if (0 > e) e = 0; this.Is = t; this.$s = s; this.Js = i; this.Ks = e; return this; } copy(t) { this.Is = t.r; this.$s = t.g; this.Js = t.b; this.Ks = t.a; return this; } clone() { return new Color(this.Is, this.$s, this.Js, this.Ks); } get r() { return this.Is; } set r(t) { if (1 < t) t = 1; else if (0 > t) t = 0; this.Is = t; } get g() { return this.$s; } set g(t) { if (1 < t) t = 1; else if (0 > t) t = 0; this.$s = t; } get b() { return this.Js; } set b(t) { if (1 < t) t = 1; else if (0 > t) t = 0; this.Js = t; } get a() { return this.Ks; } set a(t) { if (1 < t) t = 1; else if (0 > t) t = 0; this.Ks = t; } toString() { return "rgb(" + this.Is * 255 + ", " + this.$s * 255 + ", " + this.Js * 255 + ")"; } toStringWithAlpha() { return "rgba(" + this.Is * 255 + ", " + this.$s * 255 + ", " + this.Js * 255 + ", " + this.Ks + ")"; } toHex() { return "#" + ((Math.round(this.Is * 255) << 8 * 2) + (Math.round(this.$s * 255) << 8 * 1) + (Math.round(this.Js * 255) << 8 * 0)).toString(16).padStart(6, "0"); } toHexWithAlpha() { return "#" + ((Math.round(this.Is * 255) << 8 * 2) + (Math.round(this.$s * 255) << 8 * 1) + (Math.round(this.Js * 255) << 8 * 0)).toString(16).padStart(6, "0") + Math.round(this.Ks * 255).toString(16).padStart(2, "0"); } toArray() { return [ this.Is, this.$s, this.Js, this.Ks ]; } static fromHex(t) { const s = parseInt(t.slice(1, 3), 16) / 255; const i = parseInt(t.slice(3, 5), 16) / 255; const e = parseInt(t.slice(5, 7), 16) / 255; return new Color(s, i, e); } static fromArray(t) { return new Color(t[0], t[1], t[2], t[3]); } static fromString(t) { const s = t.match(/^rgba\((\d+),\s*(\d+),\s*(\d+),\s*(\d*(?:\.\d+)?)\)$/); if (s) { return new Color(parseInt(s[1]), parseInt(s[2]), parseInt(s[3]), parseFloat(s[4])); } return Color.fromHex(t); } hueRotate(t = 0, s) { t = t / 180 * Math.PI; const i = Math.sin(t); const e = Math.cos(t); this.multiplyMatrix((s ?? new Matrix3).set(.213 + e * .787 - i * .213, .715 - e * .715 - i * .715, .072 - e * .072 + i * .928, .213 - e * .213 + i * .143, .715 + e * .285 + i * .14, .072 - e * .072 - i * .283, .213 - e * .213 - i * .787, .715 - e * .715 + i * .715, .072 + e * .928 + i * .072)); } grayscale(t = 1, s) { this.multiplyMatrix((s ?? new Matrix3).set(.2126 + .7874 * (1 - t), .7152 - .7152 * (1 - t), .0722 - .0722 * (1 - t), .2126 - .2126 * (1 - t), .7152 + .2848 * (1 - t), .0722 - .0722 * (1 - t), .2126 - .2126 * (1 - t), .7152 - .7152 * (1 - t), .0722 + .9278 * (1 - t))); } sepia(t = 1, s) { this.multiplyMatrix((s ?? new Matrix3).set(.393 + .607 * (1 - t), .769 - .769 * (1 - t), .189 - .189 * (1 - t), .349 - .349 * (1 - t), .686 + .314 * (1 - t), .168 - .168 * (1 - t), .272 - .272 * (1 - t), .534 - .534 * (1 - t), .131 + .869 * (1 - t))); } saturate(t = 1, s) { this.multiplyMatrix((s ?? new Matrix3).set(.213 + .787 * t, .715 - .715 * t, .072 - .072 * t, .213 - .213 * t, .715 + .285 * t, .072 - .072 * t, .213 - .213 * t, .715 - .715 * t, .072 + .928 * t)); } brightness(t = 1) { this.linear(t); } contrast(t = 1) { this.linear(t, -(.5 * t) + .5); } invert(t = 1) { let s = t + this.Is * (1 - 2 * t); let i = t + this.$s * (1 - 2 * t); let e = t + this.Js * (1 - 2 * t); if (1 < s) s = 1; else if (s < 0) s = 0; if (1 < i) i = 1; else if (i < 0) i = 0; if (1 < e) e = 1; else if (e < 0) e = 0; this.Is = s; this.$s = i; this.Js = e; } multiplyMatrix(t) { const s = t.elements; let i = this.Is * s[0] + this.$s * s[1] + this.Js * s[2]; let e = this.Is * s[3] + this.$s * s[4] + this.Js * s[5]; let h = this.Is * s[6] + this.$s * s[7] + this.Js * s[8]; if (1 < i) i = 1; else if (i < 0) i = 0; if (1 < e) e = 1; else if (e < 0) e = 0; if (1 < h) h = 1; else if (h < 0) h = 0; this.Is = i; this.$s = e; this.Js = h; return this; } linear(t = 1, s = 0) { let i = this.Is * t + s; let e = this.$s * t + s; let h = this.Js * t + s; if (1 < i) i = 1; else if (i < 0) i = 0; if (1 < e) e = 1; else if (e < 0) e = 0; if (1 < h) h = 1; else if (h < 0) h = 0; this.Is = i; this.$s = e; this.Js = h; return this; } hsl() { const t = this.Is; const s = this.$s; const i = this.Js; const e = Math.max(t, s, i); const h = Math.min(t, s, i); let r; let a; const n = (e + h) / 2; if (e === h) { r = a = 0; } else { const l = e - h; a = n > .5 ? l / (2 - e - h) : l / (e + h); switch (e) { case t: r = (s - i) / l + (s < i ? 6 : 0); break; case s: r = (i - t) / l + 2; break; case i: r = (t - s) / l + 4; break; default: throw new Error("Unreachable"); } r /= 6; } return { h: r, s: a, l: n }; } }