import w, { extend as y } from "./define.js";
import { Rgb as N, rgbConvert as q, Color as m } from "./color.js";
import { degrees as H, radians as K } from "./math.js";
const l = 18, d = 0.96422, v = 1, M = 0.82521, o = 4 / 29, h = 6 / 29, z = 3 * h * h, L = h * h * h;
function C(t) {
  if (t instanceof e) return new e(t.l, t.a, t.b, t.opacity);
  if (t instanceof a) return x(t);
  t instanceof N || (t = q(t));
  var n = g(t.r), i = g(t.g), r = g(t.b), s = b((0.2225045 * n + 0.7168786 * i + 0.0606169 * r) / v), c, u;
  return n === i && i === r ? c = u = s : (c = b((0.4360747 * n + 0.3850649 * i + 0.1430804 * r) / d), u = b((0.0139322 * n + 0.0971045 * i + 0.7141733 * r) / M)), new e(116 * s - 16, 500 * (c - s), 200 * (s - u), t.opacity);
}
function R(t, n, i, r) {
  return arguments.length === 1 ? C(t) : new e(t, n, i, r ?? 1);
}
function e(t, n, i, r) {
  this.l = +t, this.a = +n, this.b = +i, this.opacity = +r;
}
w(e, R, y(m, {
  brighter(t) {
    return new e(this.l + l * (t ?? 1), this.a, this.b, this.opacity);
  },
  darker(t) {
    return new e(this.l - l * (t ?? 1), this.a, this.b, this.opacity);
  },
  rgb() {
    var t = (this.l + 16) / 116, n = isNaN(this.a) ? t : t + this.a / 500, i = isNaN(this.b) ? t : t - this.b / 200;
    return n = d * f(n), t = v * f(t), i = M * f(i), new N(
      p(3.1338561 * n - 1.6168667 * t - 0.4906146 * i),
      p(-0.9787684 * n + 1.9161415 * t + 0.033454 * i),
      p(0.0719453 * n - 0.2289914 * t + 1.4052427 * i),
      this.opacity
    );
  }
}));
function b(t) {
  return t > L ? Math.pow(t, 1 / 3) : t / z + o;
}
function f(t) {
  return t > h ? t * t * t : z * (t - o);
}
function p(t) {
  return 255 * (t <= 31308e-7 ? 12.92 * t : 1.055 * Math.pow(t, 1 / 2.4) - 0.055);
}
function g(t) {
  return (t /= 255) <= 0.04045 ? t / 12.92 : Math.pow((t + 0.055) / 1.055, 2.4);
}
function X(t) {
  if (t instanceof a) return new a(t.h, t.c, t.l, t.opacity);
  if (t instanceof e || (t = C(t)), t.a === 0 && t.b === 0) return new a(NaN, 0 < t.l && t.l < 100 ? 0 : NaN, t.l, t.opacity);
  var n = Math.atan2(t.b, t.a) * H;
  return new a(n < 0 ? n + 360 : n, Math.sqrt(t.a * t.a + t.b * t.b), t.l, t.opacity);
}
function Y(t, n, i, r) {
  return arguments.length === 1 ? X(t) : new a(t, n, i, r ?? 1);
}
function a(t, n, i, r) {
  this.h = +t, this.c = +n, this.l = +i, this.opacity = +r;
}
function x(t) {
  if (isNaN(t.h)) return new e(t.l, 0, 0, t.opacity);
  var n = t.h * K;
  return new e(t.l, Math.cos(n) * t.c, Math.sin(n) * t.c, t.opacity);
}
w(a, Y, y(m, {
  brighter(t) {
    return new a(this.h, this.c, this.l + l * (t ?? 1), this.opacity);
  },
  darker(t) {
    return new a(this.h, this.c, this.l - l * (t ?? 1), this.opacity);
  },
  rgb() {
    return x(this).rgb();
  }
}));
export {
  a as Hcl,
  e as Lab,
  R as default,
  Y as hcl
};
