UNPKG

@runejs/common

Version:

Common logging, networking, compression, and other miscellaneous functionality for RuneJS.

76 lines 2.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HCL = exports.HCLValues = void 0; const color_1 = require("./color"); const rgb_1 = require("./rgb"); const util_1 = require("../util"); class HCLValues extends color_1.Color { } exports.HCLValues = HCLValues; /** * A color within the HCL color space. */ class HCL extends HCLValues { constructor(arg0, chroma, luminance, alpha = 255) { super('hcl'); let hue = arg0; if (chroma === undefined && luminance === undefined) { this.rgb = typeof arg0 === 'number' ? new rgb_1.RGB(arg0) : arg0; const { h, c, l } = HCL.fromRgb(this.rgb); hue = h; chroma = c; luminance = l; alpha = this.rgb.alpha; } this.h = hue; this.c = chroma; this.l = luminance; this.alpha = alpha !== null && alpha !== void 0 ? alpha : 255; } /** * Converts the given RGB(A) color into the Hue-Chroma-Luminance (HCL) format. * @param rgb The RGB(A) color to convert into HCL. */ static fromRgb(rgb) { if (rgb.isPureBlack) { return { h: color_1.constants.black_hue, c: 0, l: 0 }; } const { r, g, b } = rgb.decimalValues; let { max, min } = rgb; max /= 255; min /= 255; const h = rgb.calculateHue(); let c, l; if (max === 0) { c = 0; l = 0; } else { let alpha = (min / max) / 100; let q = Math.exp(alpha * 3); let rg = r - g; let gb = g - b; let br = b - r; l = ((q * max) + ((1 - q) * min)) / 2; c = q * (Math.abs(rg) + Math.abs(gb) + Math.abs(br)) / 3; } return color_1.Color.values({ h, c: (0, util_1.fixedFloor)(c * 100, 0), l: (0, util_1.fixedFloor)(l * 100, 0) }); } /** * Checks to see if the given color matches this color. * @param other The new color to check against the current color. */ equals(other) { return this.h === other.h && this.c === other.c && this.l === other.l; } toString() { return `HCL(A) ( ${(0, util_1.pad)(this.h, 3)}, ${(0, util_1.pad)(this.c, 3)}%, ` + `${(0, util_1.pad)(this.l, 3)}%, ${(0, util_1.pad)(this.alpha, 3)} )`; } } exports.HCL = HCL; //# sourceMappingURL=hcl.js.map