def encode_color(color, n_bits):
    """
    If color is given as boolean, interpret it as maximally on or totally off.
    Otherwise check whether it fits within the number of bits.
    """
    if isinstance(color, bool):
        return (2 ** n_bits - 1) * color
    elif 0 <= color < 2 ** n_bits:
        return color
    else:
        raise ValueError(
            "`color` should be a boolean or be between 0 and 2 ** n_bits - 1"
        )