Functional Weave
Code in TypeScript

charts.color@1.0.1

impl/python/to_hex.py

489 bytes · the Python implementation · view raw

from .charts_color_types import Rgb

_HEX = "0123456789abcdef"


def _pair(value: int) -> str:
    if isinstance(value, bool) or not isinstance(value, int) or value < 0 or value > 255:
        raise ValueError("rgb channels must be whole numbers from 0 to 255, received %r" % (value,))
    return _HEX[value // 16] + _HEX[value % 16]


def to_hex(rgb: Rgb) -> str:
    """Lowercase "#rrggbb", the form every renderer accepts."""
    return "#" + _pair(rgb.r) + _pair(rgb.g) + _pair(rgb.b)