Пример #1
0
    def _filter_color(color):
        if color is None:
            return None

        try:
            return Style256._color_table[color]
        except KeyError:
            pass

        c = Color.from_string(color).rgb_channels(as_floats=True)
        return min(Style256._rgb_color_table.items(),
                   key=lambda item: color_distance(item[0][0], item[0][1],
                                                   item[0][2], *c))[1]
Пример #2
0
class Style256(AbstractStyle):
    """256-color style.

    Accepts any rgb color in hex format, for instance:

    .. code-block:: pycon

        >>> Style256(color='#abcdef')

    Or by css name:

    .. code-block:: pycon

        >>> Style256(color='chocolate')

    In case of a color being outside of standard xterm 256 color palette,
    it'll try to locate the closest color in it.
    """

    _color_table = {v: k for k, v in _MAP256.items()}
    _rcolor_table = _MAP256

    _rgb_color_table = {
        Color.from_string(v).rgb_channels(as_floats=True): k
        for k, v in _MAP256.items()
    }

    @staticmethod
    @functools.lru_cache(500)
    def _filter_color(color):
        if color is None:
            return None

        try:
            return Style256._color_table[color]
        except KeyError:
            pass

        c = Color.from_string(color).rgb_channels(as_floats=True)
        return min(
            Style256._rgb_color_table.items(),
            key=lambda item: color_distance(item[0][0], item[0][1],
                                            item[0][2], *c))[1]