import define_default, { extend } from "./define.js";
function Color() {}
var darker = .7, brighter = 1 / darker, reI = "\\s*([+-]?\\d+)\\s*", reN = "\\s*([+-]?(?:\\d*\\.)?\\d+(?:[eE][+-]?\\d+)?)\\s*", reP = "\\s*([+-]?(?:\\d*\\.)?\\d+(?:[eE][+-]?\\d+)?)%\\s*", reHex = /^#([0-9a-f]{3,8})$/, reRgbInteger = /* @__PURE__ */ RegExp(`^rgb\\(${reI},${reI},${reI}\\)$`), reRgbPercent = /* @__PURE__ */ RegExp(`^rgb\\(${reP},${reP},${reP}\\)$`), reRgbaInteger = /* @__PURE__ */ RegExp(`^rgba\\(${reI},${reI},${reI},${reN}\\)$`), reRgbaPercent = /* @__PURE__ */ RegExp(`^rgba\\(${reP},${reP},${reP},${reN}\\)$`), reHslPercent = /* @__PURE__ */ RegExp(`^hsl\\(${reN},${reP},${reP}\\)$`), reHslaPercent = /* @__PURE__ */ RegExp(`^hsla\\(${reN},${reP},${reP},${reN}\\)$`), named = {
	aliceblue: 15792383,
	antiquewhite: 16444375,
	aqua: 65535,
	aquamarine: 8388564,
	azure: 15794175,
	beige: 16119260,
	bisque: 16770244,
	black: 0,
	blanchedalmond: 16772045,
	blue: 255,
	blueviolet: 9055202,
	brown: 10824234,
	burlywood: 14596231,
	cadetblue: 6266528,
	chartreuse: 8388352,
	chocolate: 13789470,
	coral: 16744272,
	cornflowerblue: 6591981,
	cornsilk: 16775388,
	crimson: 14423100,
	cyan: 65535,
	darkblue: 139,
	darkcyan: 35723,
	darkgoldenrod: 12092939,
	darkgray: 11119017,
	darkgreen: 25600,
	darkgrey: 11119017,
	darkkhaki: 12433259,
	darkmagenta: 9109643,
	darkolivegreen: 5597999,
	darkorange: 16747520,
	darkorchid: 10040012,
	darkred: 9109504,
	darksalmon: 15308410,
	darkseagreen: 9419919,
	darkslateblue: 4734347,
	darkslategray: 3100495,
	darkslategrey: 3100495,
	darkturquoise: 52945,
	darkviolet: 9699539,
	deeppink: 16716947,
	deepskyblue: 49151,
	dimgray: 6908265,
	dimgrey: 6908265,
	dodgerblue: 2003199,
	firebrick: 11674146,
	floralwhite: 16775920,
	forestgreen: 2263842,
	fuchsia: 16711935,
	gainsboro: 14474460,
	ghostwhite: 16316671,
	gold: 16766720,
	goldenrod: 14329120,
	gray: 8421504,
	green: 32768,
	greenyellow: 11403055,
	grey: 8421504,
	honeydew: 15794160,
	hotpink: 16738740,
	indianred: 13458524,
	indigo: 4915330,
	ivory: 16777200,
	khaki: 15787660,
	lavender: 15132410,
	lavenderblush: 16773365,
	lawngreen: 8190976,
	lemonchiffon: 16775885,
	lightblue: 11393254,
	lightcoral: 15761536,
	lightcyan: 14745599,
	lightgoldenrodyellow: 16448210,
	lightgray: 13882323,
	lightgreen: 9498256,
	lightgrey: 13882323,
	lightpink: 16758465,
	lightsalmon: 16752762,
	lightseagreen: 2142890,
	lightskyblue: 8900346,
	lightslategray: 7833753,
	lightslategrey: 7833753,
	lightsteelblue: 11584734,
	lightyellow: 16777184,
	lime: 65280,
	limegreen: 3329330,
	linen: 16445670,
	magenta: 16711935,
	maroon: 8388608,
	mediumaquamarine: 6737322,
	mediumblue: 205,
	mediumorchid: 12211667,
	mediumpurple: 9662683,
	mediumseagreen: 3978097,
	mediumslateblue: 8087790,
	mediumspringgreen: 64154,
	mediumturquoise: 4772300,
	mediumvioletred: 13047173,
	midnightblue: 1644912,
	mintcream: 16121850,
	mistyrose: 16770273,
	moccasin: 16770229,
	navajowhite: 16768685,
	navy: 128,
	oldlace: 16643558,
	olive: 8421376,
	olivedrab: 7048739,
	orange: 16753920,
	orangered: 16729344,
	orchid: 14315734,
	palegoldenrod: 15657130,
	palegreen: 10025880,
	paleturquoise: 11529966,
	palevioletred: 14381203,
	papayawhip: 16773077,
	peachpuff: 16767673,
	peru: 13468991,
	pink: 16761035,
	plum: 14524637,
	powderblue: 11591910,
	purple: 8388736,
	rebeccapurple: 6697881,
	red: 16711680,
	rosybrown: 12357519,
	royalblue: 4286945,
	saddlebrown: 9127187,
	salmon: 16416882,
	sandybrown: 16032864,
	seagreen: 3050327,
	seashell: 16774638,
	sienna: 10506797,
	silver: 12632256,
	skyblue: 8900331,
	slateblue: 6970061,
	slategray: 7372944,
	slategrey: 7372944,
	snow: 16775930,
	springgreen: 65407,
	steelblue: 4620980,
	tan: 13808780,
	teal: 32896,
	thistle: 14204888,
	tomato: 16737095,
	turquoise: 4251856,
	violet: 15631086,
	wheat: 16113331,
	white: 16777215,
	whitesmoke: 16119285,
	yellow: 16776960,
	yellowgreen: 10145074
};
define_default(Color, color, {
	copy(c) {
		return Object.assign(new this.constructor(), this, c);
	},
	displayable() {
		return this.rgb().displayable();
	},
	hex: color_formatHex,
	formatHex: color_formatHex,
	formatHex8: color_formatHex8,
	formatHsl: color_formatHsl,
	formatRgb: color_formatRgb,
	toString: color_formatRgb
});
function color_formatHex() {
	return this.rgb().formatHex();
}
function color_formatHex8() {
	return this.rgb().formatHex8();
}
function color_formatHsl() {
	return hslConvert(this).formatHsl();
}
function color_formatRgb() {
	return this.rgb().formatRgb();
}
function color(c) {
	var A, j;
	return c = (c + "").trim().toLowerCase(), (A = reHex.exec(c)) ? (j = A[1].length, A = parseInt(A[1], 16), j === 6 ? rgbn(A) : j === 3 ? new Rgb(A >> 8 & 15 | A >> 4 & 240, A >> 4 & 15 | A & 240, (A & 15) << 4 | A & 15, 1) : j === 8 ? rgba(A >> 24 & 255, A >> 16 & 255, A >> 8 & 255, (A & 255) / 255) : j === 4 ? rgba(A >> 12 & 15 | A >> 8 & 240, A >> 8 & 15 | A >> 4 & 240, A >> 4 & 15 | A & 240, ((A & 15) << 4 | A & 15) / 255) : null) : (A = reRgbInteger.exec(c)) ? new Rgb(A[1], A[2], A[3], 1) : (A = reRgbPercent.exec(c)) ? new Rgb(A[1] * 255 / 100, A[2] * 255 / 100, A[3] * 255 / 100, 1) : (A = reRgbaInteger.exec(c)) ? rgba(A[1], A[2], A[3], A[4]) : (A = reRgbaPercent.exec(c)) ? rgba(A[1] * 255 / 100, A[2] * 255 / 100, A[3] * 255 / 100, A[4]) : (A = reHslPercent.exec(c)) ? hsla(A[1], A[2] / 100, A[3] / 100, 1) : (A = reHslaPercent.exec(c)) ? hsla(A[1], A[2] / 100, A[3] / 100, A[4]) : named.hasOwnProperty(c) ? rgbn(named[c]) : c === "transparent" ? new Rgb(NaN, NaN, NaN, 0) : null;
}
function rgbn(c) {
	return new Rgb(c >> 16 & 255, c >> 8 & 255, c & 255, 1);
}
function rgba(c, A, j, M) {
	return M <= 0 && (c = A = j = NaN), new Rgb(c, A, j, M);
}
function rgbConvert(c) {
	return c instanceof Color || (c = color(c)), c ? (c = c.rgb(), new Rgb(c.r, c.g, c.b, c.opacity)) : new Rgb();
}
function rgb(c, A, j, M) {
	return arguments.length === 1 ? rgbConvert(c) : new Rgb(c, A, j, M ?? 1);
}
function Rgb(c, A, j, M) {
	this.r = +c, this.g = +A, this.b = +j, this.opacity = +M;
}
define_default(Rgb, rgb, extend(Color, {
	brighter(c) {
		return c = c == null ? brighter : brighter ** +c, new Rgb(this.r * c, this.g * c, this.b * c, this.opacity);
	},
	darker(c) {
		return c = c == null ? darker : darker ** +c, new Rgb(this.r * c, this.g * c, this.b * c, this.opacity);
	},
	rgb() {
		return this;
	},
	clamp() {
		return new Rgb(clampi(this.r), clampi(this.g), clampi(this.b), clampa(this.opacity));
	},
	displayable() {
		return -.5 <= this.r && this.r < 255.5 && -.5 <= this.g && this.g < 255.5 && -.5 <= this.b && this.b < 255.5 && 0 <= this.opacity && this.opacity <= 1;
	},
	hex: rgb_formatHex,
	formatHex: rgb_formatHex,
	formatHex8: rgb_formatHex8,
	formatRgb: rgb_formatRgb,
	toString: rgb_formatRgb
}));
function rgb_formatHex() {
	return `#${hex(this.r)}${hex(this.g)}${hex(this.b)}`;
}
function rgb_formatHex8() {
	return `#${hex(this.r)}${hex(this.g)}${hex(this.b)}${hex((isNaN(this.opacity) ? 1 : this.opacity) * 255)}`;
}
function rgb_formatRgb() {
	let c = clampa(this.opacity);
	return `${c === 1 ? "rgb(" : "rgba("}${clampi(this.r)}, ${clampi(this.g)}, ${clampi(this.b)}${c === 1 ? ")" : `, ${c})`}`;
}
function clampa(c) {
	return isNaN(c) ? 1 : Math.max(0, Math.min(1, c));
}
function clampi(c) {
	return Math.max(0, Math.min(255, Math.round(c) || 0));
}
function hex(c) {
	return c = clampi(c), (c < 16 ? "0" : "") + c.toString(16);
}
function hsla(c, A, j, M) {
	return M <= 0 ? c = A = j = NaN : j <= 0 || j >= 1 ? c = A = NaN : A <= 0 && (c = NaN), new Hsl(c, A, j, M);
}
function hslConvert(c) {
	if (c instanceof Hsl) return new Hsl(c.h, c.s, c.l, c.opacity);
	if (c instanceof Color || (c = color(c)), !c) return new Hsl();
	if (c instanceof Hsl) return c;
	c = c.rgb();
	var A = c.r / 255, M = c.g / 255, N = c.b / 255, P = Math.min(A, M, N), F = Math.max(A, M, N), I = NaN, L = F - P, R = (F + P) / 2;
	return L ? (I = A === F ? (M - N) / L + (M < N) * 6 : M === F ? (N - A) / L + 2 : (A - M) / L + 4, L /= R < .5 ? F + P : 2 - F - P, I *= 60) : L = R > 0 && R < 1 ? 0 : I, new Hsl(I, L, R, c.opacity);
}
function hsl(c, A, j, M) {
	return arguments.length === 1 ? hslConvert(c) : new Hsl(c, A, j, M ?? 1);
}
function Hsl(c, A, j, M) {
	this.h = +c, this.s = +A, this.l = +j, this.opacity = +M;
}
define_default(Hsl, hsl, extend(Color, {
	brighter(c) {
		return c = c == null ? brighter : brighter ** +c, new Hsl(this.h, this.s, this.l * c, this.opacity);
	},
	darker(c) {
		return c = c == null ? darker : darker ** +c, new Hsl(this.h, this.s, this.l * c, this.opacity);
	},
	rgb() {
		var c = this.h % 360 + (this.h < 0) * 360, A = isNaN(c) || isNaN(this.s) ? 0 : this.s, j = this.l, M = j + (j < .5 ? j : 1 - j) * A, N = 2 * j - M;
		return new Rgb(hsl2rgb(c >= 240 ? c - 240 : c + 120, N, M), hsl2rgb(c, N, M), hsl2rgb(c < 120 ? c + 240 : c - 120, N, M), this.opacity);
	},
	clamp() {
		return new Hsl(clamph(this.h), clampt(this.s), clampt(this.l), clampa(this.opacity));
	},
	displayable() {
		return (0 <= this.s && this.s <= 1 || isNaN(this.s)) && 0 <= this.l && this.l <= 1 && 0 <= this.opacity && this.opacity <= 1;
	},
	formatHsl() {
		let c = clampa(this.opacity);
		return `${c === 1 ? "hsl(" : "hsla("}${clamph(this.h)}, ${clampt(this.s) * 100}%, ${clampt(this.l) * 100}%${c === 1 ? ")" : `, ${c})`}`;
	}
}));
function clamph(c) {
	return c = (c || 0) % 360, c < 0 ? c + 360 : c;
}
function clampt(c) {
	return Math.max(0, Math.min(1, c || 0));
}
function hsl2rgb(c, A, j) {
	return (c < 60 ? A + (j - A) * c / 60 : c < 180 ? j : c < 240 ? A + (j - A) * (240 - c) / 60 : A) * 255;
}
export { Color, Rgb, color as default, hsl, rgb, rgbConvert };
