'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var enumAnsiCodes = require('@palett/enum-ansi-codes'); var utilAnsi = require('@palett/util-ansi'); require('@ject/oneself'); /** * * applicable for smaller number * @param {number} x * @returns {number} */ const round = x => x + (x > 0 ? 0.5 : -0.5) << 0; function hexAt(tx, i) { let n = tx.charCodeAt(i); return n >> 5 <= 1 ? n & 0xf : (n & 0x7) + 9; } const prolif = n => n << 4 | n; /** * * @param {number} n * @param {number} h * @param {number} a * @param {number} l * @returns {number} */ const hf = (n, h, a, l) => { const k = (n + h / 30) % 12; return l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); }; /** * @param {string} hex * @returns {number} */ function hexToInt(hex) { let lo = 0, hi = hex === null || hex === void 0 ? void 0 : hex.length; if (hi === 7) lo++, hi--; if (hi === 6) { const r = hexAt(hex, lo++) << 4 | hexAt(hex, lo++); const g = hexAt(hex, lo++) << 4 | hexAt(hex, lo++); const b = hexAt(hex, lo++) << 4 | hexAt(hex, lo++); return r << 16 | g << 8 | b; } if (hi === 4) lo++, hi--; if (hi === 3) { return prolif(hexAt(hex, lo++)) << 16 | prolif(hexAt(hex, lo++)) << 8 | prolif(hexAt(hex, lo++)); } return 0; } /** * * @param {number} h * @param {number} s * @param {number} l * @returns {number} */ function hslToInt([h, s, l]) { s /= 100, l /= 100; const a = s * Math.min(l, 1 - l), r = hf(0, h, a, l), g = hf(8, h, a, l), b = hf(4, h, a, l); return ((round(r * 0xFF) & 0xFF) << 16) + ((round(g * 0xFF) & 0xFF) << 8) + (round(b * 0xFF) & 0xFF); } class Dye { head; tail; constructor(head, tail) { this.head = head ?? ''; this.tail = tail ?? ''; } static of(head, tail) { return new Dye(head, tail); } static from(dye) { return new Dye(dye === null || dye === void 0 ? void 0 : dye.head, dye === null || dye === void 0 ? void 0 : dye.tail); } static prep(...style) { return new Dye().style(style); } static hex(color) { return HexDye.init(this).make(color); } static hsl(color) { return HslDye.init(this).make(color); } static int(color) { return IntDye.init(this).make(color); } static rgb(color) { return RgbDye.init(this).make(color); } load(r, g, b) { if (this.head.length) this.head += ';'; if (this.tail.length) this.tail += ';'; this.head += enumAnsiCodes.FORE_INI + utilAnsi.SC + r + utilAnsi.SC + g + utilAnsi.SC + b; this.tail += enumAnsiCodes.FORE_DEF; return this; } repl(r, g, b) { const ctx = { head: (this === null || this === void 0 ? void 0 : this.head) ?? '', tail: (this === null || this === void 0 ? void 0 : this.tail) ?? '' }; return load.call(ctx, r, g, b); } into([r, g, b]) { return this.repl(r, g, b); } draw(text) { return enumAnsiCodes.CSI + this.head + enumAnsiCodes.SGR + text + enumAnsiCodes.CSI + this.tail + enumAnsiCodes.SGR; } make(color) { return draw.bind(this.into(color)); } render(color, text) { return draw.call(this.into(color), text); } clear() { return this.head = '', this.tail = '', this; } slice() { return new Dye(this.head, this.tail); } /** @param {string[]} style */ style(style) { if (!(style !== null && style !== void 0 && style.length)) return this; if (this.head.length) this.head += ';'; if (this.tail.length) this.tail += ';'; for (let t of style) { const [c] = t; c === 'b' ? (this.head += enumAnsiCodes.BOL_ON, this.tail += enumAnsiCodes.BOL_OFF // BOLD ) : c === 'd' ? (this.head += enumAnsiCodes.DIM_ON, this.tail += enumAnsiCodes.DIM_OFF // DIM ) : c === 'i' && t[1] === 't' ? (this.head += enumAnsiCodes.ITA_ON, this.tail += enumAnsiCodes.ITA_OFF // ITALIC ) : c === 'u' ? (this.head += enumAnsiCodes.UND_ON, this.tail += enumAnsiCodes.UND_OFF // UNDERLINE ) : c === 'b' ? (this.head += enumAnsiCodes.BLI_ON, this.tail += enumAnsiCodes.BLI_OFF // BLINK ) : c === 'i' ? (this.head += enumAnsiCodes.INV_ON, this.tail += enumAnsiCodes.INV_OFF // INVERSE ) : c === 'h' ? (this.head += enumAnsiCodes.HID_ON, this.tail += enumAnsiCodes.HID_OFF // HIDE ) : c === 's' ? (this.head += enumAnsiCodes.CRO_ON, this.tail += enumAnsiCodes.CRO_OFF // STRIKE ) : void 0; } return this; } } const { load, repl, draw } = Dye.prototype; class HexDye extends Dye { constructor(h, t) { super(h, t); } static init(ctx) { return new HexDye(ctx === null || ctx === void 0 ? void 0 : ctx.head, ctx === null || ctx === void 0 ? void 0 : ctx.tail); } into(c) { return c = hexToInt(c), repl.call(this, c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF); } make(c) { return draw.bind(HexDye.prototype.into.call(this, c)); } render(c, tx) { return draw.call(HexDye.prototype.into.call(this, c), tx); } } class HslDye extends Dye { constructor(h, t) { super(h, t); } static init(ctx) { return new HslDye(ctx === null || ctx === void 0 ? void 0 : ctx.head, ctx === null || ctx === void 0 ? void 0 : ctx.tail); } into(c) { return c = hslToInt(c), repl.call(this, c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF); } make(c) { return draw.bind(HslDye.prototype.into.call(this, c)); } render(c, tx) { return draw.call(HslDye.prototype.into.call(this, c), tx); } } class IntDye extends Dye { constructor(h, t) { super(h, t); } static init(ctx) { return new IntDye(ctx === null || ctx === void 0 ? void 0 : ctx.head, ctx === null || ctx === void 0 ? void 0 : ctx.tail); } into(c) { return repl.call(this, c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF); } make(c) { return draw.bind(IntDye.prototype.into.call(this, c)); } render(c, tx) { return draw.call(IntDye.prototype.into.call(this, c), tx); } } class RgbDye extends Dye { constructor(h, t) { super(h, t); } static init(ctx) { return new RgbDye(ctx === null || ctx === void 0 ? void 0 : ctx.head, ctx === null || ctx === void 0 ? void 0 : ctx.tail); } into([r, g, b]) { return repl.call(this, r, g, b); } make(c) { return draw.bind(RgbDye.prototype.into.call(this, c)); } render(c, tx) { return draw.call(RgbDye.prototype.into.call(this, c), tx); } } class DyeFab { /** @type {Dye} */ base; constructor(dye) { this.base = dye; } static build(space, style) { return (DyeFab[space] ?? DyeFab.rgb).apply(null, style); } static prep(space, ...style) { return (DyeFab[space] ?? DyeFab.rgb).apply(null, style); } static hex(...style) { return new HexDye().style(style); } static hsl(...style) { return new HslDye().style(style); } static int(...style) { return new IntDye().style(style); } static rgb(...style) { return new RgbDye().style(style); } } exports.Dye = Dye; exports.DyeFab = DyeFab; exports.DyeFactory = DyeFab; exports.HexDye = HexDye; exports.HslDye = HslDye; exports.IntDye = IntDye; exports.RgbDye = RgbDye; exports.draw = draw; exports.load = load; exports.repl = repl;