Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///
- public protocol ClampSupport {
- func clamped(from lowerBound: Self, to upperBound: Self) -> Self
- func clamped(from lowerBound: Self) -> Self
- func clamped(to lowerBound: Self) -> Self
- }
- public extension ClampSupport where Self: Comparable {
- @_transparent
- func clamped(from lowerBound: Self, to upperBound: Self) -> Self {
- return min(max(self, lowerBound), upperBound)
- }
- @_transparent
- func clamped(from lowerBound: Self) -> Self {
- return max(self, lowerBound)
- }
- @_transparent
- func clamped(to lowerBound: Self) -> Self {
- return min(self, lowerBound)
- }
- @_transparent
- func clamped(to range: ClosedRange<Self>) -> Self {
- return self.clamped(from: range.lowerBound, to: range.upperBound)
- }
- @_transparent
- func clamped(from range: PartialRangeFrom<Self>) -> Self {
- return self.clamped(from: range.lowerBound)
- }
- @_transparent
- func clamped(to range: PartialRangeThrough<Self>) -> Self {
- return self.clamped(to: range.upperBound)
- }
- }
Add Comment
Please, Sign In to add comment