Lea Verou for this neat trick. One thing I changed about it is made it use slight transparency if the contrast color is black, so it doesn’t look too ugly. The code is as follows:
--l: clamp(0, (l / 0.623 - 1) * -infinity, 1); --a: calc(var(--l) + (var(--dim-opacity) * (1 - var(--l)))); --contrast-color: oklch(from var(--accent-color) var(--l) 0 h / var(--a));