// WCAG contrast of the text candidates on Advance's brand colour.
//
// `export {}` makes this a module rather than a global script. Without it the
// functions below are globals, and tsconfig's `**/*.ts` reaches into the agent
// worktrees under .claude/, so a second copy of this file collides with this
// one as a duplicate implementation.
export {}

function oklchToSrgb(L: number, C: number, hDeg: number) {
  const h = (hDeg * Math.PI) / 180
  const a = C * Math.cos(h),
    b = C * Math.sin(h)
  const l_ = L + 0.3963377774 * a + 0.2158037573 * b
  const m_ = L - 0.1055613458 * a - 0.0638541728 * b
  const s_ = L - 0.0894841775 * a - 1.291485548 * b
  const l = l_ ** 3,
    m = m_ ** 3,
    s = s_ ** 3
  const r = +4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s
  const g = -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s
  const bb = -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s
  const enc = (x: number) =>
    x <= 0.0031308 ? 12.92 * x : 1.055 * x ** (1 / 2.4) - 0.055
  return [enc(r), enc(g), enc(bb)].map((v) => Math.min(1, Math.max(0, v)))
}
function luminance([r, g, b]: number[]) {
  const lin = (v: number) =>
    v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4
  return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b)
}
function ratio(a: number[], b: number[]) {
  const [x, y] = [luminance(a), luminance(b)].sort((p, q) => q - p)
  return (x + 0.05) / (y + 0.05)
}
const hex = (c: number[]) =>
  "#" +
  c
    .map((v) =>
      Math.round(v * 255)
        .toString(16)
        .padStart(2, "0"),
    )
    .join("")

const brand = oklchToSrgb(0.8032, 0.1315, 192.39)
console.log("marque Advance :", hex(brand))
const candidats: [string, number[]][] = [
  ["ink 0.145 (foreground clair)", oklchToSrgb(0.145, 0, 0)],
  ["ink 0.205", oklchToSrgb(0.205, 0, 0)],
  ["blanc 0.985", oklchToSrgb(0.985, 0, 0)],
]
for (const [nom, c] of candidats) {
  console.log(`  ${nom.padEnd(30)} ${hex(c)}  ${ratio(brand, c).toFixed(2)}:1`)
}
// la marque comme texte sur les deux fonds
for (const [nom, bg] of [
  ["fond sombre 0.145", oklchToSrgb(0.145, 0, 0)],
  ["fond clair 1", oklchToSrgb(1, 0, 0)],
] as [string, number[]][]) {
  console.log(
    `marque en texte sur ${nom.padEnd(20)} ${ratio(brand, bg).toFixed(2)}:1`,
  )
}

// survol : la marque a 80 % par-dessus la carte sombre
function over(fg: number[], bg: number[], alpha: number) {
  return fg.map((c, i) => c * alpha + bg[i] * (1 - alpha))
}
const carteSombre = oklchToSrgb(0.205, 0, 0)
const survol = over(brand, carteSombre, 0.8)
console.log("\nsurvol (marque 80% sur carte sombre) :", hex(survol))
console.log(
  "  encre sombre dessus :",
  ratio(survol, oklchToSrgb(0.145, 0, 0)).toFixed(2) + ":1",
)
