def rescale(values, new_range, original_range):
    """
    `values` is a list of numbers. Rescale the numbers in `values` so that
    they are always between `new_range` and `original_range`.
    """
    if new_range is None:
        return values

    if new_range == original_range:
        return values

    rescaled_values = []
    for value in values:
        original_range_size = (original_range[1] - original_range[0])
        if (original_range_size == 0):
            new_value = new_range[0]
        else:
            new_range_size = (new_range[1] - new_range[0])
            new_value = (((value - original_range[0]) * new_range_size) / original_range_size) + \
                       new_range[0]
        rescaled_values.append(new_value)
    return rescaled_values