import { palettes as e } from "./palette.js";
import { Color as t } from "./color.js";
/**
* Parses a color string (attention: currently supports only HEX, @todo)
*/
t.fromHex = (e) => {
	e = e.replace("#", "");
	let n, r, i, a = 1;
	if (e.length === 6) n = parseInt(e.slice(0, 2), 16), r = parseInt(e.slice(2, 4), 16), i = parseInt(e.slice(4, 6), 16);
	else if (e.length === 8) n = parseInt(e.slice(0, 2), 16), r = parseInt(e.slice(2, 4), 16), i = parseInt(e.slice(4, 6), 16), a = parseInt(e.slice(6, 8), 16) / 255;
	else throw Error("Invalid HEX color format.");
	return t(n, r, i, a);
}, t.fromString = (e) => {
	if (e.startsWith("#")) return t.fromHex(e);
	throw Error("TODO: implement rgb(a), hsl");
};
function n(e, t, n) {
	return e + n * (t - e);
}
/**
* Interpolates between two colors.
*
* @param {Color} color1 - Start color.
* @param {Color} color2 - End color.
* @param {number} t - Interpolation factor [0, 1].
* @returns {Color} Interpolated color.
*/
function r(e, r, i) {
	return t(Math.round(n(e.r, r.r, i)), Math.round(n(e.g, r.g, i)), Math.round(n(e.b, r.b, i)));
}
/**
* Normalizes a gradient definition into an array of Color objects.
*
* @param {GradientSource} raw - A gradient defined as an array of strings, Colors, or a Palette.
* @returns {Color[]} Array of normalized Color objects.
*/
function i(n) {
	return typeof n == "string" ? e[n].map((e) => t.fromString(e)) : n.map((e) => typeof e == "string" ? t.fromString(e) : e);
}
/**
* Creates a gradient with utilities to sample or split colors.
*/
function a(e) {
	return new class {
		constructor(e) {
			this.colors = e;
		}
		/**
		* 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(e) {
			if (e < 0) throw Error("t must be greater than or equal to 0");
			if (e > 1) throw Error("t must be less than or equal to 1");
			let t = this.colors, n = t.length - 1, i = Math.floor(e * n), a = e * n % 1, o = t[i], s = t[Math.min(i + 1, n)];
			return r(o, s, a);
		}
		/**
		* 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(e, t) {
			if (e <= 0) throw Error("n must be greater than 0");
			if (e > t) throw Error("n must be lower or equal than count of segments");
			return this.fromInterval((e - 1) / (t - 1));
		}
		/**
		* Splits the gradient into n evenly spaced colors.
		*/
		split(e) {
			if (e <= 0) throw Error("n must be greater than 0");
			let t = [];
			for (let n = 0; n < e; n++) {
				let r = n / (e - 1);
				t.push(this.fromInterval(r));
			}
			return t;
		}
	}(i(e));
}
export { a as Gradient, r as interpolateColor, i as normalizeGradient };

//# sourceMappingURL=gradient.js.map