def constrain(value, min_val, max_val):
    """ Constrains value to a specific range. """
    if value < min_val:
        return min_val
    elif value > max_val:
        return max_val
    else:
        return value