blob: cee78bcd0be4f17096f9f9884f8b8b78f7b7f1e7 [file] [log] [blame]
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
def constrain(value, min_inclusive, max_inclusive):
"""Constrain a value to a given inclusive range.
This function is expected to be used with integers but should work with any
comparable values.
value: the value to constrain.
min_inclusive, max_inclusive: the range to constrain the value to.
(constrained_value, constrained): The value after constraining, plus a
boolean value indicating whether constraining was necessary.
assert min_inclusive <= max_inclusive
if value < min_inclusive:
return min_inclusive, True
elif value > max_inclusive:
return max_inclusive, True
return value, False