import h from "../chroma.js";
import G from "../utils/limit.js";
import d from "../utils/type.js";
const { pow: H } = Math;
function Q(b) {
  let p = "rgb", x = h("#ccc"), L = 0, g = [0, 1], a = [], s = [0, 0], u = !1, o = [], C = !1, m = 0, _ = 1, B = !1, y = {}, M = !0, N = 1;
  const D = function(e) {
    if (e = e || ["#fff", "#000"], e && d(e) === "string" && h.brewer && h.brewer[e.toLowerCase()] && (e = h.brewer[e.toLowerCase()]), d(e) === "array") {
      e.length === 1 && (e = [e[0], e[0]]), e = e.slice(0);
      for (let t = 0; t < e.length; t++)
        e[t] = h(e[t]);
      a.length = 0;
      for (let t = 0; t < e.length; t++)
        a.push(t / (e.length - 1));
    }
    return A(), o = e;
  }, E = function(e) {
    if (u != null) {
      const t = u.length - 1;
      let r = 0;
      for (; r < t && e >= u[r]; )
        r++;
      return r - 1;
    }
    return 0;
  };
  let z = (e) => e, O = (e) => e;
  const w = function(e, t) {
    let r, n;
    if (t == null && (t = !1), isNaN(e) || e === null)
      return x;
    t ? n = e : u && u.length > 2 ? n = E(e) / (u.length - 2) : _ !== m ? n = (e - m) / (_ - m) : n = 1, n = O(n), t || (n = z(n)), N !== 1 && (n = H(n, N)), n = s[0] + n * (1 - s[0] - s[1]), n = G(n, 0, 1);
    const f = Math.floor(n * 1e4);
    if (M && y[f])
      r = y[f];
    else {
      if (d(o) === "array")
        for (let l = 0; l < a.length; l++) {
          const c = a[l];
          if (n <= c) {
            r = o[l];
            break;
          }
          if (n >= c && l === a.length - 1) {
            r = o[l];
            break;
          }
          if (n > c && n < a[l + 1]) {
            n = (n - c) / (a[l + 1] - c), r = h.interpolate(
              o[l],
              o[l + 1],
              n,
              p
            );
            break;
          }
        }
      else d(o) === "function" && (r = o(n));
      M && (y[f] = r);
    }
    return r;
  };
  var A = () => y = {};
  D(b);
  const i = function(e) {
    const t = h(w(e));
    return C && t[C] ? t[C]() : t;
  };
  return i.classes = function(e) {
    if (e != null) {
      if (d(e) === "array")
        u = e, g = [e[0], e[e.length - 1]];
      else {
        const t = h.analyze(g);
        e === 0 ? u = [t.min, t.max] : u = h.limits(t, "e", e);
      }
      return i;
    }
    return u;
  }, i.domain = function(e) {
    if (!arguments.length)
      return g;
    m = e[0], _ = e[e.length - 1], a = [];
    const t = o.length;
    if (e.length === t && m !== _)
      for (let r of Array.from(e))
        a.push((r - m) / (_ - m));
    else {
      for (let r = 0; r < t; r++)
        a.push(r / (t - 1));
      if (e.length > 2) {
        const r = e.map((f, l) => l / (e.length - 1)), n = e.map((f) => (f - m) / (_ - m));
        n.every((f, l) => r[l] === f) || (O = (f) => {
          if (f <= 0 || f >= 1) return f;
          let l = 0;
          for (; f >= n[l + 1]; ) l++;
          const c = (f - n[l]) / (n[l + 1] - n[l]);
          return r[l] + c * (r[l + 1] - r[l]);
        });
      }
    }
    return g = [m, _], i;
  }, i.mode = function(e) {
    return arguments.length ? (p = e, A(), i) : p;
  }, i.range = function(e, t) {
    return D(e), i;
  }, i.out = function(e) {
    return C = e, i;
  }, i.spread = function(e) {
    return arguments.length ? (L = e, i) : L;
  }, i.correctLightness = function(e) {
    return e == null && (e = !0), B = e, A(), B ? z = function(t) {
      const r = w(0, !0).lab()[0], n = w(1, !0).lab()[0], f = r > n;
      let l = w(t, !0).lab()[0];
      const c = r + (n - r) * t;
      let k = l - c, j = 0, q = 1, F = 20;
      for (; Math.abs(k) > 0.01 && F-- > 0; )
        (function() {
          return f && (k *= -1), k < 0 ? (j = t, t += (q - t) * 0.5) : (q = t, t += (j - t) * 0.5), l = w(t, !0).lab()[0], k = l - c;
        })();
      return t;
    } : z = (t) => t, i;
  }, i.padding = function(e) {
    return e != null ? (d(e) === "number" && (e = [e, e]), s = e, i) : s;
  }, i.colors = function(e, t) {
    arguments.length < 2 && (t = "hex");
    let r = [];
    if (arguments.length === 0)
      r = o.slice(0);
    else if (e === 1)
      r = [i(0.5)];
    else if (e > 1) {
      const n = g[0], f = g[1] - n;
      r = I(0, e).map(
        (l) => i(n + l / (e - 1) * f)
      );
    } else {
      b = [];
      let n = [];
      if (u && u.length > 2)
        for (let f = 1, l = u.length, c = 1 <= l; c ? f < l : f > l; c ? f++ : f--)
          n.push((u[f - 1] + u[f]) * 0.5);
      else
        n = g;
      r = n.map((f) => i(f));
    }
    return h[t] && (r = r.map((n) => n[t]())), r;
  }, i.cache = function(e) {
    return e != null ? (M = e, i) : M;
  }, i.gamma = function(e) {
    return e != null ? (N = e, i) : N;
  }, i.nodata = function(e) {
    return e != null ? (x = h(e), i) : x;
  }, i;
}
function I(b, p, x) {
  let L = [], g = b < p, a = p;
  for (let s = b; g ? s < a : s > a; g ? s++ : s--)
    L.push(s);
  return L;
}
export {
  Q as default
};
//# sourceMappingURL=scale.js.map
