import { palettes as f } from "./palette.js";
import { Color as s } from "./color.js";
s.fromHex = (t) => {
  t = t.replace("#", "");
  let r, o, n, e = 1;
  if (t.length === 6)
    r = parseInt(t.slice(0, 2), 16), o = parseInt(t.slice(2, 4), 16), n = parseInt(t.slice(4, 6), 16);
  else if (t.length === 8)
    r = parseInt(t.slice(0, 2), 16), o = parseInt(t.slice(2, 4), 16), n = parseInt(t.slice(4, 6), 16), e = parseInt(t.slice(6, 8), 16) / 255;
  else
    throw new Error("Invalid HEX color format.");
  return s(r, o, n, e);
};
s.fromString = (t) => {
  if (t.startsWith("#"))
    return s.fromHex(t);
  throw Error("TODO: implement rgb(a), hsl");
};
function l(t, r, o) {
  return t + o * (r - t);
}
function u(t, r, o) {
  const n = Math.round(l(t.r, r.r, o)), e = Math.round(l(t.g, r.g, o)), i = Math.round(l(t.b, r.b, o));
  return s(n, e, i);
}
function m(t) {
  return typeof t == "string" ? f[t].map((r) => s.fromString(r)) : t.map((r) => typeof r == "string" ? s.fromString(r) : r);
}
function h(t) {
  return new class {
    constructor(r) {
      this.colors = r;
    }
    /**
     * Samples a color at a specific point in the gradient.
     *
     * @param {number} t - A value in [0, 1] representing the position in the gradient.
     */
    fromInterval(r) {
      if (r < 0) throw new Error("t must be greater than or equal to 0");
      if (r > 1) throw new Error("t must be less than or equal to 1");
      const o = this.colors, n = o.length - 1, e = Math.floor(r * n), i = r * n % 1, a = o[e], c = o[Math.min(e + 1, n)];
      return u(a, c, i);
    }
    /**
     * Gets the nth color in a gradient divided into segments.
     *
     * @param {number} n - Index of the color (1-based).
     * @param {number} segments - Total number of segments.
     */
    getNthOf(r, o) {
      if (r <= 0) throw new Error("n must be greater than 0");
      if (r > o) throw Error("n must be lower or equal than count of segments");
      return this.fromInterval((r - 1) / (o - 1));
    }
    /**
     * Splits the gradient into n evenly spaced colors.
     */
    split(r) {
      if (r <= 0) throw new Error("n must be greater than 0");
      const o = [];
      for (let n = 0; n < r; n++) {
        const e = n / (r - 1);
        o.push(this.fromInterval(e));
      }
      return o;
    }
  }(m(t));
}
export {
  h as Gradient,
  u as interpolateColor,
  m as normalizeGradient
};
//# sourceMappingURL=gradient.js.map
