"use strict";

exports.__esModule = true;
exports.getPalette = getPalette;
exports.Gradients = exports.getRandomIntInclusive = exports.RGB = exports.ColorThief = void 0;

/*!
 * Color Thief v2.0
 * by Lokesh Dhakar - http://www.lokeshdhakar.com
 *
 * License
 * -------
 * Creative Commons Attribution 2.5 License:
 * http://creativecommons.org/licenses/by/2.5/
 *
 * Thanks
 * ------
 * Nick Rabinowitz - For creating quantize.js.
 * John Schulz - For clean up and optimization. @JFSIII
 * Nathan Spady - For adding drag and drop support to the demo page.
 *
 */

/* eslint-disable */
var CanvasImage = function (a) {
  this.canvas = document.createElement("canvas"), this.context = this.canvas.getContext("2d"), document.body.appendChild(this.canvas), this.width = this.canvas.width = a.width, this.height = this.canvas.height = a.height, this.context.drawImage(a, 0, 0, this.width, this.height);
};

CanvasImage.prototype.clear = function () {
  this.context.clearRect(0, 0, this.width, this.height);
}, CanvasImage.prototype.update = function (a) {
  this.context.putImageData(a, 0, 0);
}, CanvasImage.prototype.getPixelCount = function () {
  return this.width * this.height;
}, CanvasImage.prototype.getImageData = function () {
  return this.context.getImageData(0, 0, this.width, this.height);
}, CanvasImage.prototype.removeCanvas = function () {
  this.canvas.parentNode.removeChild(this.canvas);
};

var ColorThief = function () {};
/* ! quantize.js Copyright 2008 Nick Rabinowitz. Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php */

/* ! Block below copied from Protovis: http://mbostock.github.com/protovis/ Copyright 2010 Stanford Visualization Group Licensed under the BSD License: http://www.opensource.org/licenses/bsd-license.php */


exports.ColorThief = ColorThief;
let index, gval, bval, rval;
if (ColorThief.prototype.getColor = function (a, b) {
  var c = this.getPalette(a, 5, b),
      d = c[0];
  return d;
}, ColorThief.prototype.getPalette = function (a, b, c) {
  "undefined" == typeof b && (b = 10), "undefined" == typeof c && (c = 10);

  for (var d, e, f, g, h, i = new CanvasImage(a), j = i.getImageData(), k = j.data, l = i.getPixelCount(), m = [], n = 0; l > n; n += c) d = 4 * n, e = k[d + 0], f = k[d + 1], g = k[d + 2], h = k[d + 3], h >= 125 && (e > 250 && f > 250 && g > 250 || m.push([e, f, g]));

  var o = MMCQ.quantize(m, b),
      p = o.palette();
  return i.removeCanvas(), p;
}, !pv) var pv = {
  map: function (a, b) {
    var c = {};
    return b ? a.map(function (a, d) {
      return c.index = d, b.call(c, a);
    }) : a.slice();
  },
  naturalOrder: function (a, b) {
    return b > a ? -1 : a > b ? 1 : 0;
  },
  sum: function (a, b) {
    var c = {};
    return a.reduce(b ? function (a, d, e) {
      return c.index = e, a + b.call(c, d);
    } : function (a, b) {
      return a + b;
    }, 0);
  },
  max: function (a, b) {
    return Math.max.apply(null, b ? pv.map(a, b) : a);
  }
};

var MMCQ = function () {
  function a(a, b, c) {
    return (a << 2 * i) + (b << i) + c;
  }

  function b(a) {
    function b() {
      c.sort(a), d = !0;
    }

    var c = [],
        d = !1;
    return {
      push: function (a) {
        c.push(a), d = !1;
      },
      peek: function (a) {
        return d || b(), void 0 === a && (a = c.length - 1), c[a];
      },
      pop: function () {
        return d || b(), c.pop();
      },
      size: function () {
        return c.length;
      },
      map: function (a) {
        return c.map(a);
      },
      debug: function () {
        return d || b(), c;
      }
    };
  }

  function c(a, b, c, d, e, f, g) {
    var h = this;
    h.r1 = a, h.r2 = b, h.g1 = c, h.g2 = d, h.b1 = e, h.b2 = f, h.histo = g;
  }

  function d() {
    this.vboxes = new b(function (a, b) {
      return pv.naturalOrder(a.vbox.count() * a.vbox.volume(), b.vbox.count() * b.vbox.volume());
    });
  }

  function e(b) {
    var c,
        d,
        e,
        f,
        g = 1 << 3 * i,
        h = new Array(g);
    return b.forEach(function (b) {
      d = b[0] >> j, e = b[1] >> j, f = b[2] >> j, c = a(d, e, f), h[c] = (h[c] || 0) + 1;
    }), h;
  }

  function f(a, b) {
    var d,
        e,
        f,
        g = 1e6,
        h = 0,
        i = 1e6,
        k = 0,
        l = 1e6,
        m = 0;
    return a.forEach(function (a) {
      d = a[0] >> j, e = a[1] >> j, f = a[2] >> j, g > d ? g = d : d > h && (h = d), i > e ? i = e : e > k && (k = e), l > f ? l = f : f > m && (m = f);
    }), new c(g, h, i, k, l, m, b);
  }

  function g(b, c) {
    function d(a) {
      var b,
          d,
          e,
          f,
          g,
          h = a + "1",
          j = a + "2",
          k = 0;

      for (i = c[h]; i <= c[j]; i++) if (o[i] > n / 2) {
        for (e = c.copy(), f = c.copy(), b = i - c[h], d = c[j] - i, g = d >= b ? Math.min(c[j] - 1, ~~(i + d / 2)) : Math.max(c[h], ~~(i - 1 - b / 2)); !o[g];) g++;

        for (k = p[g]; !k && o[g - 1];) k = p[--g];

        return e[j] = g, f[h] = e[j] + 1, [e, f];
      }
    }

    if (c.count()) {
      var e = c.r2 - c.r1 + 1,
          f = c.g2 - c.g1 + 1,
          g = c.b2 - c.b1 + 1,
          h = pv.max([e, f, g]);
      if (1 == c.count()) return [c.copy()];
      var i,
          j,
          k,
          l,
          m,
          n = 0,
          o = [],
          p = [];
      if (h == e) for (i = c.r1; i <= c.r2; i++) {
        for (l = 0, j = c.g1; j <= c.g2; j++) for (k = c.b1; k <= c.b2; k++) m = a(i, j, k), l += b[m] || 0;

        n += l, o[i] = n;
      } else if (h == f) for (i = c.g1; i <= c.g2; i++) {
        for (l = 0, j = c.r1; j <= c.r2; j++) for (k = c.b1; k <= c.b2; k++) m = a(j, i, k), l += b[m] || 0;

        n += l, o[i] = n;
      } else for (i = c.b1; i <= c.b2; i++) {
        for (l = 0, j = c.r1; j <= c.r2; j++) for (k = c.g1; k <= c.g2; k++) m = a(j, k, i), l += b[m] || 0;

        n += l, o[i] = n;
      }
      return o.forEach(function (a, b) {
        p[b] = n - a;
      }), d(h == e ? "r" : h == f ? "g" : "b");
    }
  }

  function h(a, c) {
    function h(a, b) {
      for (var c, d = 1, e = 0; k > e;) if (c = a.pop(), c.count()) {
        var f = g(i, c),
            h = f[0],
            j = f[1];
        if (!h) return;
        if (a.push(h), j && (a.push(j), d++), d >= b) return;
        if (e++ > k) return;
      } else a.push(c), e++;
    }

    if (!a.length || 2 > c || c > 256) return !1;
    var i = e(a),
        j = 0;
    i.forEach(function () {
      j++;
    });
    var m = f(a, i),
        n = new b(function (a, b) {
      return pv.naturalOrder(a.count(), b.count());
    });
    n.push(m), h(n, l * c);

    for (var o = new b(function (a, b) {
      return pv.naturalOrder(a.count() * a.volume(), b.count() * b.volume());
    }); n.size();) o.push(n.pop());

    h(o, c - o.size());

    for (var p = new d(); o.size();) p.push(o.pop());

    return p;
  }

  var i = 5,
      j = 8 - i,
      k = 1e3,
      l = 0.75;
  return c.prototype = {
    volume: function (a) {
      var b = this;
      return (!b._volume || a) && (b._volume = (b.r2 - b.r1 + 1) * (b.g2 - b.g1 + 1) * (b.b2 - b.b1 + 1)), b._volume;
    },
    count: function (b) {
      var c = this,
          d = c.histo;

      if (!c._count_set || b) {
        var e,
            f,
            g,
            h = 0;

        for (e = c.r1; e <= c.r2; e++) for (f = c.g1; f <= c.g2; f++) for (g = c.b1; g <= c.b2; g++) index = a(e, f, g), h += d[index] || 0;

        c._count = h, c._count_set = !0;
      }

      return c._count;
    },
    copy: function () {
      var a = this;
      return new c(a.r1, a.r2, a.g1, a.g2, a.b1, a.b2, a.histo);
    },
    avg: function (b) {
      var c = this,
          d = c.histo;

      if (!c._avg || b) {
        var e,
            f,
            g,
            h,
            j,
            k = 0,
            l = 1 << 8 - i,
            m = 0,
            n = 0,
            o = 0;

        for (f = c.r1; f <= c.r2; f++) for (g = c.g1; g <= c.g2; g++) for (h = c.b1; h <= c.b2; h++) j = a(f, g, h), e = d[j] || 0, k += e, m += e * (f + 0.5) * l, n += e * (g + 0.5) * l, o += e * (h + 0.5) * l;

        c._avg = k ? [~~(m / k), ~~(n / k), ~~(o / k)] : [~~(l * (c.r1 + c.r2 + 1) / 2), ~~(l * (c.g1 + c.g2 + 1) / 2), ~~(l * (c.b1 + c.b2 + 1) / 2)];
      }

      return c._avg;
    },
    contains: function (a) {
      var b = this,
          c = a[0] >> j;
      return gval = a[1] >> j, bval = a[2] >> j, c >= b.r1 && c <= b.r2 && gval >= b.g1 && gval <= b.g2 && bval >= b.b1 && bval <= b.b2;
    }
  }, d.prototype = {
    push: function (a) {
      this.vboxes.push({
        vbox: a,
        color: a.avg()
      });
    },
    palette: function () {
      return this.vboxes.map(function (a) {
        return a.color;
      });
    },
    size: function () {
      return this.vboxes.size();
    },
    map: function (a) {
      for (var b = this.vboxes, c = 0; c < b.size(); c++) if (b.peek(c).vbox.contains(a)) return b.peek(c).color;

      return this.nearest(a);
    },
    nearest: function (a) {
      for (var b, c, d, e = this.vboxes, f = 0; f < e.size(); f++) c = Math.sqrt(Math.pow(a[0] - e.peek(f).color[0], 2) + Math.pow(a[1] - e.peek(f).color[1], 2) + Math.pow(a[2] - e.peek(f).color[2], 2)), (b > c || void 0 === b) && (b = c, d = e.peek(f).color);

      return d;
    },
    forcebw: function () {
      var a = this.vboxes;
      a.sort(function (a, b) {
        return pv.naturalOrder(pv.sum(a.color), pv.sum(b.color));
      });
      var b = a[0].color;
      b[0] < 5 && b[1] < 5 && b[2] < 5 && (a[0].color = [0, 0, 0]);
      var c = a.length - 1,
          d = a[c].color;
      d[0] > 251 && d[1] > 251 && d[2] > 251 && (a[c].color = [255, 255, 255]);
    }
  }, {
    quantize: h
  };
}();

const RGB = function (c0, c1, c2) {
  var isArray = Array.isArray(c0);

  if (!isArray) {
    if (typeof c0 === "string" && c0.length && c0[0] === "#") {
      c2 = c0.substr(5, 2);
      c1 = c0.substr(3, 2);
      c0 = c0.substr(1, 2);
      c2 = parseInt(c2, 16);
      c1 = parseInt(c1, 16);
      c0 = parseInt(c0, 16);
    }
  }

  this.r = Math.min(Math.floor(isArray ? c0[0] : c0), 255);
  this.g = Math.min(Math.floor(isArray ? c0[1] : c1), 255);
  this.b = Math.min(Math.floor(isArray ? c0[2] : c2), 255);
}; //returns range from 0 to 255
//from http://alienryderflex.com/hsp.html by way of http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx


exports.RGB = RGB;

RGB.prototype.brightness = function () {
  var r = this.r,
      g = this.g,
      b = this.b;
  return Math.sqrt(r * r * 0.241 + g * g * 0.691 + b * b * 0.068);
};

RGB.prototype.toHexString = function () {
  var r = (this.r * 1).toString(16);
  if (r.length === 1) r = "0" + r;
  var g = (this.g * 1).toString(16);
  if (g.length === 1) g = "0" + g;
  var b = (this.b * 1).toString(16);
  if (b.length === 1) b = "0" + b;
  return r + g + b;
};

RGB.prototype.toRGBString = function () {
  var rgb = "rgb(" + this.r + "," + this.g + "," + this.b + ")";
  return rgb;
};

RGB.prototype.toHalfBright = function () {
  var rgb = new RGB(this.r / 2, this.g / 2, this.b / 2);
  return rgb;
};

RGB.prototype.toTwiceBright = function () {
  var rgb = new RGB(this.r * 2, this.g * 2, this.b * 2);
  return rgb;
};

RGB.prototype.toThreeFourthsBright = function () {
  var rgb = new RGB(this.r * 0.75, this.g * 0.75, this.b * 0.75);
  return rgb;
}; // Returns a random integer between min (included) and max (included)
// Using Math.round() will give you a non-uniform distribution!


const getRandomIntInclusive = function (min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
};

exports.getRandomIntInclusive = getRandomIntInclusive;
var p, colorThief;

function getPalette(img, colorCount = 6) {
  colorThief = new ColorThief();
  p = colorThief.getPalette(img, colorCount);
  return p;
}

const Gradients = {
  createLinearGradients: function (img, colorCount = 6) {
    console.log("img", img.nodeName);

    try {
      if (img.nodeName) {
        p = getPalette(img, colorCount);
      } else {
        p = img;
      }
    } catch (e) {
      // usually caused by flipping "next" too fast
      console.warn(e);
      return [];
    }

    var c1,
        c2 = new RGB(0, 0, 0),
        rgb1,
        rgb2,
        gradstring;
    var gradients = [],
        max = Math.max(p.length, 6);

    for (var i = 0; i < max; ++i) {
      c1 = new RGB(p[i]);
      if (c1.brightness() > 130) c1 = c1.toHalfBright();
      rgb1 = c1.toRGBString();
      rgb2 = c2.toRGBString();
      gradstring = "linear-gradient(" + rgb2 + ", " + rgb1 + ")";
      gradients.push(gradstring);
      c2 = c1.toHalfBright();
      rgb1 = c1.toRGBString();
      rgb2 = c2.toRGBString();
      gradstring = "linear-gradient(" + rgb1 + ", " + rgb2 + ")";
      gradients.push(gradstring);

      if (i === max - 1) {
        c1 = new RGB(0, 0, 0);
        rgb1 = c1.toRGBString();
        gradstring = "linear-gradient(" + rgb2 + ", " + rgb1 + ")";
        gradients.push(gradstring);
      }
    }

    return gradients;
  },
  createCenterGradients: function (img, colorCount = 6) {
    try {
      if (img.nodeName) {
        p = getPalette(img, colorCount);
      } else {
        p = img;
      }
    } catch (e) {
      // usually caused by flipping "next" too fast
      console.warn(e);
      return [];
    }

    var gradients = [];

    for (var i = 0; i < 4; ++i) {
      var c = new RGB(p[i]);
      if (c.brightness() > 130) c = c.toHalfBright();
      var c2 = c.toHalfBright();
      var rgb = c.toRGBString();
      var rgb2 = c2.toRGBString();
      var gradstring = "radial-gradient(" + rgb + ", " + rgb2 + ")";
      gradients.push(gradstring);
    }

    return gradients;
  },
  gradientTypes: ["linear-gradient(", "linear-gradient(45deg,", "linear-gradient(135deg,", "radial-gradient("],
  createRandomGradients: function (img, colorCount, count, excludeRadial) {
    var i,
        colors = [],
        halfColors = [],
        c,
        hc,
        c1,
        c2,
        ci;
    if (!colorCount) colorCount = 6;
    if (!count) count = 4;

    try {
      if (img.nodeName) {
        p = getPalette(img, colorCount);
      } else {
        p = img;
      }

      for (i = 0; i < p.length; ++i) {
        c = new RGB(p[i]);
        if (c.brightness() > 130) c = c.toHalfBright();
        hc = c.toHalfBright();
        colors.push(c);
        halfColors.push(hc);
      }
    } catch (e) {
      // usually caused by flipping "next" too fast
      console.warn(e);
      return [];
    }

    var gradients = [];

    for (i = 0; i < count; ++i) {
      var gradientLimit = this.gradientTypes.length - 1;
      if (excludeRadial) gradientLimit -= 1;
      var type = getRandomIntInclusive(0, gradientLimit); // console.log("type 0-3", type);

      type = this.gradientTypes[type];
      var start = 0,
          end = 9;

      if (type === "radial-gradient(") {
        // radials should only use the internal
        start = 2;
        end = 5;
      }

      var ctype = getRandomIntInclusive(start, end); // console.log("ctype 0-9", ctype);

      var clength = colors.length - 1;

      switch (ctype) {
        // toggle with black
        case 0:
          c1 = new RGB(0, 0, 0);
          c2 = colors[getRandomIntInclusive(0, clength)];
          break;

        case 1:
          c1 = colors[getRandomIntInclusive(0, clength)];
          c2 = new RGB(0, 0, 0);
          break;
        // toggle with its own half-color

        case 2:
        case 3:
          ci = getRandomIntInclusive(0, clength);
          c1 = colors[ci];
          c2 = halfColors[ci];
          break;

        case 4:
        case 5:
          ci = getRandomIntInclusive(0, clength);
          c1 = halfColors[ci];
          c2 = colors[ci];
          break;
        // toggle with its a (potentially) different half-color

        case 6:
        case 7:
          ci = getRandomIntInclusive(0, clength);
          c1 = colors[ci];
          ci = getRandomIntInclusive(0, clength);
          c2 = halfColors[ci];
          break;

        case 8:
        case 9:
          ci = getRandomIntInclusive(0, clength);
          c1 = halfColors[ci];
          ci = getRandomIntInclusive(0, clength);
          c2 = colors[ci];
          break;
      }

      var rgb = c1.toRGBString();
      var rgb2 = c2.toRGBString();
      var gradstring = type + rgb + ", " + rgb2 + ")";
      gradients.push(gradstring);
    }

    return {
      gradients: gradients,
      palette: p
    };
  }
};
exports.Gradients = Gradients;