# -*- coding: utf-8 -*-
"""The shared accent choices, independent of Kodi and either material family."""
ACCENT_FILL = '$INFO[Window(Home).Property(milionar.accent.fill)]'
ACCENT_INK = '$INFO[Window(Home).Property(milionar.accent.ink)]'
ACCENT_SOFT = '$INFO[Window(Home).Property(milionar.accent.soft)]'
ACCENT_TEXT = '$INFO[Window(Home).Property(milionar.accent.text)]'

PALETTE = (
    ('gold.xml', 'C49A45', 32538),
    ('milionar-kodi-blue.xml', '12B2E7', 32539),
    ('midnight.xml', '2866A4', 32540),
    ('teal.xml', '009688', 32541),
    ('green.xml', '24CA7A', 32542),
    ('orange.xml', 'FF9800', 32543),
    ('maroon.xml', 'C40300', 32545),
    ('pink.xml', 'E91E63', 32546),
    ('violet.xml', 'C050FF', 32547),
    ('milionar-lime.xml', 'D7FF64', 35614),
)


def _luminance(rgb):
    values = [int(rgb[i:i + 2], 16) / 255 for i in (0, 2, 4)]
    return sum((v / 12.92 if v <= .04045 else ((v + .055) / 1.055) ** 2.4) * w
               for v, w in zip(values, (.2126, .7152, .0722)))


def contrast_ink(color):
    """Light or dark text with the higher contrast against an RGB/ARGB fill."""
    light = _luminance(color[-6:])
    dark = 'FF101211'
    return dark if (light + .05) / (_luminance(dark[-6:]) + .05) > 1.05 / (light + .05) else 'FFFFFFFF'


def blend(color, background, opacity):
    """RGB of an accent over a dark panel, for its translucent selected state."""
    return ''.join('%02X' % round(int(color[-6:][i:i+2], 16) * opacity +
                                 int(background[-6:][i:i+2], 16) * (1 - opacity)) for i in (0, 2, 4))


def accent_text(color):
    """Keep the accent hue readable when the accent itself is text on the dark UI."""
    rgb = [int(color[-6:][i:i + 2], 16) for i in (0, 2, 4)]
    for step in range(51):
        result = ''.join('%02X' % round(v + (255 - v) * step / 50) for v in rgb)
        if (_luminance(result) + .05) / (_luminance('26354A') + .05) >= 4.5:
            return 'FF' + result
