import x from "../Color.js";
import v from "../utils/clip_rgb.js";
import "../utils/type.js";
const { pow: y, sqrt: N, PI: i, cos: z, sin: m, atan2: E } = Math, I = (f, c = "lrgb", n = null) => {
  const l = f.length;
  n || (n = Array.from(new Array(l)).map(() => 1));
  const p = l / n.reduce(function(t, a) {
    return t + a;
  });
  if (n.forEach((t, a) => {
    n[a] *= p;
  }), f = f.map((t) => new x(t)), c === "lrgb")
    return g(f, n);
  const u = f.shift(), r = u.get(c), e = [];
  let A = 0, b = 0;
  for (let t = 0; t < r.length; t++)
    if (r[t] = (r[t] || 0) * n[0], e.push(isNaN(r[t]) ? 0 : n[0]), c.charAt(t) === "h" && !isNaN(r[t])) {
      const a = r[t] / 180 * i;
      A += z(a) * n[0], b += m(a) * n[0];
    }
  let h = u.alpha() * n[0];
  f.forEach((t, a) => {
    const s = t.get(c);
    h += t.alpha() * n[a + 1];
    for (let o = 0; o < r.length; o++)
      if (!isNaN(s[o]))
        if (e[o] += n[a + 1], c.charAt(o) === "h") {
          const _ = s[o] / 180 * i;
          A += z(_) * n[a + 1], b += m(_) * n[a + 1];
        } else
          r[o] += s[o] * n[a + 1];
  });
  for (let t = 0; t < r.length; t++)
    if (c.charAt(t) === "h") {
      let a = E(b / e[t], A / e[t]) / i * 180;
      for (; a < 0; ) a += 360;
      for (; a >= 360; ) a -= 360;
      r[t] = a;
    } else
      r[t] = r[t] / e[t];
  return h /= l, new x(r, c).alpha(h > 0.99999 ? 1 : h, !0);
}, g = (f, c) => {
  const n = f.length, l = [0, 0, 0, 0];
  for (let p = 0; p < f.length; p++) {
    const u = f[p], r = c[p] / n, e = u._rgb;
    l[0] += y(e[0], 2) * r, l[1] += y(e[1], 2) * r, l[2] += y(e[2], 2) * r, l[3] += e[3] * r;
  }
  return l[0] = N(l[0]), l[1] = N(l[1]), l[2] = N(l[2]), l[3] > 0.9999999 && (l[3] = 1), new x(v(l));
};
export {
  I as default
};
//# sourceMappingURL=average.js.map
