
def text_color(hex_color):
    """Dark or white text, depending on how bright the card color is."""
    try:
        h = hex_color.lstrip("#")
        if len(h) == 3:
            h = "".join(ch * 2 for ch in h)
        r, g, b = (int(h[i:i + 2], 16) for i in (0, 2, 4))
    except (ValueError, AttributeError):
        return "#111827"
    return "#111827" if (0.299 * r + 0.587 * g + 0.114 * b) / 255 > 0.6 else "#ffffff"