diff --git a/BXSwiftUtils.xcodeproj/project.pbxproj b/BXSwiftUtils.xcodeproj/project.pbxproj index 1357742..2b837e1 100644 --- a/BXSwiftUtils.xcodeproj/project.pbxproj +++ b/BXSwiftUtils.xcodeproj/project.pbxproj @@ -59,6 +59,7 @@ 48DF72F520697E1E00D224AA /* TypedKVOTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48DF72F420697E1E00D224AA /* TypedKVOTests.swift */; }; 48FF9BC820E116B100024C10 /* Weak.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48FF9BC620E116B100024C10 /* Weak.swift */; }; 48FF9BCA20E1174C00024C10 /* Weak.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48FF9BC920E1174C00024C10 /* Weak.swift */; }; + 95CBED9321A5B4D100344220 /* Comparable+Clip.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95CBED9221A5B4D100344220 /* Comparable+Clip.swift */; }; D0149008213D566500A38870 /* KVO+propagateChanges.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0149006213D566500A38870 /* KVO+propagateChanges.swift */; }; D03234EB205BF939003D6CCB /* URL+Info.swift in Sources */ = {isa = PBXBuildFile; fileRef = D03234E9205BF938003D6CCB /* URL+Info.swift */; }; D03234EF205BFC9F003D6CCB /* String+Localized.swift in Sources */ = {isa = PBXBuildFile; fileRef = D03234ED205BFC9F003D6CCB /* String+Localized.swift */; }; @@ -153,6 +154,7 @@ 48DF72F420697E1E00D224AA /* TypedKVOTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypedKVOTests.swift; sourceTree = ""; }; 48FF9BC620E116B100024C10 /* Weak.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Weak.swift; sourceTree = ""; }; 48FF9BC920E1174C00024C10 /* Weak.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Weak.swift; sourceTree = ""; }; + 95CBED9221A5B4D100344220 /* Comparable+Clip.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Comparable+Clip.swift"; sourceTree = ""; }; D0149006213D566500A38870 /* KVO+propagateChanges.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "KVO+propagateChanges.swift"; sourceTree = ""; }; D03234E9205BF938003D6CCB /* URL+Info.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "URL+Info.swift"; sourceTree = ""; }; D03234ED205BFC9F003D6CCB /* String+Localized.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Localized.swift"; sourceTree = ""; }; @@ -464,6 +466,7 @@ D089124A215B723C00403100 /* CGRect+String.swift */, 481A25BC2175D36000FB8E9A /* CGPath+RoundedRect.swift */, 481A25BF2175D38700FB8E9A /* CACornerMask+Convenience.swift */, + 95CBED9221A5B4D100344220 /* Comparable+Clip.swift */, ); path = "Math & Geometry"; sourceTree = ""; @@ -741,6 +744,7 @@ 48A971E22159024100215F9F /* Enum+Comparable.swift in Sources */, D06F462E209073D0000986B8 /* Collection+Codable.swift in Sources */, D03234EF205BFC9F003D6CCB /* String+Localized.swift in Sources */, + 95CBED9321A5B4D100344220 /* Comparable+Clip.swift in Sources */, D046DF85216B7ED0004331C3 /* Optional+If.swift in Sources */, D03234FE205C2919003D6CCB /* CGRect+Accessors.swift in Sources */, D032350E205C362A003D6CCB /* Date+Components.swift in Sources */, @@ -935,6 +939,7 @@ INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_DYLIB_INSTALL_NAME = "$(DYLIB_INSTALL_NAME_BASE:standardizepath)/$(EXECUTABLE_PATH)"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + MACOSX_DEPLOYMENT_TARGET = 10.14; PRODUCT_BUNDLE_IDENTIFIER = com.boinx.BXSwiftUtils; PRODUCT_NAME = BXSwiftUtils; SKIP_INSTALL = YES; @@ -964,6 +969,7 @@ INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_DYLIB_INSTALL_NAME = "$(DYLIB_INSTALL_NAME_BASE:standardizepath)/$(EXECUTABLE_PATH)"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + MACOSX_DEPLOYMENT_TARGET = 10.14; PRODUCT_BUNDLE_IDENTIFIER = com.boinx.BXSwiftUtils; PRODUCT_NAME = BXSwiftUtils; SKIP_INSTALL = YES; diff --git a/BXSwiftUtils/Math & Geometry/Comparable+Clip.swift b/BXSwiftUtils/Math & Geometry/Comparable+Clip.swift new file mode 100644 index 0000000..4adc5a3 --- /dev/null +++ b/BXSwiftUtils/Math & Geometry/Comparable+Clip.swift @@ -0,0 +1,33 @@ +// +// Comparable+Clip.swift +// BXSwiftUtils +// +// Created by Benjamin Federer on 21.11.18. +// Copyright © 2018 Boinx Software Ltd. & Imagine GbR. All rights reserved. +// + +import Foundation + +extension Comparable +{ + /// Returns `self` clipped to the closed interval `[minValue, maxValue]`. + /// + /// - Parameters: + /// - minValue: The minimum allowed value. Will clip `self` if smaller. + /// - maxValue: The maximum allowed value. Will clip `self` if greater. + /// - Returns: `self` if `self` is within the interval `[minValue, maxValue]`, `minValue` or `maxValue` otherwise. + public func clipped(minValue: T, maxValue: T) -> T where T : Comparable + { + return min(maxValue, max(minValue, self as! T)) + } + + /// Clips to the closed interval `[minValue, maxValue]`. + /// + /// - Parameters: + /// - minValue: The minimum allowed value. Will clip `self` if smaller. + /// - maxValue: The maximum allowed value. Will clip `self` if greater. + public mutating func clip(minValue: T, maxValue: T) where T : Comparable + { + self = min(maxValue, max(minValue, self as! T)) as! Self + } +}