def clamp(value, minval, maxval):
    """Return a value clamped to the given interval [minval, maxval]."""
    minval, maxval = min(minval, maxval), max(minval, maxval)
    return min(max(value, minval), maxval)