"use strict";
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.limits = exports.analyze = exports.deltaE = exports.distance = exports.contrast = exports.scales = exports.Scale = exports.scale = exports.blend = exports.bezier = exports.average = exports.xyz = exports.rgb = exports.num = exports.lab = exports.kelvin = exports.hsv = exports.hsl = exports.hsi = exports.lch = exports.hcg = exports.gl = exports.cmyk = exports.css = exports.mix = exports.random = exports.CubeHelix = exports.cubehelix = exports.w3cx11 = exports.brewer = exports.white = exports.black = exports.Color = exports.color = void 0;
var abs = Math.abs, atan2 = Math.atan2, cos = Math.cos, floor = Math.floor, log = Math.log, min = Math.min, max = Math.max, round = Math.round, sign = Math.sign, sin = Math.sin, sqrt = Math.sqrt, cbrt = Math.cbrt, PI = Math.PI, hypot = Math.hypot;
function lerp(a, b, f) {
    return a + (b - a) * f;
}
function lerpInv(a, b, f) {
    return (f - a) / (b - a);
}
function clamp(x, min, max) {
    if (min === void 0) { min = 0; }
    if (max === void 0) { max = 1; }
    return x < min ? min : x > max ? max : x;
}
function newtonIterate1d(f, xStart, max_steps, eps) {
    if (eps === void 0) { eps = 1e-8; }
    var x = xStart, fx;
    while (max_steps-- && abs((fx = f(x))) > eps) {
        var dfdx = (f(x + eps) - fx) / eps;
        console.log("fx / dfdx", fx / dfdx, "fx", fx, "x", x);
        x = x - fx / dfdx;
    }
    return x;
}
function bisect(f, a, b, steps) {
    var fA = f(a);
    while (steps--) {
        var c = (a + b) / 2;
        var fC = f(c);
        if (sign(fA) == sign(fC)) {
            a = c;
            fA = fC;
        }
        else {
            b = c;
        }
    }
    return (a + b) / 2;
}
var TWOPI = 2 * PI;
var DEG2RAD = PI / 180;
var RAD2DEG = 180 / PI;
function color() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    if (args[0] instanceof Color) {
        return args[0];
    }
    if (args.length > 1 && "string" == typeof args[args.length - 1]) {
        return guess(args.slice(0, args.length - 1), args[args.length - 1]);
    }
    else if (Array.isArray(args[0])) {
        return guess(args[0]);
    }
    else {
        return guess(args);
    }
}
exports.color = color;
var Color = (function () {
    function Color(r, g, b, a) {
        if (a === void 0) { a = 1; }
        this.r = r;
        this.g = g;
        this.b = b;
        this.a = a;
    }
    Color.prototype.mix = function (col2, f, m) {
        if (m === void 0) { m = "rgb"; }
        return mix(this, col2, f, m);
    };
    Color.prototype.rgb = function (doRound, clamp_) {
        if (doRound === void 0) { doRound = true; }
        if (clamp_ === void 0) { clamp_ = true; }
        var f = function (t) {
            if (doRound)
                t = round(t);
            if (clamp_)
                t = clamp(t, 0, 255);
            return t;
        };
        var _a = this, r = _a.r, g = _a.g, b = _a.b;
        return [f(r), f(g), f(b)];
    };
    Color.prototype.rgba = function (doRound, clamp_) {
        if (doRound === void 0) { doRound = true; }
        if (clamp_ === void 0) { clamp_ = true; }
        var f = function (t) {
            if (doRound)
                t = round(t);
            if (clamp_)
                t = clamp(t, 0, 255);
            return t;
        };
        var _a = this, r = _a.r, g = _a.g, b = _a.b, a = _a.a;
        return [f(r), f(g), f(b), a];
    };
    Color.prototype.hex = function (mode) {
        if (mode === void 0) { mode = "rgb"; }
        var _a = this, r = _a.r, g = _a.g, b = _a.b, a = _a.a;
        return rgb2hex(r, g, b, a, mode);
    };
    Color.prototype.hsl = function () {
        var _a = this, r = _a.r, g = _a.g, b = _a.b;
        return rgb2hsl(r, g, b);
    };
    Color.prototype.hsv = function () {
        var _a = this, r = _a.r, g = _a.g, b = _a.b;
        return rgb2hsv(r, g, b);
    };
    Color.prototype.hcg = function () {
        var _a = this, r = _a.r, g = _a.g, b = _a.b;
        return rgb2hcg(r, g, b);
    };
    Color.prototype.css = function (mode) {
        if (mode === void 0) { mode = "rgb"; }
        if ("rgb" == mode) {
            var _a = this, r = _a.r, g = _a.g, b = _a.b, a = _a.a;
            return rgb2css(r, g, b, a);
        }
        else if ("hsl" == mode) {
            return hsl2css(this.hsl(), this.alpha());
        }
        else {
            throw new Error();
        }
    };
    Color.prototype.name = function (closest) {
        if (closest === void 0) { closest = false; }
        var thisNum = this.num();
        var name = Object.keys(exports.w3cx11).find(function (name) { return exports.w3cx11[name] == thisNum; });
        if (!name && closest) {
            var _a = __read(this.lab(), 3), thisLStar_1 = _a[0], thisAStar_1 = _a[1], thisBStar_1 = _a[2];
            return withMax(Object.keys(exports.w3cx11), function (name) {
                var _a = __read(num(exports.w3cx11[name]).lab(), 3), lStar = _a[0], aStar = _a[1], bStar = _a[2];
                return -hypot(thisLStar_1 - lStar, thisAStar_1 - aStar, thisBStar_1 - bStar);
            });
        }
        return name;
    };
    Color.prototype.cmyk = function () {
        var _a = this, r = _a.r, g = _a.g, b = _a.b;
        return rgb2cmyk(r, g, b);
    };
    Color.prototype.gl = function () {
        var _a = this, r = _a.r, g = _a.g, b = _a.b, a = _a.a;
        return [r / 255, g / 255, b / 255, a];
    };
    Color.prototype.luminance = function (lum1) {
        var _a = this, r = _a.r, g = _a.g, b = _a.b, a = _a.a;
        var _b = __read(rgb2xyz(r, g, b), 2), Y = _b[1];
        if (undefined === lum1) {
            return Y;
        }
        var inverseLerp = function (a, b, val) { return (val - a) / (b - a); };
        if (lum1 > Y) {
            return mix(this, exports.white, inverseLerp(Y, 1, lum1), "xyz").alpha(a);
        }
        else {
            return mix(exports.black, this, inverseLerp(0, Y, lum1), "xyz").alpha(a);
        }
    };
    Color.prototype.temperature = function () {
        var _a = this, r = _a.r, g = _a.g, b = _a.b;
        return rgb2kelvin(r, g, b);
    };
    Color.prototype.set = function (modeAndChannel, value) {
        var _a = __read(modeAndChannel.split("."), 2), mode = _a[0], channel = _a[1];
        var src = this[mode]();
        var i = mode.indexOf(channel);
        if (-1 == i)
            throw new Error("invalid channel");
        src[i] = "number" == typeof value ? value : value(src[i]);
        return color(src, mode).alpha(this.a);
    };
    Color.prototype.clipped = function () {
        var _a = this, r = _a.r, g = _a.g, b = _a.b;
        return !(0 <= r && r <= 255 && 0 <= g && g <= 255 && 0 <= b && b <= 255);
    };
    Color.prototype.textColor = function () {
        return this.luminance() > 0.5 ? exports.black : exports.white;
    };
    Color.prototype.alpha = function (alpha1) {
        if (undefined === alpha1) {
            return this.a;
        }
        var _a = this, r = _a.r, g = _a.g, b = _a.b;
        return rgb(r, g, b, alpha1);
    };
    Color.prototype.darker = function (amount) {
        if (amount === void 0) { amount = 1; }
        var _a = __read(this.lab(), 3), l = _a[0], a = _a[1], b = _a[2];
        return lab(l - LAB_Kn * amount, a, b, this.alpha());
    };
    Color.prototype.brighter = function (amount) {
        if (amount === void 0) { amount = 1; }
        return this.darker(-amount);
    };
    Color.prototype.saturate = function (amount) {
        if (amount === void 0) { amount = 1; }
        var _a = __read(this.lch(), 3), l = _a[0], c = _a[1], h = _a[2];
        return lch(l, max(0, c + amount * LAB_Kn), h, this.alpha());
    };
    Color.prototype.desaturate = function (amount) {
        if (amount === void 0) { amount = 1; }
        return this.saturate(-amount);
    };
    Color.prototype.premultiplied = function () {
        var _a = this, r = _a.r, g = _a.g, b = _a.b, a = _a.a;
        return rgb(r * a, g * a, b * a, a);
    };
    Color.prototype.hsi = function () {
        var _a = this, r = _a.r, g = _a.g, b = _a.b;
        return rgb2hsi(r, g, b);
    };
    Color.prototype.lab = function () {
        var _a = this, r = _a.r, g = _a.g, b = _a.b;
        return rgb2lab(r, g, b);
    };
    Color.prototype.num = function (mode) {
        if (mode === void 0) { mode = "rgb"; }
        var _a = this, r = _a.r, g = _a.g, b = _a.b, a = _a.a;
        return rgb2num(r, g, b, a, mode);
    };
    Color.prototype.lch = function () {
        var _a = this, r = _a.r, g = _a.g, b = _a.b;
        return rgb2lch(r, g, b);
    };
    Color.prototype.xyz = function () {
        var _a = this, r = _a.r, g = _a.g, b = _a.b;
        return rgb2xyz(r, g, b);
    };
    Color.prototype.equals = function (color) {
        var _a = this, r = _a.r, g = _a.g, b = _a.b, a = _a.a;
        var r2 = color.r, g2 = color.g, b2 = color.b, a2 = color.a;
        return r == r2 && g == g2 && b == b2 && a == a2;
    };
    Color.prototype.hashCode = function () {
        return this.num("rgba");
    };
    Color.prototype.toSource = function () {
        var _a = this, r = _a.r, g = _a.g, b = _a.b, a = _a.a;
        return "chroma.rgb(" + r + ", " + g + ", " + b + (a === 1 ? ")" : ", " + a + ")");
    };
    return Color;
}());
exports.Color = Color;
Color.prototype.toString = Color.prototype.css;
Color.prototype.kelvin = Color.prototype.temperature;
exports.black = new Color(0, 0, 0, 1);
exports.white = new Color(255, 255, 255, 1);
exports.brewer = {
    OrRd: [0xfff7ec, 0xfee8c8, 0xfdd49e, 0xfdbb84, 0xfc8d59, 0xef6548, 0xd7301f, 0xb30000, 0x7f0000],
    PuBu: [0xfff7fb, 0xece7f2, 0xd0d1e6, 0xa6bddb, 0x74a9cf, 0x3690c0, 0x0570b0, 0x045a8d, 0x023858],
    BuPu: [0xf7fcfd, 0xe0ecf4, 0xbfd3e6, 0x9ebcda, 0x8c96c6, 0x8c6bb1, 0x88419d, 0x810f7c, 0x4d004b],
    Oranges: [0xfff5eb, 0xfee6ce, 0xfdd0a2, 0xfdae6b, 0xfd8d3c, 0xf16913, 0xd94801, 0xa63603, 0x7f2704],
    BuGn: [0xf7fcfd, 0xe5f5f9, 0xccece6, 0x99d8c9, 0x66c2a4, 0x41ae76, 0x238b45, 0x006d2c, 0x00441b],
    YlOrBr: [0xffffe5, 0xfff7bc, 0xfee391, 0xfec44f, 0xfe9929, 0xec7014, 0xcc4c02, 0x993404, 0x662506],
    YlGn: [0xffffe5, 0xf7fcb9, 0xd9f0a3, 0xaddd8e, 0x78c679, 0x41ab5d, 0x238443, 0x006837, 0x004529],
    Reds: [0xfff5f0, 0xfee0d2, 0xfcbba1, 0xfc9272, 0xfb6a4a, 0xef3b2c, 0xcb181d, 0xa50f15, 0x67000d],
    RdPu: [0xfff7f3, 0xfde0dd, 0xfcc5c0, 0xfa9fb5, 0xf768a1, 0xdd3497, 0xae017e, 0x7a0177, 0x49006a],
    Greens: [0xf7fcf5, 0xe5f5e0, 0xc7e9c0, 0xa1d99b, 0x74c476, 0x41ab5d, 0x238b45, 0x006d2c, 0x00441b],
    YlGnBu: [0xffffd9, 0xedf8b1, 0xc7e9b4, 0x7fcdbb, 0x41b6c4, 0x1d91c0, 0x225ea8, 0x253494, 0x081d58],
    Purples: [0xfcfbfd, 0xefedf5, 0xdadaeb, 0xbcbddc, 0x9e9ac8, 0x807dba, 0x6a51a3, 0x54278f, 0x3f007d],
    GnBu: [0xf7fcf0, 0xe0f3db, 0xccebc5, 0xa8ddb5, 0x7bccc4, 0x4eb3d3, 0x2b8cbe, 0x0868ac, 0x084081],
    Greys: [0xffffff, 0xf0f0f0, 0xd9d9d9, 0xbdbdbd, 0x969696, 0x737373, 0x525252, 0x252525, 0x000000],
    YlOrRd: [0xffffcc, 0xffeda0, 0xfed976, 0xfeb24c, 0xfd8d3c, 0xfc4e2a, 0xe31a1c, 0xbd0026, 0x800026],
    PuRd: [0xf7f4f9, 0xe7e1ef, 0xd4b9da, 0xc994c7, 0xdf65b0, 0xe7298a, 0xce1256, 0x980043, 0x67001f],
    Blues: [0xf7fbff, 0xdeebf7, 0xc6dbef, 0x9ecae1, 0x6baed6, 0x4292c6, 0x2171b5, 0x08519c, 0x08306b],
    PuBuGn: [0xfff7fb, 0xece2f0, 0xd0d1e6, 0xa6bddb, 0x67a9cf, 0x3690c0, 0x02818a, 0x016c59, 0x014636],
    Viridis: [0x440154, 0x482777, 0x3f4a8a, 0x31678e, 0x26838f, 0x1f9d8a, 0x6cce5a, 0xb6de2b, 0xfee825],
    Spectral: [
        0x9e0142,
        0xd53e4f,
        0xf46d43,
        0xfdae61,
        0xfee08b,
        0xffffbf,
        0xe6f598,
        0xabdda4,
        0x66c2a5,
        0x3288bd,
        0x5e4fa2,
    ],
    RdYlGn: [
        0xa50026,
        0xd73027,
        0xf46d43,
        0xfdae61,
        0xfee08b,
        0xffffbf,
        0xd9ef8b,
        0xa6d96a,
        0x66bd63,
        0x1a9850,
        0x006837,
    ],
    RdBu: [
        0x67001f,
        0xb2182b,
        0xd6604d,
        0xf4a582,
        0xfddbc7,
        0xf7f7f7,
        0xd1e5f0,
        0x92c5de,
        0x4393c3,
        0x2166ac,
        0x053061,
    ],
    PiYG: [
        0x8e0152,
        0xc51b7d,
        0xde77ae,
        0xf1b6da,
        0xfde0ef,
        0xf7f7f7,
        0xe6f5d0,
        0xb8e186,
        0x7fbc41,
        0x4d9221,
        0x276419,
    ],
    PRGn: [
        0x40004b,
        0x762a83,
        0x9970ab,
        0xc2a5cf,
        0xe7d4e8,
        0xf7f7f7,
        0xd9f0d3,
        0xa6dba0,
        0x5aae61,
        0x1b7837,
        0x00441b,
    ],
    RdYlBu: [
        0xa50026,
        0xd73027,
        0xf46d43,
        0xfdae61,
        0xfee090,
        0xffffbf,
        0xe0f3f8,
        0xabd9e9,
        0x74add1,
        0x4575b4,
        0x313695,
    ],
    BrBG: [
        0x543005,
        0x8c510a,
        0xbf812d,
        0xdfc27d,
        0xf6e8c3,
        0xf5f5f5,
        0xc7eae5,
        0x80cdc1,
        0x35978f,
        0x01665e,
        0x003c30,
    ],
    RdGy: [
        0x67001f,
        0xb2182b,
        0xd6604d,
        0xf4a582,
        0xfddbc7,
        0xffffff,
        0xe0e0e0,
        0xbababa,
        0x878787,
        0x4d4d4d,
        0x1a1a1a,
    ],
    PuOr: [
        0x7f3b08,
        0xb35806,
        0xe08214,
        0xfdb863,
        0xfee0b6,
        0xf7f7f7,
        0xd8daeb,
        0xb2abd2,
        0x8073ac,
        0x542788,
        0x2d004b,
    ],
    Set2: [0x66c2a5, 0xfc8d62, 0x8da0cb, 0xe78ac3, 0xa6d854, 0xffd92f, 0xe5c494, 0xb3b3b3],
    Accent: [0x7fc97f, 0xbeaed4, 0xfdc086, 0xffff99, 0x386cb0, 0xf0027f, 0xbf5b17, 0x666666],
    Set1: [0xe41a1c, 0x377eb8, 0x4daf4a, 0x984ea3, 0xff7f00, 0xffff33, 0xa65628, 0xf781bf, 0x999999],
    Set3: [
        0x8dd3c7,
        0xffffb3,
        0xbebada,
        0xfb8072,
        0x80b1d3,
        0xfdb462,
        0xb3de69,
        0xfccde5,
        0xd9d9d9,
        0xbc80bd,
        0xccebc5,
        0xffed6f,
    ],
    Dark2: [0x1b9e77, 0xd95f02, 0x7570b3, 0xe7298a, 0x66a61e, 0xe6ab02, 0xa6761d, 0x666666],
    Paired: [
        0xa6cee3,
        0x1f78b4,
        0xb2df8a,
        0x33a02c,
        0xfb9a99,
        0xe31a1c,
        0xfdbf6f,
        0xff7f00,
        0xcab2d6,
        0x6a3d9a,
        0xffff99,
        0xb15928,
    ],
    Pastel2: [0xb3e2cd, 0xfdcdac, 0xcbd5e8, 0xf4cae4, 0xe6f5c9, 0xfff2ae, 0xf1e2cc, 0xcccccc],
    Pastel1: [0xfbb4ae, 0xb3cde3, 0xccebc5, 0xdecbe4, 0xfed9a6, 0xffffcc, 0xe5d8bd, 0xfddaec, 0xf2f2f2],
};
exports.w3cx11 = {
    aliceblue: 0xf0f8ff,
    antiquewhite: 0xfaebd7,
    aqua: 0x00ffff,
    aquamarine: 0x7fffd4,
    azure: 0xf0ffff,
    beige: 0xf5f5dc,
    bisque: 0xffe4c4,
    black: 0x000000,
    blanchedalmond: 0xffebcd,
    blue: 0x0000ff,
    blueviolet: 0x8a2be2,
    brown: 0xa52a2a,
    burlywood: 0xdeb887,
    cadetblue: 0x5f9ea0,
    chartreuse: 0x7fff00,
    chocolate: 0xd2691e,
    coral: 0xff7f50,
    cornflower: 0x6495ed,
    cornflowerblue: 0x6495ed,
    cornsilk: 0xfff8dc,
    crimson: 0xdc143c,
    cyan: 0x00ffff,
    darkblue: 0x00008b,
    darkcyan: 0x008b8b,
    darkgoldenrod: 0xb8860b,
    darkgray: 0xa9a9a9,
    darkgreen: 0x006400,
    darkgrey: 0xa9a9a9,
    darkkhaki: 0xbdb76b,
    darkmagenta: 0x8b008b,
    darkolivegreen: 0x556b2f,
    darkorange: 0xff8c00,
    darkorchid: 0x9932cc,
    darkred: 0x8b0000,
    darksalmon: 0xe9967a,
    darkseagreen: 0x8fbc8f,
    darkslateblue: 0x483d8b,
    darkslategray: 0x2f4f4f,
    darkslategrey: 0x2f4f4f,
    darkturquoise: 0x00ced1,
    darkviolet: 0x9400d3,
    deeppink: 0xff1493,
    deepskyblue: 0x00bfff,
    dimgray: 0x696969,
    dimgrey: 0x696969,
    dodgerblue: 0x1e90ff,
    firebrick: 0xb22222,
    floralwhite: 0xfffaf0,
    forestgreen: 0x228b22,
    fuchsia: 0xff00ff,
    gainsboro: 0xdcdcdc,
    ghostwhite: 0xf8f8ff,
    gold: 0xffd700,
    goldenrod: 0xdaa520,
    gray: 0x808080,
    green: 0x008000,
    greenyellow: 0xadff2f,
    grey: 0x808080,
    honeydew: 0xf0fff0,
    hotpink: 0xff69b4,
    indianred: 0xcd5c5c,
    indigo: 0x4b0082,
    ivory: 0xfffff0,
    khaki: 0xf0e68c,
    laserlemon: 0xffff54,
    lavender: 0xe6e6fa,
    lavenderblush: 0xfff0f5,
    lawngreen: 0x7cfc00,
    lemonchiffon: 0xfffacd,
    lightblue: 0xadd8e6,
    lightcoral: 0xf08080,
    lightcyan: 0xe0ffff,
    lightgoldenrod: 0xfafad2,
    lightgoldenrodyellow: 0xfafad2,
    lightgray: 0xd3d3d3,
    lightgreen: 0x90ee90,
    lightgrey: 0xd3d3d3,
    lightpink: 0xffb6c1,
    lightsalmon: 0xffa07a,
    lightseagreen: 0x20b2aa,
    lightskyblue: 0x87cefa,
    lightslategray: 0x778899,
    lightslategrey: 0x778899,
    lightsteelblue: 0xb0c4de,
    lightyellow: 0xffffe0,
    lime: 0x00ff00,
    limegreen: 0x32cd32,
    linen: 0xfaf0e6,
    magenta: 0xff00ff,
    maroon: 0x800000,
    maroon2: 0x7f0000,
    maroon3: 0xb03060,
    mediumaquamarine: 0x66cdaa,
    mediumblue: 0x0000cd,
    mediumorchid: 0xba55d3,
    mediumpurple: 0x9370db,
    mediumseagreen: 0x3cb371,
    mediumslateblue: 0x7b68ee,
    mediumspringgreen: 0x00fa9a,
    mediumturquoise: 0x48d1cc,
    mediumvioletred: 0xc71585,
    midnightblue: 0x191970,
    mintcream: 0xf5fffa,
    mistyrose: 0xffe4e1,
    moccasin: 0xffe4b5,
    navajowhite: 0xffdead,
    navy: 0x000080,
    oldlace: 0xfdf5e6,
    olive: 0x808000,
    olivedrab: 0x6b8e23,
    orange: 0xffa500,
    orangered: 0xff4500,
    orchid: 0xda70d6,
    palegoldenrod: 0xeee8aa,
    palegreen: 0x98fb98,
    paleturquoise: 0xafeeee,
    palevioletred: 0xdb7093,
    papayawhip: 0xffefd5,
    peachpuff: 0xffdab9,
    peru: 0xcd853f,
    pink: 0xffc0cb,
    plum: 0xdda0dd,
    powderblue: 0xb0e0e6,
    purple: 0x800080,
    purple2: 0x7f007f,
    purple3: 0xa020f0,
    rebeccapurple: 0x663399,
    red: 0xff0000,
    rosybrown: 0xbc8f8f,
    royalblue: 0x4169e1,
    saddlebrown: 0x8b4513,
    salmon: 0xfa8072,
    sandybrown: 0xf4a460,
    seagreen: 0x2e8b57,
    seashell: 0xfff5ee,
    sienna: 0xa0522d,
    silver: 0xc0c0c0,
    skyblue: 0x87ceeb,
    slateblue: 0x6a5acd,
    slategray: 0x708090,
    slategrey: 0x708090,
    snow: 0xfffafa,
    springgreen: 0x00ff7f,
    steelblue: 0x4682b4,
    tan: 0xd2b48c,
    teal: 0x008080,
    thistle: 0xd8bfd8,
    tomato: 0xff6347,
    turquoise: 0x40e0d0,
    violet: 0xee82ee,
    wheat: 0xf5deb3,
    white: 0xffffff,
    whitesmoke: 0xf5f5f5,
    yellow: 0xffff00,
    yellowgreen: 0x9acd32,
};
function cubehelix(start, rotations, hue, gamma, lightness) {
    if (start === void 0) { start = 300; }
    if (rotations === void 0) { rotations = -1.5; }
    if (hue === void 0) { hue = 1; }
    if (gamma === void 0) { gamma = 1; }
    if (lightness === void 0) { lightness = [0, 1]; }
    var f = (function (t) { return f.at(t); });
    Object.getOwnPropertyNames(CubeHelix.prototype).forEach(function (key) { return (f[key] = CubeHelix.prototype[key]); });
    f.start(start);
    f.rotations(rotations);
    f.hue(hue);
    f.gamma(gamma);
    f.lightness(lightness);
    return f;
}
exports.cubehelix = cubehelix;
var CubeHelix = (function () {
    function CubeHelix() {
    }
    CubeHelix.prototype.start = function (s) {
        if (undefined === s) {
            return this._start;
        }
        this._start = s;
        return this;
    };
    CubeHelix.prototype.rotations = function (r) {
        if (undefined === r) {
            return this._rotations;
        }
        this._rotations = r;
        return this;
    };
    CubeHelix.prototype.gamma = function (g) {
        if (undefined === g) {
            return this._gamma;
        }
        this._gamma = g;
        return this;
    };
    CubeHelix.prototype.hue = function (h) {
        if (undefined === h) {
            return this._hue;
        }
        this._hue = Array.isArray(h) ? h : [h, h];
        return this;
    };
    CubeHelix.prototype.lightness = function (h) {
        if (undefined === h) {
            return this._lightness;
        }
        this._lightness = Array.isArray(h) ? h : [h, h];
        return this;
    };
    CubeHelix.prototype.scale = function () {
        return scale(this);
    };
    CubeHelix.prototype.at = function (fract) {
        var a = TWOPI * ((this._start + 120) / 360 + this._rotations * fract);
        var l = Math.pow(lerp(this._lightness[0], this._lightness[1], fract), this._gamma);
        var h = lerp(this._hue[0], this._hue[1], fract);
        var amp = (h * l * (1 - l)) / 2;
        var cos_a = cos(a);
        var sin_a = sin(a);
        var r = l + amp * (-0.14861 * cos_a + 1.78277 * sin_a);
        var g = l + amp * (-0.29227 * cos_a - 0.90649 * sin_a);
        var b = l + amp * (+1.97294 * cos_a);
        return rgb([r * 255, g * 255, b * 255, 1]);
    };
    return CubeHelix;
}());
exports.CubeHelix = CubeHelix;
function random(randomSource) {
    if (randomSource === void 0) { randomSource = Math.random; }
    return num((randomSource() * 16777216) | 0);
}
exports.random = random;
function mix(col1, col2, f, m) {
    if (f === void 0) { f = 0.5; }
    if (m === void 0) { m = "rgb"; }
    var c1 = color(col1);
    var c2 = color(col2);
    var res = interpolators[m] && interpolators[m](c1, c2, f, m);
    if (!res) {
        throw new Error("color mode " + m + " is not supported");
    }
    return res.alpha(lerp(c1.alpha(), c2.alpha(), f));
}
exports.mix = mix;
function css(cssString) {
    var _a = __read(css2rgb(cssString), 4), r = _a[0], g = _a[1], b = _a[2], a = _a[3];
    return new Color(r, g, b, a);
}
exports.css = css;
function cmyk() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    return guess(args, "cmyk");
}
exports.cmyk = cmyk;
function gl() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    return guess(args, "gl");
}
exports.gl = gl;
function hcg() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    return guess(args, "hcg");
}
exports.hcg = hcg;
function lch() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    return guess(args, "lch");
}
exports.lch = lch;
function hsi() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    return guess(args, "hsi");
}
exports.hsi = hsi;
function hsl() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    return guess(args, "hsl");
}
exports.hsl = hsl;
function hsv() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    return guess(args, "hsv");
}
exports.hsv = hsv;
function kelvin(temperature) {
    var _a = __read(kelvin2rgb(temperature), 3), r = _a[0], g = _a[1], b = _a[2];
    return new Color(r, g, b);
}
exports.kelvin = kelvin;
function lab() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    return guess(args, "lab");
}
exports.lab = lab;
function num(num) {
    var _a = __read(num2rgb(num), 3), r = _a[0], g = _a[1], b = _a[2];
    return new Color(r, g, b);
}
exports.num = num;
function rgb() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    return guess(args, "rgb");
}
exports.rgb = rgb;
function xyz() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    return guess(args, "xyz");
}
exports.xyz = xyz;
function average(chromables, mode) {
    var e_1, _a, e_2, _b;
    if (mode === void 0) { mode = "rgb"; }
    var colors = chromables.map(function (c) { return color(c); });
    if (mode == "lrgb") {
        return _average_lrgb(colors);
    }
    if (mode == "num") {
        var numSum = 0, alphaSum_1 = 0;
        try {
            for (var colors_1 = __values(colors), colors_1_1 = colors_1.next(); !colors_1_1.done; colors_1_1 = colors_1.next()) {
                var col = colors_1_1.value;
                numSum += col.num();
                alphaSum_1 += col.alpha();
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (colors_1_1 && !colors_1_1.done && (_a = colors_1.return)) _a.call(colors_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return num(numSum / colors.length).alpha(alphaSum_1 / colors.length);
    }
    var xyz = [0, 0, 0];
    var dx = 0;
    var dy = 0;
    var alphaSum = 0;
    try {
        for (var colors_2 = __values(colors), colors_2_1 = colors_2.next(); !colors_2_1.done; colors_2_1 = colors_2.next()) {
            var c = colors_2_1.value;
            var xyz2 = c[mode]();
            alphaSum += c.alpha();
            for (var i = 0; i < xyz.length; i++) {
                if (mode.charAt(i) == "h") {
                    var A = xyz2[i] * DEG2RAD;
                    dx += cos(A);
                    dy += sin(A);
                }
                else {
                    xyz[i] += xyz2[i];
                }
            }
        }
    }
    catch (e_2_1) { e_2 = { error: e_2_1 }; }
    finally {
        try {
            if (colors_2_1 && !colors_2_1.done && (_b = colors_2.return)) _b.call(colors_2);
        }
        finally { if (e_2) throw e_2.error; }
    }
    for (var i = 0; i < xyz.length; i++) {
        if (mode.charAt(i) == "h") {
            var A = atan2(dy / colors.length, dx / colors.length) * RAD2DEG;
            xyz[i] = (A + 360) % 360;
        }
        else {
            xyz[i] = xyz[i] / colors.length;
        }
    }
    return guess(xyz, mode).alpha(alphaSum / colors.length);
}
exports.average = average;
function bezier() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    var chromables = Array.isArray(args[0]) ? args[0] : args;
    var f = _bezier(chromables);
    f.scale = CubeHelix.prototype.scale;
    return f;
}
exports.bezier = bezier;
function blend(bottom, top, mode) {
    if (!blend_fs[mode]) {
        throw new Error("unknown blend mode " + mode);
    }
    return blend_fs[mode](bottom, top);
}
exports.blend = blend;
var blend_fs = {
    normal: blend_f(each(function (a, _) { return a; })),
    multiply: blend_f(each(function (a, b) { return (a * b) / 255; })),
    screen: blend_f(each(_screen)),
    overlay: blend_f(each(_overlay)),
    darken: blend_f(each(min)),
    lighten: blend_f(each(max)),
    dodge: blend_f(each(_dodge)),
    burn: blend_f(each(_burn)),
};
function scale() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    var f = (function (t) { return f._at(t); });
    Object.getOwnPropertyNames(Scale.prototype).forEach(function (key) { return (f[key] = Scale.prototype[key]); });
    if (Array.isArray(args[0]))
        args = args[0];
    if (args.length == 1 && "string" == typeof args[0])
        args = exports.brewer[args[0]];
    f._init("function" == typeof args[0] ? args[0] : args.map(function (a) { return color(a); }));
    return f;
}
exports.scale = scale;
var Scale = (function () {
    function Scale() {
    }
    Scale.prototype.classes = function (classes) {
        if (undefined === classes) {
            return this._classes;
        }
        if (Array.isArray(classes)) {
            this._classes = classes;
            this.domain(classes[0], classes[classes.length - 1]);
        }
        else {
            if (classes % 1 != 0 || classes < 1)
                throw new Error("invalid classes param");
            this._classes = limits(this.domain(), "e", classes);
        }
        return this;
    };
    Scale.prototype.domain = function () {
        var _this = this;
        var domain = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            domain[_i] = arguments[_i];
        }
        if (undefined === domain[0]) {
            return "function" !== typeof this._colors
                ? this._pos.map(function (p) { return lerp(_this._min, _this._max, p); })
                : [this._min, this._max];
        }
        this._min = domain[0];
        this._max = domain[domain.length - 1];
        if (2 == domain.length) {
            if ("function" !== typeof this._colors) {
                this._pos = this._colors.map(function (_, c) { return c / (_this._colors.length - 1); });
            }
        }
        else if ("function" !== typeof this._colors && domain.length == this._colors.length) {
            this._pos = domain.map(function (d) { return lerpInv(_this._min, _this._max, d); });
        }
        else {
            throw new Error("invalid domain " + domain);
        }
        return this;
    };
    Scale.prototype.mode = function (mode) {
        if (undefined === mode) {
            return this._mode;
        }
        this._mode = mode;
        this._resetCache();
        return this;
    };
    Scale.prototype.out = function (outputFormat) {
        this._out = outputFormat;
        return this;
    };
    Scale.prototype.correctLightness = function (enableCorrectLightness) {
        if (enableCorrectLightness === void 0) { enableCorrectLightness = true; }
        if (this._correctLightness != enableCorrectLightness) {
            this._resetCache();
            var colors = this._colors;
            if (enableCorrectLightness && "function" !== typeof colors) {
                var sign_1 = 0;
                for (var i = 1; i < colors.length; i++) {
                    var sign2 = colors[i].lab()[0] - colors[i - 1].lab()[0];
                    if (0 == sign_1) {
                        sign_1 = sign2;
                    }
                    else if (sign_1 * sign2 < 0) {
                        throw new Error("scale color lightnesses must be monotonic");
                    }
                }
            }
        }
        this._correctLightness = enableCorrectLightness;
        return this;
    };
    Scale.prototype.padding = function (paddingLeft, paddingRight) {
        if (paddingRight === void 0) { paddingRight = paddingLeft; }
        if (!paddingLeft) {
            return [this._paddingLeft, this._paddingRight];
        }
        this._paddingLeft = paddingLeft;
        this._paddingRight = paddingRight;
        return this;
    };
    Scale.prototype.colors = function (numColors, format) {
        var _this = this;
        if (format === void 0) { format = "hex"; }
        var result;
        if (undefined === numColors) {
            result = this._colors.slice();
        }
        else if (numColors == 1) {
            result = [this._color((this._min + this._max) / 2)];
        }
        else if (numColors > 1) {
            result = Array.from({ length: numColors }, function (_, i) {
                return _this._color(lerp(_this._min, _this._max, i / (numColors - 1)));
            });
        }
        else {
            var samples = void 0;
            if (this._classes && this._classes.length > 2) {
                samples = Array.from({ length: this._classes.length - 1 }, function (_, i) { return (_this._classes[i] + _this._classes[i + 1]) * 0.5; });
            }
            else {
                samples = this.domain();
            }
            result = samples.map(function (s) { return _this._color(s); });
        }
        return (format != "color" ? result.map(function (c) { return c[format](); }) : result);
    };
    Scale.prototype.cache = function (enableCache) {
        if (undefined === enableCache) {
            return !!this._cache;
        }
        this._cache = enableCache ? this._cache || new Map() : undefined;
        return this;
    };
    Scale.prototype.gamma = function (gamma) {
        if (undefined === gamma) {
            return this._gamma;
        }
        this._gamma = gamma;
        return this;
    };
    Scale.prototype._at = function (t) {
        var c = this._color(t);
        return this._out ? c[this._out]() : c;
    };
    Scale.prototype._init = function (colorsOrFunction) {
        this._colors = colorsOrFunction;
        if ("function" != typeof colorsOrFunction) {
            this._pos = colorsOrFunction.map(function (_, i) { return i / (colorsOrFunction.length - 1); });
        }
        this._mode = "rgb";
        this.domain(0, 1);
        this._paddingLeft = 0;
        this._paddingRight = 0;
        this._correctLightness = false;
        this._cache = new Map();
        this._gamma = 1;
    };
    Scale.prototype._getClass = function (value) {
        return this._classes.findIndex(function (cls) { return value <= cls; }) - 1;
    };
    Scale.prototype._color = function (val, bypassMap) {
        if (bypassMap === void 0) { bypassMap = false; }
        var t;
        if (!bypassMap) {
            var min_1 = this._min, max_1 = this._max;
            if (this._classes && this._classes.length > 2) {
                var c = this._getClass(val);
                t = c / (this._classes.length - 2);
            }
            else if (max_1 !== min_1) {
                t = (val - min_1) / (max_1 - min_1);
            }
            else {
                t = 1;
            }
            if (this._correctLightness) {
                t = this._tCorrectedLightness(t);
            }
        }
        else {
            t = val;
        }
        t = Math.pow(t, this._gamma);
        t = (this._paddingLeft + t) / (1 + this._paddingLeft + this._paddingRight);
        t = clamp(t, 0, 1);
        var tHash = t;
        var cacheResult = this._cache && this._cache.get(tHash);
        if (cacheResult) {
            return cacheResult;
        }
        else {
            var col = void 0;
            if (Array.isArray(this._colors)) {
                for (var i = 0; i < this._pos.length; i++) {
                    var p = this._pos[i];
                    if (t <= p) {
                        col = this._colors[i];
                        break;
                    }
                    if (t >= p && i == this._pos.length - 1) {
                        col = this._colors[i];
                        break;
                    }
                    if (t > p && t < this._pos[i + 1]) {
                        t = (t - p) / (this._pos[i + 1] - p);
                        col = mix(this._colors[i], this._colors[i + 1], t, this._mode);
                        break;
                    }
                }
            }
            else {
                col = this._colors(t);
            }
            if (this._cache) {
                this._cache.set(tHash, col);
            }
            return col;
        }
    };
    Scale.prototype._tCorrectedLightness = function (t0_1) {
        var _this = this;
        var L0 = this._color(0, true).lab()[0];
        var L1 = this._color(1, true).lab()[0];
        var L_ideal = lerp(L0, L1, t0_1);
        return bisect(function (t) { return _this._color(t, true).lab()[0] - L_ideal; }, 0, 1, 8);
    };
    Scale.prototype._resetCache = function () {
        if (this._cache)
            this._cache.clear();
    };
    return Scale;
}());
exports.Scale = Scale;
var scales;
(function (scales) {
    function cool() {
        return scale([hsl(180, 1, 0.9), hsl(250, 0.7, 0.4)]);
    }
    scales.cool = cool;
    function hot() {
        return scale(["#000", "#f00", "#ff0", "#fff"]).mode("rgb");
    }
    scales.hot = hot;
})(scales = exports.scales || (exports.scales = {}));
function contrast(a, b) {
    var l1 = color(a).luminance();
    var l2 = color(b).luminance();
    if (l1 > l2) {
        return (l1 + 0.05) / (l2 + 0.05);
    }
    else {
        return (l2 + 0.05) / (l1 + 0.05);
    }
}
exports.contrast = contrast;
function distance(a, b, mode) {
    if (mode === void 0) { mode = "lab"; }
    var l1 = color(a)[mode]();
    var l2 = color(b)[mode]();
    var channelDifferences = l1.map(function (channelValue, channelIndex) { return channelValue - l2[channelIndex]; });
    return hypot.apply(void 0, __spreadArray([], __read(channelDifferences), false));
}
exports.distance = distance;
function deltaE(reference, sample, L, C) {
    if (L === void 0) { L = 1; }
    if (C === void 0) { C = 1; }
    var _a = __read(color(reference).lab(), 3), L1 = _a[0], a1 = _a[1], b1 = _a[2];
    var _b = __read(color(sample).lab(), 3), L2 = _b[0], a2 = _b[1], b2 = _b[2];
    var c1 = sqrt(a1 * a1 + b1 * b1);
    var c2 = sqrt(a2 * a2 + b2 * b2);
    var sl = L1 < 16.0 ? 0.511 : (0.040975 * L1) / (1.0 + 0.01765 * L1);
    var sc = (0.0638 * c1) / (1.0 + 0.0131 * c1) + 0.638;
    var h1 = norm360(c1 < 0.000001 ? 0.0 : atan2(b1, a1) * RAD2DEG);
    var t = h1 >= 164.0 && h1 <= 345.0
        ? 0.56 + abs(0.2 * cos((h1 + 168.0) * DEG2RAD))
        : 0.36 + abs(0.4 * cos((h1 + 35.0) * DEG2RAD));
    var c4 = c1 * c1 * c1 * c1;
    var f = sqrt(c4 / (c4 + 1900.0));
    var sh = sc * (f * t + 1.0 - f);
    var delL = L1 - L2;
    var delC = c1 - c2;
    var delA = a1 - a2;
    var delB = b1 - b2;
    var dH2 = delA * delA + delB * delB - delC * delC;
    var v1 = delL / (L * sl);
    var v2 = delC / (C * sc);
    var v3 = sh;
    return sqrt(v1 * v1 + v2 * v2 + dH2 / (v3 * v3));
}
exports.deltaE = deltaE;
function analyze(data) {
    var r = {
        min: Infinity,
        max: -Infinity,
        sum: 0,
        values: [],
        count: 0,
    };
    function add(val) {
        if (val != undefined && !isNaN(val)) {
            r.values.push(val);
            r.sum += val;
            if (val < r.min)
                r.min = val;
            if (val > r.max)
                r.max = val;
            r.count += 1;
        }
    }
    data.forEach(function (val) { return add(val); });
    r.domain = [r.min, r.max];
    r.limits = function (mode, num) {
        return limits(this, mode, num);
    };
    return r;
}
exports.analyze = analyze;
function limits(data, mode, num) {
    var e_3, _a;
    if (mode === void 0) { mode = "e"; }
    if (num === void 0) { num = 7; }
    var info = Array.isArray(data) ? analyze(data) : data;
    var min = info.min, max = info.max, values = info.values;
    values.sort(function (a, b) { return a - b; });
    if (num == 1) {
        return [min, max];
    }
    if (mode.startsWith("c")) {
        return [min, max];
    }
    else if (mode.startsWith("e")) {
        return Array.from({ length: num + 1 }, function (_, i) { return lerp(min, max, i / num); });
    }
    else if (mode.startsWith("l")) {
        if (min <= 0) {
            throw new Error("Logarithmic scales are only possible for values > 0");
        }
        var min_log_1 = Math.LOG10E * log(min);
        var max_log_1 = Math.LOG10E * log(max);
        return Array.from({ length: num + 1 }, function (_, i) { return Math.pow(10, lerp(min_log_1, max_log_1, i / num)); });
    }
    else if (mode.startsWith("q")) {
        return Array.from({ length: num + 1 }, function (_, i) {
            var p = ((values.length - 1) * i) / num;
            var pb = floor(p);
            return pb == p ? values[pb] : lerp(values[pb], values[pb + 1], p - pb);
        });
    }
    else if (mode.startsWith("k")) {
        var n = values.length;
        var assignments = new Array(n);
        var clusterSizes = new Array(num);
        var repeat = true;
        var nb_iters = 0;
        var centroids_1 = Array.from({ length: num + 1 }, function (_, i) { return lerp(min, max, i / num); });
        do {
            clusterSizes.fill(0);
            var _loop_1 = function (i) {
                var value = values[i];
                var minDistIndex = indexOfMax(centroids_1, function (c) { return -abs(c - value); });
                clusterSizes[minDistIndex]++;
                assignments[i] = minDistIndex;
            };
            for (var i = 0; i < values.length; i++) {
                _loop_1(i);
            }
            var newCentroids = new Array(num).fill(0);
            for (var i = 0; i < assignments.length; i++) {
                var cluster = assignments[i];
                newCentroids[cluster] += values[i];
            }
            for (var j = 0; j < newCentroids.length; j++) {
                newCentroids[j] /= clusterSizes[j];
            }
            repeat = newCentroids.some(function (nc, j) { return nc != centroids_1[j]; });
            centroids_1 = newCentroids;
        } while (nb_iters++ < 200 && repeat);
        var kClusters = Array.from({ length: num }, function () { return []; });
        for (var i = 0; i < assignments.length; i++) {
            var cluster = assignments[i];
            kClusters[cluster].push(values[i]);
        }
        var tmpKMeansBreaks = [];
        try {
            for (var kClusters_1 = __values(kClusters), kClusters_1_1 = kClusters_1.next(); !kClusters_1_1.done; kClusters_1_1 = kClusters_1.next()) {
                var kCluster = kClusters_1_1.value;
                tmpKMeansBreaks.push(kCluster[0], kCluster[kCluster.length - 1]);
            }
        }
        catch (e_3_1) { e_3 = { error: e_3_1 }; }
        finally {
            try {
                if (kClusters_1_1 && !kClusters_1_1.done && (_a = kClusters_1.return)) _a.call(kClusters_1);
            }
            finally { if (e_3) throw e_3.error; }
        }
        tmpKMeansBreaks.sort(function (a, b) { return a - b; });
        var limits_1 = [];
        limits_1.push(tmpKMeansBreaks[0]);
        for (var i = 1; i < tmpKMeansBreaks.length; i += 2) {
            var v = tmpKMeansBreaks[i];
            if (!isNaN(v) && limits_1.indexOf(v) == -1) {
                limits_1.push(v);
            }
        }
        return limits_1;
    }
    else {
        throw new Error("unknown mode");
    }
}
exports.limits = limits;
var interpolators = {};
var _input = {};
function linear_interpolator(col1, col2, f, m) {
    var xyz1 = col1[m]();
    var xyz2 = col2[m]();
    return guess([
        lerp(xyz1[0], xyz2[0], f),
        lerp(xyz1[1], xyz2[1], f),
        lerp(xyz1[2], xyz2[2], f),
        lerp(col1.alpha(), col2.alpha(), f),
    ], m);
}
interpolators.xyz = interpolators.rgb = interpolators.lab = linear_interpolator;
interpolators.num = function (col1, col2, f) {
    var n1 = col1.num();
    var n2 = col2.num();
    return num(lerp(n1, n2, f));
};
interpolators.lrgb = function (col1, col2, f) {
    var _a = __read(col1.rgba(false, false), 4), r1 = _a[0], g1 = _a[1], b1 = _a[2], a1 = _a[3];
    var _b = __read(col2.rgba(false, false), 4), r2 = _b[0], g2 = _b[1], b2 = _b[2], a2 = _b[3];
    return new Color(sqrt(Math.pow(r1, 2) * (1 - f) + Math.pow(r2, 2) * f), sqrt(Math.pow(g1, 2) * (1 - f) + Math.pow(g2, 2) * f), sqrt(Math.pow(b1, 2) * (1 - f) + Math.pow(b2, 2) * f), lerp(a1, a2, f));
};
function _bezier(chromables) {
    var colors = chromables.map(function (c) { return color(c); });
    var _a = __read(colors.map(function (c) { return c.lab(); }), 4), lab0 = _a[0], lab1 = _a[1], lab2 = _a[2], lab3 = _a[3];
    if (2 == chromables.length) {
        return function (t) {
            return lab([0, 1, 2].map(function (i) { return lerp(lab0[i], lab1[i], t); }));
        };
    }
    else if (3 == chromables.length) {
        var bezier2_1 = function (p0, p1, p2, t) {
            return Math.pow((1 - t), 2) * p0 + 2 * (1 - t) * t * p1 + Math.pow(t, 2) * p2;
        };
        return function (t) { return lab([0, 1, 2].map(function (i) { return bezier2_1(lab0[i], lab1[i], lab2[i], t); })); };
    }
    else if (4 == chromables.length) {
        var bezier3_1 = function (p0, p1, p2, p3, t) {
            return Math.pow((1 - t), 3) * p0 + 3 * Math.pow((1 - t), 2) * t * p1 + 3 * (1 - t) * Math.pow(t, 2) * p2 + Math.pow(t, 3) * p3;
        };
        return function (t) { return lab([0, 1, 2].map(function (i) { return bezier3_1(lab0[i], lab1[i], lab2[i], lab3[i], t); })); };
    }
    else if (5 == chromables.length) {
        var I0_1 = _bezier(colors.slice(0, 3));
        var I1_1 = _bezier(colors.slice(2, 5));
        return function (t) { return (t < 0.5 ? I0_1(t * 2) : I1_1((t - 0.5) * 2)); };
    }
    else
        throw new Error();
}
function guess(args, mode) {
    if (Array.isArray(args[0]))
        args = args[0];
    if (!mode) {
        if (args.length == 1 && args[0] in exports.w3cx11) {
            mode = "name";
        }
        else if (args.length == 1 && "string" == typeof args[0]) {
            mode = "css";
        }
        else if (args.length == 3) {
            mode = "rgb";
        }
        else if (args.length == 4 && "number" == typeof args[3] && args[3] >= 0 && args[3] <= 1) {
            mode = "rgb";
        }
        else if (args.length == 1 && "number" == typeof args[0] && args[0] >= 0 && args[0] <= 0xffffff) {
            mode = "num";
        }
        else
            throw new Error("could not guess mode. args " + JSON.stringify(args));
    }
    var channels = _input[mode].apply(_input, __spreadArray([], __read(args), false));
    return new Color(channels[0], channels[1], channels[2], undefined !== channels[3] ? channels[3] : 1);
}
function _average_lrgb(colors) {
    var e_4, _a;
    var rSquareSum = 0, gSquareSum = 0, bSquareSum = 0, alphaSum = 0;
    try {
        for (var colors_3 = __values(colors), colors_3_1 = colors_3.next(); !colors_3_1.done; colors_3_1 = colors_3.next()) {
            var col = colors_3_1.value;
            var _b = __read(col.rgba(false, false), 4), r = _b[0], g = _b[1], b = _b[2], alpha = _b[3];
            rSquareSum += Math.pow(r, 2);
            gSquareSum += Math.pow(g, 2);
            bSquareSum += Math.pow(b, 2);
            alphaSum += alpha;
        }
    }
    catch (e_4_1) { e_4 = { error: e_4_1 }; }
    finally {
        try {
            if (colors_3_1 && !colors_3_1.done && (_a = colors_3.return)) _a.call(colors_3);
        }
        finally { if (e_4) throw e_4.error; }
    }
    return new Color(sqrt(rSquareSum) / colors.length, sqrt(gSquareSum) / colors.length, sqrt(bSquareSum) / colors.length, alphaSum / colors.length);
}
function hex2rgb(hex) {
    var m;
    if ((m = hex.match(/^#?([A-F\d]{2})([A-F\d]{2})([A-F\d]{2})([A-F\d]{2})?$/i))) {
        return [parseInt(m[1], 16), parseInt(m[2], 16), parseInt(m[3], 16), m[4] ? parseInt(m[4], 16) / 255 : 1];
    }
    else if ((m = hex.match(/^#?([A-F\d])([A-F\d])([A-F\d])([A-F\d])?$/i))) {
        return [
            parseInt(m[1], 16) * 0x11,
            parseInt(m[2], 16) * 0x11,
            parseInt(m[3], 16) * 0x11,
            m[4] ? (parseInt(m[4], 16) * 0x11) / 255 : 1,
        ];
    }
    throw new Error("invalid hex color: " + hex);
}
function rgb2hex(r255, g255, b255, a1, mode) {
    if (mode === void 0) { mode = "rgb"; }
    r255 = clamp(round(r255), 0, 255);
    g255 = clamp(round(g255), 0, 255);
    b255 = clamp(round(b255), 0, 255);
    var rgb = (r255 << 16) | (g255 << 8) | b255;
    var rgbString = rgb.toString(16).padStart(6, "0");
    var alphaString = round(clamp(a1) * 255)
        .toString(16)
        .padStart(2, "0");
    return "#" + ("argb" == mode ? alphaString + rgbString : "rgba" == mode ? rgbString + alphaString : rgbString);
}
_input.lrgb = _input.rgb;
_input.hex = hex2rgb;
_input.hsl = hsl2rgb;
function norm360(degrees) {
    return ((degrees % 360) + 360) % 360;
}
_input.hsv = hsv2rgb;
function num2rgb(num) {
    if (!("number" == typeof num && num >= 0 && num <= 0xffffff)) {
        throw new Error("unknown num color: " + num);
    }
    var r = num >> 16;
    var g = (num >> 8) & 0xff;
    var b = num & 0xff;
    return [r, g, b, 1];
}
function rgb2num(r255, g255, b255, a1, mode) {
    if (a1 === void 0) { a1 = 1; }
    if (mode === void 0) { mode = "rgb"; }
    var rgbNum = (round(r255) << 16) | (round(g255) << 8) | round(b255);
    if ("rgb" === mode) {
        return rgbNum;
    }
    else if ("rgba" === mode) {
        return (rgbNum << 8) | (round(a1 * 255) << 24);
    }
    else {
        return (round(a1 * 255) << 24) | rgbNum;
    }
}
_input.num = num2rgb;
var WS = "\\s*";
var FLOAT = "([+-]?(?:\\d*\\.?)?\\d+(?:[eE][+-]?\\d+)?)";
var CSS_RGB_REGEX = new RegExp(["^rgba?\\(", FLOAT, ",", FLOAT, ",", FLOAT, "(?:,", FLOAT + "(%)?", ")?\\)$"].join(WS), "i");
var CSS_RGB_WS_REGEX = new RegExp(["^rgba?\\(", FLOAT, FLOAT, FLOAT, "(?:/", FLOAT + "(%)?", ")?\\)$"].join(WS), "i");
var CSS_RGB_PERCENT_REGEX = new RegExp(["^rgba?\\(", FLOAT + "%", ",", FLOAT + "%", ",", FLOAT + "%", "(?:,", FLOAT + "(%)?", ")?\\)$"].join(WS), "i");
var CSS_RGB_WS_PERCENT_REGEX = new RegExp(["^rgba?\\(", FLOAT + "%", FLOAT + "%", FLOAT + "%", "(?:/", FLOAT + "(%)?", ")?\\)$"].join(WS), "i");
var CSS_HSL_REGEX = new RegExp(["^hsla?\\(", FLOAT + "(deg|rad|turn)?", ",", FLOAT + "%", ",", FLOAT + "%", "(?:,", FLOAT + "(%)?", ")?\\)$"].join(WS), "i");
var CSS_HSL_WS_REGEX = new RegExp(["^hsla?\\(", FLOAT + "(deg|rad|turn)?\\s+" + FLOAT + "%", FLOAT + "%", "(?:/", FLOAT + "(%)?", ")?\\)$"].join(WS), "i");
function css2rgb(css) {
    if (exports.w3cx11 && exports.w3cx11.hasOwnProperty(css)) {
        return num2rgb(exports.w3cx11[css.toLowerCase()]);
    }
    var m;
    if ((m = css.match(CSS_RGB_REGEX) || css.match(CSS_RGB_WS_REGEX))) {
        return [
            clamp(+m[1], 0, 255),
            clamp(+m[2], 0, 255),
            clamp(+m[3], 0, 255),
            m[4] ? clamp(m[5] ? +m[4] / 100 : +m[4]) : 1,
        ];
    }
    else if ((m = css.match(CSS_RGB_PERCENT_REGEX) || css.match(CSS_RGB_WS_PERCENT_REGEX))) {
        return [
            clamp(+m[1] / 100) * 255,
            clamp(+m[2] / 100) * 255,
            clamp(+m[3] / 100) * 255,
            m[4] ? clamp(m[5] ? +m[4] / 100 : +m[4]) : 1,
        ];
    }
    else if ((m = css.match(CSS_HSL_REGEX) || css.match(CSS_HSL_WS_REGEX))) {
        var CONVERSION = { deg: 1, rad: RAD2DEG, turn: 360 };
        var angleUnit = (m[2] ? m[2].toLowerCase() : "deg");
        return hsl2rgb((((+m[1] * CONVERSION[angleUnit]) % 360) + 360) % 360, clamp(+m[3] / 100), clamp(+m[4] / 100), m[5] ? clamp(m[6] ? +m[5] / 100 : +m[5]) : 1);
    }
    else {
        return hex2rgb(css);
    }
}
function rgb2css(r, g, b, a) {
    if (a === void 0) { a = 1; }
    if (a >= 1) {
        return "rgb(" + [r, g, b].map(round).join(",") + ")";
    }
    else {
        return "rgba(" + [r, g, b].map(round).join(",") + "," + a + ")";
    }
}
function rnd(a) {
    return round(a * 100) / 100;
}
function hsl2css(_a, alpha) {
    var _b = __read(_a, 3), h = _b[0], s = _b[1], l = _b[2];
    var mode = alpha < 1 ? "hsla" : "hsl";
    return (mode +
        "(" +
        rnd(h) +
        "," +
        rnd(s * 100) +
        "%" +
        "," +
        rnd(l * 100) +
        "%" +
        ("hsla" == mode ? "," + rnd(alpha) : "") +
        ")");
}
_input.css = css2rgb;
_input.name = function (name) {
    return num2rgb(exports.w3cx11[name]);
};
function lch2lab(l, c, hueDegrees) {
    return [l, cos(hueDegrees * DEG2RAD) * c, sin(hueDegrees * DEG2RAD) * c];
}
function lch2rgb(l, c, hDegrees, alpha1) {
    if (alpha1 === void 0) { alpha1 = 1; }
    var _a = __read(lch2lab(l, c, hDegrees), 3), a = _a[1], b = _a[2];
    return cielab2rgb(l, a, b, alpha1);
}
function lab2lch(l, a, b) {
    var c = hypot(a, b);
    var h = (atan2(b, a) * RAD2DEG + 360) % 360;
    return [l, c, h];
}
function rgb2lch(r255, g255, b255) {
    var _a = __read(rgb2lab(r255, g255, b255), 3), l = _a[0], a = _a[1], b2 = _a[2];
    return lab2lch(l, a, b2);
}
_input.lch = lch2rgb;
function rgb2cmyk(r255, g255, b255) {
    r255 /= 255;
    g255 /= 255;
    b255 /= 255;
    var k = 1 - max(r255, g255, b255);
    if (1 == k)
        return [0, 0, 0, 1];
    var c = (1 - r255 - k) / (1 - k);
    var m = (1 - g255 - k) / (1 - k);
    var y = (1 - b255 - k) / (1 - k);
    return [c, m, y, k];
}
function cmyk2rgb(c1, m1, y1, k1, alpha1) {
    if (alpha1 === void 0) { alpha1 = 1; }
    if (k1 == 1) {
        return [0, 0, 0, alpha1];
    }
    var r255 = 255 * (1 - c1) * (1 - k1);
    var g255 = 255 * (1 - m1) * (1 - k1);
    var b255 = 255 * (1 - y1) * (1 - k1);
    return [r255, g255, b255, alpha1];
}
_input.cmyk = cmyk2rgb;
_input.gl = function (r, g, b, a) {
    if (a === void 0) { a = 1; }
    return [r * 255, g * 255, b * 255, a];
};
function rgbChannel2RgbLinear(x255) {
    var x1 = x255 / 255;
    if (x1 <= 0.04045) {
        return x1 / 12.92;
    }
    else {
        return Math.pow(((x1 + 0.055) / 1.055), 2.4);
    }
}
function rgbLinearChannel2Rgb(xLinear1) {
    if (xLinear1 <= 0.0031308) {
        return 255 * (12.92 * xLinear1);
    }
    else {
        return 255 * ((1 + 0.055) * Math.pow(xLinear1, (1 / 2.4)) - 0.055);
    }
}
function kelvin2rgb(kelvin) {
    var t = kelvin / 100;
    var r, g, b;
    if (t < 66) {
        r = 255;
        g = -155.25485562709179 - 0.44596950469579133 * (t - 2) + 104.49216199393888 * log(t - 2);
        b = t < 20 ? 0 : -254.76935184120902 + 0.8274096064007395 * (t - 10) + 115.67994401066147 * log(t - 10);
    }
    else {
        r = 351.97690566805693 + 0.114206453784165 * (t - 55) - 40.25366309332127 * log(t - 55);
        g = 325.4494125711974 + 0.07943456536662342 * (t - 50) - 28.0852963507957 * log(t - 50);
        b = 255;
    }
    return [r, g, b];
}
_input.rgb = function () {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    return args;
};
function rgb2kelvin(r255, g255, b255) {
    console.log(b255 - r255);
    if (g255 + b255 < 158.61) {
        console.log("0 < t < 20");
        return round(newtonIterate1d(function (t) { return g255 - (-155.25485562709179 - 0.44596950469579133 * (t - 2) + 104.49216199393888 * log(t - 2)); }, 15, 4) * 100);
        return (Math.pow(Math.E, ((g255 + 155.25485562709179 + 0.44596950469579133 * (15 - 2)) / 104.49216199393888)) + 2) * 100;
    }
    else if (b255 - r255 < 0) {
        console.log("20 < t < 66");
        return round(newtonIterate1d(function (t) { return b255 - (-254.76935184120902 + 0.8274096064007395 * (t - 10) + 115.67994401066147 * log(t - 10)); }, 43, 4) * 100);
        return ((Math.pow(Math.E, ((b255 + 254.76935184120902 - 0.8274096064007395 * (43 - 10)) / 115.67994401066147)) + 10) * 100);
    }
    else {
        console.log("0 < t < 400, start= " + (-1.4 * (r255 + g255) + 755));
        return round(newtonIterate1d(function (t) { return r255 - (351.97690566805693 + 0.114206453784165 * (t - 55) - 40.25366309332127 * log(t - 55)); }, -1.4 * (r255 + g255) + 755, 8) * 100);
        return (Math.pow((r255 / 329.698727446), (1 / -0.1332047592)) + 60) * 100;
    }
    return newtonIterate1d(function (k) {
        var eps = 1e-9;
        var _a = __read(kelvin2rgb(k), 3), kr = _a[0], kg = _a[1], kb = _a[2];
        var _b = __read(kelvin2rgb(k + eps), 3), kr2 = _b[0], kg2 = _b[1], kb2 = _b[2];
        var dkr = (kr2 - kr) / eps, dkg = (kg2 - kg) / eps, dkb = (kb2 - kb) / eps;
        return dkr * (kr - r255) + dkg * (kg - g255) + dkb * (kb - b255);
        return kb / kr - b255 / r255;
    }, Math.pow(Math.E, ((b255 / r255 + 2.5) / 0.4)), 20);
    var maxTemp = 40000;
    var minTemp = 1000;
    var eps = 0.4;
    var temp = 0;
    var rgb;
    while (maxTemp - minTemp > eps) {
        temp = (maxTemp + minTemp) * 0.5;
        rgb = kelvin2rgb(temp);
        if (rgb[2] / rgb[0] >= b255 / r255) {
            maxTemp = temp;
        }
        else {
            minTemp = temp;
        }
    }
    return round(temp);
}
_input.temperature = _input.kelvin = _input.K = kelvin2rgb;
function blend_f(f) {
    return function (bottom, top) {
        var _a = __read(f(color(top).rgb(), color(bottom).rgb()), 3), r = _a[0], g = _a[1], b = _a[2];
        return rgb(r, g, b);
    };
}
function each(f) {
    return function (c1, c2) {
        return c1.map(function (e, i) { return f(e, c2[i]); });
    };
}
function _screen(a, b) {
    return 255 * (1 - (1 - a / 255) * (1 - b / 255));
}
function _overlay(a, b) {
    if (b < 128) {
        return (2 * a * b) / 255;
    }
    else {
        return 255 * (1 - 2 * (1 - a / 255) * (1 - b / 255));
    }
}
function _burn(a, b) {
    return 255 * (1 - (1 - b / 255) / (a / 255));
}
function _dodge(a, b) {
    if (a == 255) {
        return 255;
    }
    return 255 * min(1, b / 255 / (1 - a / 255));
}
function rgb2hexhue(r, g, b) {
    var m = min(r, g, b);
    var M = max(r, g, b);
    var delta = M - m;
    var hueTurnX6;
    if (0 == delta) {
        hueTurnX6 = 0;
    }
    else if (r == M) {
        hueTurnX6 = (g - b) / delta + (g < b ? 6 : 0);
    }
    else if (g == M) {
        hueTurnX6 = 2 + (b - r) / delta;
    }
    else {
        hueTurnX6 = 4 + (r - g) / delta;
    }
    return [hueTurnX6 * 60, m, M];
}
function hcxm2rgb(hueDegrees, c1, x1, m1, alpha1) {
    var m255 = m1 * 255;
    var cm255 = c1 * 255 + m255;
    var xm255 = x1 * 255 + m255;
    if (hueDegrees < 60) {
        return [cm255, xm255, m255, alpha1];
    }
    else if (hueDegrees < 120) {
        return [xm255, cm255, m255, alpha1];
    }
    else if (hueDegrees < 180) {
        return [m255, cm255, xm255, alpha1];
    }
    else if (hueDegrees < 240) {
        return [m255, xm255, cm255, alpha1];
    }
    else if (hueDegrees < 300) {
        return [xm255, m255, cm255, alpha1];
    }
    else {
        return [cm255, m255, xm255, alpha1];
    }
}
function hsl2rgb(hueDegrees, s1, l1, alpha1) {
    if (alpha1 === void 0) { alpha1 = 1; }
    hueDegrees = norm360(hueDegrees);
    var c1 = (1 - abs(2 * l1 - 1)) * s1;
    return hcxm2rgb(hueDegrees, c1, c1 * (1 - abs(((hueDegrees / 60) % 2) - 1)), l1 - c1 / 2, alpha1);
}
function rgb2hsl(r255, g255, b255) {
    var _a = __read(rgb2hexhue(r255 / 255, g255 / 255, b255 / 255), 3), hue = _a[0], min1 = _a[1], max1 = _a[2];
    var l1 = (max1 + min1) / 2;
    var s1;
    if (max1 == min1) {
        s1 = 0;
    }
    else {
        s1 = l1 < 0.5 ? (max1 - min1) / (max1 + min1) : (max1 - min1) / (2 - max1 - min1);
    }
    return [hue, s1, l1];
}
function hsv2rgb(hueDegrees, s1, v1, alpha1) {
    if (alpha1 === void 0) { alpha1 = 1; }
    hueDegrees = norm360(hueDegrees);
    var c1 = v1 * s1;
    return hcxm2rgb(hueDegrees, c1, c1 * (1 - abs(((hueDegrees / 60) % 2) - 1)), v1 - c1, alpha1);
}
function rgb2hsv(r255, g255, b255) {
    var _a = __read(rgb2hexhue(r255, g255, b255), 3), hue = _a[0], min255 = _a[1], max255 = _a[2];
    var delta255 = max255 - min255;
    var v1 = max255 / 255.0;
    var s1 = max255 == 0 ? 0 : delta255 / max255;
    return [hue, s1, v1];
}
function hcg2rgb(hueDegrees, c1, g1, alpha1) {
    if (alpha1 === void 0) { alpha1 = 1; }
    hueDegrees = norm360(hueDegrees);
    var p = g1 * (1 - c1);
    return hcxm2rgb(hueDegrees, c1, c1 * (1 - abs(((hueDegrees / 60) % 2) - 1)), p, alpha1);
}
function rgb2hcg(r255, g255, b255) {
    var _a = __read(rgb2hexhue(r255, g255, b255), 3), hue = _a[0], min255 = _a[1], max255 = _a[2];
    var c1 = (max255 - min255) / 255;
    var _g1 = c1 < 1 ? min255 / 255 / (1 - c1) : 0;
    return [hue, c1, _g1];
}
_input.hcg = hcg2rgb;
function cielab2rgb(LStar100, aStar, bStar, alpha) {
    if (alpha === void 0) { alpha = 1; }
    var _a = __read(cielab2xyz(LStar100, aStar, bStar), 3), x = _a[0], y = _a[1], z = _a[2];
    return xyz2rgb(x, y, z, alpha);
}
function cielab2xyz(LStar100, aStar, bStar) {
    function fInv(t) {
        if (t > LAB_delta) {
            return Math.pow(t, 3);
        }
        else {
            return LAB_3DeltaPow2 * (t - 4 / 29);
        }
    }
    return [
        LAB_Xn * fInv((LStar100 + 16) / 116 + aStar / 500),
        LAB_Yn * fInv((LStar100 + 16) / 116),
        LAB_Zn * fInv((LStar100 + 16) / 116 - bStar / 200),
    ];
}
function xyz2cielab(x, y, z) {
    function f(t) {
        if (t > LAB_deltaPow3) {
            return cbrt(t);
        }
        else {
            return t / LAB_3DeltaPow2 + 4 / 29;
        }
    }
    return [116 * f(y / LAB_Yn) - 16, 500 * (f(x / LAB_Xn) - f(y / LAB_Yn)), 200 * (f(y / LAB_Yn) - f(z / LAB_Zn))];
}
var LAB_Kn = 18;
var LAB_Xn = 0.95047;
var LAB_Yn = 1;
var LAB_Zn = 1.08883;
var LAB_delta = 0.206896552;
var LAB_3DeltaPow2 = 0.12841855;
var LAB_deltaPow3 = 0.008856452;
function rgb2lab(r255, g255, b255) {
    var _a = __read(rgb2xyz(r255, g255, b255), 3), x = _a[0], y = _a[1], z = _a[2];
    return xyz2cielab(x, y, z);
}
function rgb2xyz(r255, g255, b255) {
    var r1Linear = rgbChannel2RgbLinear(r255);
    var g1Linear = rgbChannel2RgbLinear(g255);
    var b1Linear = rgbChannel2RgbLinear(b255);
    var X = 0.4124564 * r1Linear + 0.3575761 * g1Linear + 0.1804375 * b1Linear;
    var Y = 0.2126729 * r1Linear + 0.7151522 * g1Linear + 0.072175 * b1Linear;
    var Z = 0.0193339 * r1Linear + 0.119192 * g1Linear + 0.9503041 * b1Linear;
    return [X, Y, Z];
}
function xyz2rgb(X1, Y1, Z1, alpha1) {
    if (alpha1 === void 0) { alpha1 = 1; }
    var r1Linear = 3.2404542 * X1 - 1.5371385 * Y1 - 0.4985314 * Z1;
    var g1Linear = -0.969266 * X1 + 1.8760108 * Y1 + 0.041556 * Z1;
    var b1Linear = 0.0556434 * X1 - 0.2040259 * Y1 + 1.0572252 * Z1;
    return [rgbLinearChannel2Rgb(r1Linear), rgbLinearChannel2Rgb(g1Linear), rgbLinearChannel2Rgb(b1Linear), alpha1];
}
_input.xyz = xyz2rgb;
_input.lab = cielab2rgb;
function hsi2rgb(hueDegrees, s1, i1, alpha1) {
    if (alpha1 === void 0) { alpha1 = 1; }
    var r, g, b;
    var hRad = hueDegrees * DEG2RAD;
    if (hRad < (2 * PI) / 3) {
        b = (1 - s1) / 3;
        r = (1 + (s1 * cos(hRad)) / cos(PI / 3 - hRad)) / 3;
        g = 1 - (b + r);
    }
    else if (hRad < (4 * PI) / 3) {
        hRad -= (2 * PI) / 3;
        r = (1 - s1) / 3;
        g = (1 + (s1 * cos(hRad)) / cos(PI / 3 - hRad)) / 3;
        b = 1 - (r + g);
    }
    else {
        hRad -= (4 * PI) / 3;
        g = (1 - s1) / 3;
        b = (1 + (s1 * cos(hRad)) / cos(PI / 3 - hRad)) / 3;
        r = 1 - (g + b);
    }
    return [3 * i1 * r * 255, 3 * i1 * g * 255, 3 * i1 * b * 255, alpha1];
}
function rgb2hsi(r255, g255, b255) {
    var r1 = r255 / 255;
    var g1 = g255 / 255;
    var b1 = b255 / 255;
    var i1 = (r1 + g1 + b1) / 3;
    if (r1 == g1 && g1 == b1) {
        return [0, 0, i1];
    }
    else {
        var alpha = (1 / 2) * (2 * r1 - g1 - b1);
        var beta = (sqrt(3) / 2) * (g1 - b1);
        var hRad = atan2(beta, alpha);
        var min1 = min(r1, g1, b1);
        var s1 = 1 - min1 / i1;
        return [(hRad < 0 ? 2 * PI + hRad : hRad) * RAD2DEG, s1, i1];
    }
}
_input.hsi = hsi2rgb;
interpolators.hsv = interpolators.hsl = interpolators.hsi = interpolators.lch = interpolators.hcg = function interpolate_hsx(color1, color2, f, m) {
    var _a = __read(color1[m](), 3), a1 = _a[0], b1 = _a[1], c1 = _a[2];
    var _b = __read(color2[m](), 3), a2 = _b[0], b2 = _b[1], c2 = _b[2];
    function lerpHue(hue1, hue2, f) {
        var dh = norm360(hue2 - hue1 + 180) - 180;
        return hue1 + f * dh;
    }
    return color(("h" == m.charAt(0) ? lerpHue : lerp)(a1, a2, f), lerp(b1, b2, f), ("h" == m.charAt(2) ? lerpHue : lerp)(c1, c2, f), m);
};
function indexOfMax(arr, f) {
    var maxValue = -Infinity, maxValueIndex = -1;
    for (var i = 0; i < arr.length; i++) {
        var value = f(arr[i]);
        if (value > maxValue) {
            maxValue = value;
            maxValueIndex = i;
        }
    }
    return maxValueIndex;
}
function withMax(arr, f) {
    return arr[indexOfMax(arr, f)];
}

//# sourceMappingURL=color.js.map
