from operator import add
from operator import sub


def brighten(rgb, values=(20, 20, 20)):
    """
    Brighten an RGB color.

    If the brightened color is over (255, 255, 255), subtract `values` instead of adding
    `values`, effectively dimming the color. If this results in a negative number,
    return the original color. This is implemented to guarantee a valid (and hopefully
    different) color is returned.

    Parameters
    ----------
    rgb : tuple[int]
        An RGB tuple.
    values : tuple[int], optional
        Amount of brightening to apply to R, G, B respectively.

    Returns
    -------
    tuple[int]
        A brightened RGB color.
    """
    # Apply brightness.
    br_color = tuple(map(add, rgb, values))
    if any(i > 255 for i in br_color):
        br_color = tuple(map(sub, rgb, values))
    if any(i < 0 for i in br_color):
        br_color = rgb

    # Make sure elements are ints.
    br_color = tuple(map(round, br_color))

    return br_color