diff options
Diffstat (limited to 'third_party/rust/image/src/math/utils.rs')
-rw-r--r-- | third_party/rust/image/src/math/utils.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/third_party/rust/image/src/math/utils.rs b/third_party/rust/image/src/math/utils.rs new file mode 100644 index 0000000000..1ddea5dbc2 --- /dev/null +++ b/third_party/rust/image/src/math/utils.rs @@ -0,0 +1,24 @@ +//! Shared mathematical utility functions. + +/// Cut value to be inside given range +/// +/// ``` +/// use image::math::utils; +/// +/// assert_eq!(utils::clamp(-5, 0, 10), 0); +/// assert_eq!(utils::clamp( 6, 0, 10), 6); +/// assert_eq!(utils::clamp(15, 0, 10), 10); +/// ``` +#[inline] +pub fn clamp<N>(a: N, min: N, max: N) -> N +where + N: PartialOrd, +{ + if a < min { + return min; + } + if a > max { + return max; + } + a +} |