DynamicGradient.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * DynamicColor
  3. *
  4. * Copyright 2015-present Yannick Loriot.
  5. * http://yannickloriot.com
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. */
  26. #if os(iOS) || os(tvOS) || os(watchOS)
  27. import UIKit
  28. #elseif os(OSX)
  29. import AppKit
  30. #endif
  31. /**
  32. Object representing a gradient object. It allows you to manipulate colors inside different gradients and color spaces.
  33. */
  34. final public class DynamicGradient {
  35. let colors: [DynamicColor]
  36. /**
  37. Initializes and creates a gradient from a color array.
  38. - Parameter colors: An array of colors.
  39. */
  40. public init(colors: [DynamicColor]) {
  41. self.colors = colors
  42. }
  43. /**
  44. Returns the color palette of `amount` elements by grabbing equidistant colors.
  45. - Parameter amount: An amount of colors to return. 2 by default.
  46. - Parameter colorspace: The color space used to mix the colors. By default it uses the RBG color space.
  47. - Returns: An array of DynamicColor objects with equi-distant space in the gradient.
  48. */
  49. public func colorPalette(amount: UInt = 2, inColorSpace colorspace: DynamicColorSpace = .rgb) -> [DynamicColor] {
  50. guard amount > 0 && colors.count > 0 else {
  51. return []
  52. }
  53. guard colors.count > 1 else {
  54. return (0 ..< amount).map { _ in colors[0] }
  55. }
  56. let increment = 1.0 / CGFloat(amount - 1)
  57. return (0 ..< amount).map { pickColorAt(scale: CGFloat($0) * increment, inColorSpace: colorspace) }
  58. }
  59. /**
  60. Picks up and returns the color at the given scale by interpolating the colors.
  61. For example, given this color array `[red, green, blue]` and a scale of `0.25` you will get a kaki color.
  62. - Parameter scale: A float value between 0.0 and 1.0.
  63. - Parameter colorspace: The color space used to mix the colors. By default it uses the RBG color space.
  64. - Returns: A DynamicColor object corresponding to the color at the given scale.
  65. */
  66. public func pickColorAt(scale: CGFloat, inColorSpace colorspace: DynamicColorSpace = .rgb) -> DynamicColor {
  67. guard colors.count > 1 else {
  68. return colors.first ?? .black
  69. }
  70. let clippedScale = clip(scale, 0.0, 1.0)
  71. let positions = (0 ..< colors.count).map { CGFloat($0) / CGFloat(colors.count - 1) }
  72. var color: DynamicColor = .black
  73. for (index, position) in positions.enumerated() {
  74. guard clippedScale <= position else { continue }
  75. guard clippedScale != 0.0 && clippedScale != 1.0 else {
  76. return colors[index]
  77. }
  78. let previousPosition = positions[index - 1]
  79. let weight = (clippedScale - previousPosition) / (position - previousPosition)
  80. color = colors[index - 1].mixed(withColor: colors[index], weight: weight, inColorSpace: colorspace)
  81. break
  82. }
  83. return color
  84. }
  85. }