import sass
from PIL import ImageColor


def hex2sassColor(color):
    try:
        color = ImageColor.getrgb(color)
        # No alpha is given
        if len(color) == 3:
            return sass.SassColor(*color, a=1)
        return sass.SassColor(*color)
    except ValueError:
        raise ValueError("hex2rgba can only parse hex colors.")


def ocyanvariable(variable):
    from oxyan.themes import definitions

    return definitions.get(variable)


def ocyanlist(name):
    from oxyan.themes import definitions

    _list = definitions.get(name, [])
    return sass.SassList(_list, sass.SASS_SEPARATOR_COMMA, False)


def ocyannumber(name, unit):
    from oxyan.themes import definitions

    number = definitions.get(name)
    return sass.SassNumber(number, unit)


def ocyanbarenumber(name):
    return ocyannumber(name, "")


def ocyancolor(name):
    from oxyan.themes import definitions

    color = definitions.get(name)
    return hex2sassColor(color)
