def hex2web()

in chartify/_core/colour.py [0:0]


def hex2web(hex):
    """Converts HEX representation to WEB

    :param rgb: 3 hex char or 6 hex char string representation
    :rtype: web string representation (human readable if possible)

    WEB representation uses X11 rgb.txt to define conversion
    between RGB and english color names.

    Usage
    =====

    >>> from colour import hex2web

    >>> hex2web('#ff0000')
    'red'

    >>> hex2web('#aaaaaa')
    '#aaa'

    >>> hex2web('#abc')
    '#abc'

    >>> hex2web('#acacac')
    '#acacac'

    """
    dec_rgb = tuple(int(v * 255) for v in hex2rgb(hex))
    if dec_rgb in RGB_TO_COLOR_NAMES:
        # take the first one
        color_name = RGB_TO_COLOR_NAMES[dec_rgb][0]
        # Enforce full lowercase for single worded color name.
        return color_name if len(re.sub(r"[^A-Z]", "", color_name)) > 1 else color_name.lower()

    # Hex format is verified by hex2rgb function. And should be 3 or 6 digit
    if len(hex) == 7:
        if hex[1] == hex[2] and hex[3] == hex[4] and hex[5] == hex[6]:
            return "#" + hex[1] + hex[3] + hex[5]
    return hex