"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.colorizeString = exports.rgbForKey = exports.hslForKey = exports.hslToRgb = void 0;
var joaat_1 = require("./joaat");
// https://stackoverflow.com/a/9493060
function hue2rgb(v1, v2, vH) {
    if (vH < 0)
        vH += 1;
    if (vH > 1)
        vH -= 1;
    if (vH < 1 / 6)
        return v1 + (v2 - v1) * 6 * vH;
    if (vH < 1 / 2)
        return v2;
    if (vH < 2 / 3)
        return v1 + (v2 - v1) * (2 / 3 - vH) * 6;
    return v1;
}
function hslToRgb(h, s, l) {
    var r, g, b;
    if (s == 0) {
        r = g = b = l; // achromatic
    }
    else {
        var v2 = l < 0.5
            ? l * (1 + s)
            : l + s - l * s;
        var v1 = 2 * l - v2;
        r = 255 * hue2rgb(v1, v2, h + 1 / 3);
        g = 255 * hue2rgb(v1, v2, h);
        b = 255 * hue2rgb(v1, v2, h - 1 / 3);
    }
    return [r | 0, g | 0, b | 0];
}
exports.hslToRgb = hslToRgb;
function hslForKey(key) {
    return [joaat_1.joaat(key) % 360, 80, 40];
}
exports.hslForKey = hslForKey;
function rgbForKey(key) {
    var hue = (joaat_1.joaat(key) % 360) / 360;
    return hslToRgb(hue, .8, .4);
}
exports.rgbForKey = rgbForKey;
var colorizeColorIndexRegexp = /\^([0-9])/g;
var colorizeRemnantsRegexp = /<span[^>]*><\/span[^>]*>/g;
function colorizeString(msg) {
    var s = "<span>" + (msg.replace(colorizeColorIndexRegexp, function (str, color) { return "</span><span class=\"color-" + color + "\">"; })) + "</span>";
    return s.replace(colorizeRemnantsRegexp, '');
}
exports.colorizeString = colorizeString;
//# sourceMappingURL=color.js.map