Functional Weave
Code in Rust

charts.palette@1.0.0

impl/python.py

944 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from typing import List

from .charts_palette_data import PALETTES  ← this capability’s own data, compiled from data/palettes.json into the same file by fune build


def palette(scheme: str, count: int, cycle: bool) -> List[str]:
    """The first ``count`` colours of a named categorical scheme, in its order.

    Asking for more colours than the scheme has is an error unless ``cycle``
    says repeating them is acceptable.
    """
    colours = [c.hex for c in sorted((c for c in PALETTES if c.scheme == scheme), key=lambda c: c.position)]
    if not colours:
        raise ValueError('unknown palette "%s"; use okabe-ito or tol-bright' % (scheme,))
    if isinstance(count, bool) or not isinstance(count, int) or count < 0:
        raise ValueError("count must be a whole number of at least 0, received %r" % (count,))
    if count > len(colours) and not cycle:
        raise ValueError('palette "%s" has only %d colours; pass cycle to repeat them' % (scheme, len(colours)))
    return [colours[i % len(colours)] for i in range(count)]