ImageProcessor.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. //
  2. // ImageProcessor.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 2016/08/26.
  6. //
  7. // Copyright (c) 2017 Wei Wang <onevcat@gmail.com>
  8. //
  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:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  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
  25. // THE SOFTWARE.
  26. import Foundation
  27. import CoreGraphics
  28. /// The item which could be processed by an `ImageProcessor`
  29. ///
  30. /// - image: Input image
  31. /// - data: Input data
  32. public enum ImageProcessItem {
  33. case image(Image)
  34. case data(Data)
  35. }
  36. /// An `ImageProcessor` would be used to convert some downloaded data to an image.
  37. public protocol ImageProcessor {
  38. /// Identifier of the processor. It will be used to identify the processor when
  39. /// caching and retrieving an image. You might want to make sure that processors with
  40. /// same properties/functionality have the same identifiers, so correct processed images
  41. /// could be retrived with proper key.
  42. ///
  43. /// - Note: Do not supply an empty string for a customized processor, which is already taken by
  44. /// the `DefaultImageProcessor`. It is recommended to use a reverse domain name notation
  45. /// string of your own for the identifier.
  46. var identifier: String { get }
  47. /// Process an input `ImageProcessItem` item to an image for this processor.
  48. ///
  49. /// - parameter item: Input item which will be processed by `self`
  50. /// - parameter options: Options when processing the item.
  51. ///
  52. /// - returns: The processed image.
  53. ///
  54. /// - Note: The return value will be `nil` if processing failed while converting data to image.
  55. /// If input item is already an image and there is any errors in processing, the input
  56. /// image itself will be returned.
  57. /// - Note: Most processor only supports CG-based images.
  58. /// watchOS is not supported for processers containing filter, the input image will be returned directly on watchOS.
  59. func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image?
  60. }
  61. typealias ProcessorImp = ((ImageProcessItem, KingfisherOptionsInfo) -> Image?)
  62. public extension ImageProcessor {
  63. /// Append an `ImageProcessor` to another. The identifier of the new `ImageProcessor`
  64. /// will be "\(self.identifier)|>\(another.identifier)".
  65. ///
  66. /// - parameter another: An `ImageProcessor` you want to append to `self`.
  67. ///
  68. /// - returns: The new `ImageProcessor` will process the image in the order
  69. /// of the two processors concatenated.
  70. public func append(another: ImageProcessor) -> ImageProcessor {
  71. let newIdentifier = identifier.appending("|>\(another.identifier)")
  72. return GeneralProcessor(identifier: newIdentifier) {
  73. item, options in
  74. if let image = self.process(item: item, options: options) {
  75. return another.process(item: .image(image), options: options)
  76. } else {
  77. return nil
  78. }
  79. }
  80. }
  81. }
  82. func ==(left: ImageProcessor, right: ImageProcessor) -> Bool {
  83. return left.identifier == right.identifier
  84. }
  85. func !=(left: ImageProcessor, right: ImageProcessor) -> Bool {
  86. return !(left == right)
  87. }
  88. fileprivate struct GeneralProcessor: ImageProcessor {
  89. let identifier: String
  90. let p: ProcessorImp
  91. func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  92. return p(item, options)
  93. }
  94. }
  95. /// The default processor. It convert the input data to a valid image.
  96. /// Images of .PNG, .JPEG and .GIF format are supported.
  97. /// If an image is given, `DefaultImageProcessor` will do nothing on it and just return that image.
  98. public struct DefaultImageProcessor: ImageProcessor {
  99. /// A default `DefaultImageProcessor` could be used across.
  100. public static let `default` = DefaultImageProcessor()
  101. /// Identifier of the processor.
  102. /// - Note: See documentation of `ImageProcessor` protocol for more.
  103. public let identifier = ""
  104. /// Initialize a `DefaultImageProcessor`
  105. public init() {}
  106. /// Process an input `ImageProcessItem` item to an image for this processor.
  107. ///
  108. /// - parameter item: Input item which will be processed by `self`
  109. /// - parameter options: Options when processing the item.
  110. ///
  111. /// - returns: The processed image.
  112. ///
  113. /// - Note: See documentation of `ImageProcessor` protocol for more.
  114. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  115. switch item {
  116. case .image(let image):
  117. return image.kf.scaled(to: options.scaleFactor)
  118. case .data(let data):
  119. return Kingfisher<Image>.image(
  120. data: data,
  121. scale: options.scaleFactor,
  122. preloadAllAnimationData: options.preloadAllAnimationData,
  123. onlyFirstFrame: options.onlyLoadFirstFrame)
  124. }
  125. }
  126. }
  127. public struct RectCorner: OptionSet {
  128. public let rawValue: Int
  129. public static let topLeft = RectCorner(rawValue: 1 << 0)
  130. public static let topRight = RectCorner(rawValue: 1 << 1)
  131. public static let bottomLeft = RectCorner(rawValue: 1 << 2)
  132. public static let bottomRight = RectCorner(rawValue: 1 << 3)
  133. public static let all: RectCorner = [.topLeft, .topRight, .bottomLeft, .bottomRight]
  134. public init(rawValue: Int) {
  135. self.rawValue = rawValue
  136. }
  137. var cornerIdentifier: String {
  138. if self == .all {
  139. return ""
  140. }
  141. return "_corner(\(rawValue))"
  142. }
  143. }
  144. /// Processor for making round corner images. Only CG-based images are supported in macOS,
  145. /// if a non-CG image passed in, the processor will do nothing.
  146. public struct RoundCornerImageProcessor: ImageProcessor {
  147. /// Identifier of the processor.
  148. /// - Note: See documentation of `ImageProcessor` protocol for more.
  149. public let identifier: String
  150. /// Corner radius will be applied in processing.
  151. public let cornerRadius: CGFloat
  152. /// The target corners which will be applied rounding.
  153. public let roundingCorners: RectCorner
  154. /// Target size of output image should be. If `nil`, the image will keep its original size after processing.
  155. public let targetSize: CGSize?
  156. /// Background color of the output image. If `nil`, it will stay transparent.
  157. public let backgroundColor: Color?
  158. /// Initialize a `RoundCornerImageProcessor`
  159. ///
  160. /// - parameter cornerRadius: Corner radius will be applied in processing.
  161. /// - parameter targetSize: Target size of output image should be. If `nil`,
  162. /// the image will keep its original size after processing.
  163. /// Default is `nil`.
  164. /// - parameter corners: The target corners which will be applied rounding. Default is `.all`.
  165. /// - parameter backgroundColor: Backgroud color to apply for the output image. Default is `nil`.
  166. public init(cornerRadius: CGFloat, targetSize: CGSize? = nil, roundingCorners corners: RectCorner = .all, backgroundColor: Color? = nil) {
  167. self.cornerRadius = cornerRadius
  168. self.targetSize = targetSize
  169. self.roundingCorners = corners
  170. self.backgroundColor = backgroundColor
  171. self.identifier = {
  172. var identifier = ""
  173. if let size = targetSize {
  174. identifier = "com.onevcat.Kingfisher.RoundCornerImageProcessor(\(cornerRadius)_\(size)\(corners.cornerIdentifier))"
  175. } else {
  176. identifier = "com.onevcat.Kingfisher.RoundCornerImageProcessor(\(cornerRadius)\(corners.cornerIdentifier))"
  177. }
  178. if let backgroundColor = backgroundColor {
  179. identifier += "_\(backgroundColor)"
  180. }
  181. return identifier
  182. }()
  183. }
  184. /// Process an input `ImageProcessItem` item to an image for this processor.
  185. ///
  186. /// - parameter item: Input item which will be processed by `self`
  187. /// - parameter options: Options when processing the item.
  188. ///
  189. /// - returns: The processed image.
  190. ///
  191. /// - Note: See documentation of `ImageProcessor` protocol for more.
  192. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  193. switch item {
  194. case .image(let image):
  195. let size = targetSize ?? image.kf.size
  196. return image.kf.scaled(to: options.scaleFactor)
  197. .kf.image(withRoundRadius: cornerRadius, fit: size, roundingCorners: roundingCorners, backgroundColor: backgroundColor)
  198. case .data(_):
  199. return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  200. }
  201. }
  202. }
  203. /// Specify how a size adjusts itself to fit a target size.
  204. ///
  205. /// - none: Not scale the content.
  206. /// - aspectFit: Scale the content to fit the size of the view by maintaining the aspect ratio.
  207. /// - aspectFill: Scale the content to fill the size of the view
  208. public enum ContentMode {
  209. case none
  210. case aspectFit
  211. case aspectFill
  212. }
  213. /// Processor for resizing images. Only CG-based images are supported in macOS.
  214. public struct ResizingImageProcessor: ImageProcessor {
  215. /// Identifier of the processor.
  216. /// - Note: See documentation of `ImageProcessor` protocol for more.
  217. public let identifier: String
  218. /// The reference size for resizing operation.
  219. public let referenceSize: CGSize
  220. /// Target content mode of output image should be.
  221. /// Default to ContentMode.none
  222. public let targetContentMode: ContentMode
  223. /// Initialize a `ResizingImageProcessor`.
  224. ///
  225. /// - Parameters:
  226. /// - referenceSize: The reference size for resizing operation.
  227. /// - mode: Target content mode of output image should be.
  228. ///
  229. /// - Note:
  230. /// The instance of `ResizingImageProcessor` will follow its `mode` property
  231. /// and try to resizing the input images to fit or fill the `referenceSize`.
  232. /// That means if you are using a `mode` besides of `.none`, you may get an
  233. /// image with its size not be the same as the `referenceSize`.
  234. ///
  235. /// **Example**: With input image size: {100, 200},
  236. /// `referenceSize`: {100, 100}, `mode`: `.aspectFit`,
  237. /// you will get an output image with size of {50, 100}, which "fit"s
  238. /// the `referenceSize`.
  239. ///
  240. /// If you need an output image exactly to be a specified size, append or use
  241. /// a `CroppingImageProcessor`.
  242. public init(referenceSize: CGSize, mode: ContentMode = .none) {
  243. self.referenceSize = referenceSize
  244. self.targetContentMode = mode
  245. if mode == .none {
  246. self.identifier = "com.onevcat.Kingfisher.ResizingImageProcessor(\(referenceSize))"
  247. } else {
  248. self.identifier = "com.onevcat.Kingfisher.ResizingImageProcessor(\(referenceSize), \(mode))"
  249. }
  250. }
  251. /// Process an input `ImageProcessItem` item to an image for this processor.
  252. ///
  253. /// - parameter item: Input item which will be processed by `self`
  254. /// - parameter options: Options when processing the item.
  255. ///
  256. /// - returns: The processed image.
  257. ///
  258. /// - Note: See documentation of `ImageProcessor` protocol for more.
  259. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  260. switch item {
  261. case .image(let image):
  262. return image.kf.scaled(to: options.scaleFactor)
  263. .kf.resize(to: referenceSize, for: targetContentMode)
  264. case .data(_):
  265. return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  266. }
  267. }
  268. }
  269. /// Processor for adding blur effect to images. `Accelerate.framework` is used underhood for
  270. /// a better performance. A simulated Gaussian blur with specified blur radius will be applied.
  271. public struct BlurImageProcessor: ImageProcessor {
  272. /// Identifier of the processor.
  273. /// - Note: See documentation of `ImageProcessor` protocol for more.
  274. public let identifier: String
  275. /// Blur radius for the simulated Gaussian blur.
  276. public let blurRadius: CGFloat
  277. /// Initialize a `BlurImageProcessor`
  278. ///
  279. /// - parameter blurRadius: Blur radius for the simulated Gaussian blur.
  280. public init(blurRadius: CGFloat) {
  281. self.blurRadius = blurRadius
  282. self.identifier = "com.onevcat.Kingfisher.BlurImageProcessor(\(blurRadius))"
  283. }
  284. /// Process an input `ImageProcessItem` item to an image for this processor.
  285. ///
  286. /// - parameter item: Input item which will be processed by `self`
  287. /// - parameter options: Options when processing the item.
  288. ///
  289. /// - returns: The processed image.
  290. ///
  291. /// - Note: See documentation of `ImageProcessor` protocol for more.
  292. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  293. switch item {
  294. case .image(let image):
  295. let radius = blurRadius * options.scaleFactor
  296. return image.kf.scaled(to: options.scaleFactor)
  297. .kf.blurred(withRadius: radius)
  298. case .data(_):
  299. return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  300. }
  301. }
  302. }
  303. /// Processor for adding an overlay to images. Only CG-based images are supported in macOS.
  304. public struct OverlayImageProcessor: ImageProcessor {
  305. /// Identifier of the processor.
  306. /// - Note: See documentation of `ImageProcessor` protocol for more.
  307. public let identifier: String
  308. /// Overlay color will be used to overlay the input image.
  309. public let overlay: Color
  310. /// Fraction will be used when overlay the color to image.
  311. public let fraction: CGFloat
  312. /// Initialize an `OverlayImageProcessor`
  313. ///
  314. /// - parameter overlay: Overlay color will be used to overlay the input image.
  315. /// - parameter fraction: Fraction will be used when overlay the color to image.
  316. /// From 0.0 to 1.0. 0.0 means solid color, 1.0 means transparent overlay.
  317. public init(overlay: Color, fraction: CGFloat = 0.5) {
  318. self.overlay = overlay
  319. self.fraction = fraction
  320. self.identifier = "com.onevcat.Kingfisher.OverlayImageProcessor(\(overlay.hex)_\(fraction))"
  321. }
  322. /// Process an input `ImageProcessItem` item to an image for this processor.
  323. ///
  324. /// - parameter item: Input item which will be processed by `self`
  325. /// - parameter options: Options when processing the item.
  326. ///
  327. /// - returns: The processed image.
  328. ///
  329. /// - Note: See documentation of `ImageProcessor` protocol for more.
  330. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  331. switch item {
  332. case .image(let image):
  333. return image.kf.scaled(to: options.scaleFactor)
  334. .kf.overlaying(with: overlay, fraction: fraction)
  335. case .data(_):
  336. return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  337. }
  338. }
  339. }
  340. /// Processor for tint images with color. Only CG-based images are supported.
  341. public struct TintImageProcessor: ImageProcessor {
  342. /// Identifier of the processor.
  343. /// - Note: See documentation of `ImageProcessor` protocol for more.
  344. public let identifier: String
  345. /// Tint color will be used to tint the input image.
  346. public let tint: Color
  347. /// Initialize a `TintImageProcessor`
  348. ///
  349. /// - parameter tint: Tint color will be used to tint the input image.
  350. public init(tint: Color) {
  351. self.tint = tint
  352. self.identifier = "com.onevcat.Kingfisher.TintImageProcessor(\(tint.hex))"
  353. }
  354. /// Process an input `ImageProcessItem` item to an image for this processor.
  355. ///
  356. /// - parameter item: Input item which will be processed by `self`
  357. /// - parameter options: Options when processing the item.
  358. ///
  359. /// - returns: The processed image.
  360. ///
  361. /// - Note: See documentation of `ImageProcessor` protocol for more.
  362. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  363. switch item {
  364. case .image(let image):
  365. return image.kf.scaled(to: options.scaleFactor)
  366. .kf.tinted(with: tint)
  367. case .data(_):
  368. return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  369. }
  370. }
  371. }
  372. /// Processor for applying some color control to images. Only CG-based images are supported.
  373. /// watchOS is not supported.
  374. public struct ColorControlsProcessor: ImageProcessor {
  375. /// Identifier of the processor.
  376. /// - Note: See documentation of `ImageProcessor` protocol for more.
  377. public let identifier: String
  378. /// Brightness changing to image.
  379. public let brightness: CGFloat
  380. /// Contrast changing to image.
  381. public let contrast: CGFloat
  382. /// Saturation changing to image.
  383. public let saturation: CGFloat
  384. /// InputEV changing to image.
  385. public let inputEV: CGFloat
  386. /// Initialize a `ColorControlsProcessor`
  387. ///
  388. /// - parameter brightness: Brightness changing to image.
  389. /// - parameter contrast: Contrast changing to image.
  390. /// - parameter saturation: Saturation changing to image.
  391. /// - parameter inputEV: InputEV changing to image.
  392. public init(brightness: CGFloat, contrast: CGFloat, saturation: CGFloat, inputEV: CGFloat) {
  393. self.brightness = brightness
  394. self.contrast = contrast
  395. self.saturation = saturation
  396. self.inputEV = inputEV
  397. self.identifier = "com.onevcat.Kingfisher.ColorControlsProcessor(\(brightness)_\(contrast)_\(saturation)_\(inputEV))"
  398. }
  399. /// Process an input `ImageProcessItem` item to an image for this processor.
  400. ///
  401. /// - parameter item: Input item which will be processed by `self`
  402. /// - parameter options: Options when processing the item.
  403. ///
  404. /// - returns: The processed image.
  405. ///
  406. /// - Note: See documentation of `ImageProcessor` protocol for more.
  407. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  408. switch item {
  409. case .image(let image):
  410. return image.kf.scaled(to: options.scaleFactor)
  411. .kf.adjusted(brightness: brightness, contrast: contrast, saturation: saturation, inputEV: inputEV)
  412. case .data(_):
  413. return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  414. }
  415. }
  416. }
  417. /// Processor for applying black and white effect to images. Only CG-based images are supported.
  418. /// watchOS is not supported.
  419. public struct BlackWhiteProcessor: ImageProcessor {
  420. /// Identifier of the processor.
  421. /// - Note: See documentation of `ImageProcessor` protocol for more.
  422. public let identifier = "com.onevcat.Kingfisher.BlackWhiteProcessor"
  423. /// Initialize a `BlackWhiteProcessor`
  424. public init() {}
  425. /// Process an input `ImageProcessItem` item to an image for this processor.
  426. ///
  427. /// - parameter item: Input item which will be processed by `self`
  428. /// - parameter options: Options when processing the item.
  429. ///
  430. /// - returns: The processed image.
  431. ///
  432. /// - Note: See documentation of `ImageProcessor` protocol for more.
  433. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  434. return ColorControlsProcessor(brightness: 0.0, contrast: 1.0, saturation: 0.0, inputEV: 0.7)
  435. .process(item: item, options: options)
  436. }
  437. }
  438. /// Processor for cropping an image. Only CG-based images are supported.
  439. /// watchOS is not supported.
  440. public struct CroppingImageProcessor: ImageProcessor {
  441. /// Identifier of the processor.
  442. /// - Note: See documentation of `ImageProcessor` protocol for more.
  443. public let identifier: String
  444. /// Target size of output image should be.
  445. public let size: CGSize
  446. /// Anchor point from which the output size should be calculate.
  447. /// The anchor point is consisted by two values between 0.0 and 1.0.
  448. /// It indicates a related point in current image.
  449. /// See `CroppingImageProcessor.init(size:anchor:)` for more.
  450. public let anchor: CGPoint
  451. /// Initialize a `CroppingImageProcessor`
  452. ///
  453. /// - Parameters:
  454. /// - size: Target size of output image should be.
  455. /// - anchor: The anchor point from which the size should be calculated.
  456. /// Default is `CGPoint(x: 0.5, y: 0.5)`, which means the center of input image.
  457. /// - Note:
  458. /// The anchor point is consisted by two values between 0.0 and 1.0.
  459. /// It indicates a related point in current image, eg: (0.0, 0.0) for top-left
  460. /// corner, (0.5, 0.5) for center and (1.0, 1.0) for bottom-right corner.
  461. /// The `size` property of `CroppingImageProcessor` will be used along with
  462. /// `anchor` to calculate a target rectange in the size of image.
  463. ///
  464. /// The target size will be automatically calculated with a reasonable behavior.
  465. /// For example, when you have an image size of `CGSize(width: 100, height: 100)`,
  466. /// and a target size of `CGSize(width: 20, height: 20)`:
  467. /// - with a (0.0, 0.0) anchor (top-left), the crop rect will be `{0, 0, 20, 20}`;
  468. /// - with a (0.5, 0.5) anchor (center), it will be `{40, 40, 20, 20}`
  469. /// - while with a (1.0, 1.0) anchor (bottom-right), it will be `{80, 80, 20, 20}`
  470. public init(size: CGSize, anchor: CGPoint = CGPoint(x: 0.5, y: 0.5)) {
  471. self.size = size
  472. self.anchor = anchor
  473. self.identifier = "com.onevcat.Kingfisher.CroppingImageProcessor(\(size)_\(anchor))"
  474. }
  475. /// Process an input `ImageProcessItem` item to an image for this processor.
  476. ///
  477. /// - parameter item: Input item which will be processed by `self`
  478. /// - parameter options: Options when processing the item.
  479. ///
  480. /// - returns: The processed image.
  481. ///
  482. /// - Note: See documentation of `ImageProcessor` protocol for more.
  483. public func process(item: ImageProcessItem, options: KingfisherOptionsInfo) -> Image? {
  484. switch item {
  485. case .image(let image):
  486. return image.kf.scaled(to: options.scaleFactor)
  487. .kf.crop(to: size, anchorOn: anchor)
  488. case .data(_): return (DefaultImageProcessor.default >> self).process(item: item, options: options)
  489. }
  490. }
  491. }
  492. /// Concatenate two `ImageProcessor`s. `ImageProcessor.appen(another:)` is used internally.
  493. ///
  494. /// - parameter left: First processor.
  495. /// - parameter right: Second processor.
  496. ///
  497. /// - returns: The concatenated processor.
  498. public func >>(left: ImageProcessor, right: ImageProcessor) -> ImageProcessor {
  499. return left.append(another: right)
  500. }
  501. fileprivate extension Color {
  502. var hex: String {
  503. var r: CGFloat = 0
  504. var g: CGFloat = 0
  505. var b: CGFloat = 0
  506. var a: CGFloat = 0
  507. getRed(&r, green: &g, blue: &b, alpha: &a)
  508. let rInt = Int(r * 255) << 24
  509. let gInt = Int(g * 255) << 16
  510. let bInt = Int(b * 255) << 8
  511. let aInt = Int(a * 255)
  512. let rgba = rInt | gInt | bInt | aInt
  513. return String(format:"#%08x", rgba)
  514. }
  515. }