5 // Created by Ethan Gill on 2017/11/28.
7 // Copyright (c) 2018 Ethan Gill <ethan.gill@me.com>
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 /// An `ImageModifier` can be used to change properties on an Image in between
30 /// cache serialization and use of the image.
31 public protocol ImageModifier {
32 /// Modify an input `Image`.
34 /// - parameter image: Image which will be modified by `self`
36 /// - returns: The modified image.
38 /// - Note: The return value will be unmodified if modifying is not possible on
39 /// the current platform.
40 /// - Note: Most modifiers support UIImage or NSImage, but not CGImage.
41 func modify(_ image: Image) -> Image
44 extension ImageModifier {
45 func modify(_ image: Image?) -> Image? {
46 guard let image = image else {
53 typealias ModifierImp = ((Image) -> Image)
55 fileprivate struct GeneralModifier: ImageModifier {
56 let identifier: String
58 func modify(_ image: Image) -> Image {
63 /// The default modifier.
64 /// Does nothing and returns the image it was given
65 public struct DefaultImageModifier: ImageModifier {
67 /// A default `DefaultImageModifier` which can be used everywhere.
68 public static let `default` = DefaultImageModifier()
70 /// Initialize a `DefaultImageModifier`
73 /// Modify an input `Image`.
75 /// - parameter image: Image which will be modified by `self`
77 /// - returns: The modified image.
79 /// - Note: See documentation of `ImageModifier` protocol for more.
80 public func modify(_ image: Image) -> Image {
85 /// A custom modifier.
86 /// Can be initialized with a block to modify images in a custom way
87 public struct AnyImageModifier: ImageModifier {
89 /// A block which modifies images, or returns the original image
90 /// if modification cannot be performed.
91 let block: (Image) -> Image
93 /// Initialize an `AnyImageModifier`
94 public init(modify: @escaping (Image) -> Image) {
98 /// Modifies an input `Image` using this `AnyImageModifier`'s `block`.
100 /// - parameter image: Image which will be modified by `self`
102 /// - returns: The modified image.
104 /// - Note: See documentation of `ImageModifier` protocol for more.
105 public func modify(_ image: Image) -> Image {
110 #if os(iOS) || os(tvOS) || os(watchOS)
113 /// Modifier for setting the rendering mode of images.
114 /// Only UI-based images are supported; if a non-UI image is passed in, the
115 /// modifier will do nothing.
116 public struct RenderingModeImageModifier: ImageModifier {
118 /// The rendering mode to apply to the image.
119 public let renderingMode: UIImageRenderingMode
121 /// Initialize a `RenderingModeImageModifier`
123 /// - parameter renderingMode: The rendering mode to apply to the image.
124 /// Default is .automatic
125 public init(renderingMode: UIImageRenderingMode = .automatic) {
126 self.renderingMode = renderingMode
129 /// Modify an input `Image`.
131 /// - parameter image: Image which will be modified by `self`
133 /// - returns: The modified image.
135 /// - Note: See documentation of `ImageModifier` protocol for more.
136 public func modify(_ image: Image) -> Image {
137 return image.withRenderingMode(renderingMode)
141 /// Modifier for setting the `flipsForRightToLeftLayoutDirection` property of images.
142 /// Only UI-based images are supported; if a non-UI image is passed in, the
143 /// modifier will do nothing.
144 public struct FlipsForRightToLeftLayoutDirectionImageModifier: ImageModifier {
145 /// Initialize a `FlipsForRightToLeftLayoutDirectionImageModifier`
147 /// - Note: On versions of iOS lower than 9.0, the image will be returned
151 /// Modify an input `Image`.
153 /// - parameter image: Image which will be modified by `self`
155 /// - returns: The modified image.
157 /// - Note: See documentation of `ImageModifier` protocol for more.
158 public func modify(_ image: Image) -> Image {
159 if #available(iOS 9.0, *) {
160 return image.imageFlippedForRightToLeftLayoutDirection()
167 /// Modifier for setting the `alignmentRectInsets` property of images.
168 /// Only UI-based images are supported; if a non-UI image is passed in, the
169 /// modifier will do nothing.
170 public struct AlignmentRectInsetsImageModifier: ImageModifier {
172 /// The alignment insets to apply to the image
173 public let alignmentInsets: UIEdgeInsets
175 /// Initialize a `AlignmentRectInsetsImageModifier`
176 public init(alignmentInsets: UIEdgeInsets) {
177 self.alignmentInsets = alignmentInsets
180 /// Modify an input `Image`.
182 /// - parameter image: Image which will be modified by `self`
184 /// - returns: The modified image.
186 /// - Note: See documentation of `ImageModifier` protocol for more.
187 public func modify(_ image: Image) -> Image {
188 return image.withAlignmentRectInsets(alignmentInsets)