"""WCAG 2.2 relative luminance and contrast ratio, exactly as quoted from https://www.w3.org/TR/WCAG22/ (Rec 12 Dec 2024)."""
def hex_rgb(h): h = h.lstrip('#'); return tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
def rgb_hex(c): return '#%02X%02X%02X' % tuple(max(0, min(255, int(v + 0.5))) for v in c)
def _lin(v8):
    s = v8 / 255
    return s / 12.92 if s <= 0.04045 else ((s + 0.055) / 1.055) ** 2.4
def lum(c): return 0.2126 * _lin(c[0]) + 0.7152 * _lin(c[1]) + 0.0722 * _lin(c[2])
def ratio(a, b):
    x, y = lum(a), lum(b)
    return (max(x, y) + 0.05) / (min(x, y) + 0.05)
def mix(c, to, t): return tuple(c[i] + (to[i] - c[i]) * t for i in range(3))
