|
| 1 | +// |
| 2 | +// GradientColor.swift |
| 3 | +// FXViewKit |
| 4 | +// |
| 5 | +// Created by mac on 2021/7/2. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +public struct GradientColor: CaseIterable { |
| 11 | + public let colors: [UIColor] |
| 12 | +} |
| 13 | + |
| 14 | +public extension GradientColor { |
| 15 | + // pink |
| 16 | + static let JShine = GradientColor(colors: ["#12c2e9", "#c471ed", "#f64f59"].map({ UIColor(hexString: $0)! })) |
| 17 | + static let AzurePop = GradientColor(colors: ["#ef32d9", "#89fffd"].map({ UIColor(hexString: $0)! })) |
| 18 | + static let Dania = GradientColor(colors: ["#BE93C5", "#7BC6CC"].map({ UIColor(hexString: $0)! })) |
| 19 | + static let Timber = GradientColor(colors: ["#fc00ff", "#00dbde"].map({ UIColor(hexString: $0)! })) |
| 20 | + static let Atlas = GradientColor(colors: ["#FEAC5E", "#C779D0", "#4BC0C8"].map({ UIColor(hexString: $0)! })) |
| 21 | + static let BlackRosé = GradientColor(colors: ["#f4c4f3", "#fc67fa"].map({ UIColor(hexString: $0)! })) |
| 22 | + |
| 23 | + // other |
| 24 | + static let CoolBlues = GradientColor(colors: ["#2193b0", "#6dd5ed"].map({ UIColor(hexString: $0)! })) |
| 25 | + static let Memariani = GradientColor(colors: ["#aa4b6b", "#6b6b83", "#3b8d99"].map({ UIColor(hexString: $0)! })) |
| 26 | + static let AzurLane = GradientColor(colors: ["#7F7FD5", "#86A8E7", "#3b8d99"].map({ UIColor(hexString: $0)! })) |
| 27 | + static let KyeMeh = GradientColor(colors: ["#8360c3", "#2ebf91"].map({ UIColor(hexString: $0)! })) |
| 28 | + static let ByDesign = GradientColor(colors: ["#009FFF", "#ec2F4B"].map({ UIColor(hexString: $0)! })) |
| 29 | + static let EveningNight = GradientColor(colors: ["#005AA7", "#FFFDE4"].map({ UIColor(hexString: $0)! })) |
| 30 | + static let RedSunset = GradientColor(colors: ["#355C7D", "#6C5B7B", "#C06C84"].map({ UIColor(hexString: $0)! })) |
| 31 | + static let WeddingDayBlues = GradientColor(colors: ["#40E0D0", "#FF8C00", "#C06C84"].map({ UIColor(hexString: $0)! })) |
| 32 | + static let Quepal = GradientColor(colors: ["#11998e", "#38ef7d"].map({ UIColor(hexString: $0)! })) |
| 33 | + static let PunYeta = GradientColor(colors: ["#108dc7", "#ef8e38"].map({ UIColor(hexString: $0)! })) |
| 34 | + static let Ohhappiness = GradientColor(colors: ["#00b09b", "#96c93d"].map({ UIColor(hexString: $0)! })) |
| 35 | + static let DigitalWater = GradientColor(colors: ["#74ebd5", "#ACB6E5"].map({ UIColor(hexString: $0)! })) |
| 36 | + static let Subu = GradientColor(colors: ["#0cebeb", "#20e3b2", "#29ffc6"].map({ UIColor(hexString: $0)! })) |
| 37 | + static let Telegram = GradientColor(colors: ["#1c92d2", "#f2fcfe"].map({ UIColor(hexString: $0)! })) |
| 38 | + static let Transfile = GradientColor(colors: ["#16BFFD", "#CB3066"].map({ UIColor(hexString: $0)! })) |
| 39 | + static let RoseWater = GradientColor(colors: ["#E55D87", "#5FC3E4"].map({ UIColor(hexString: $0)! })) |
| 40 | + static let Sky = GradientColor(colors: ["#076585", "#fff"].map({ UIColor(hexString: $0)! })) |
| 41 | + |
| 42 | + static var pinkCases: [GradientColor] = [ |
| 43 | + JShine, |
| 44 | + AzurePop, |
| 45 | + Dania, |
| 46 | + Timber, |
| 47 | + Atlas, |
| 48 | + BlackRosé, |
| 49 | + ] |
| 50 | + |
| 51 | + static var allCases: [GradientColor] = [ |
| 52 | + JShine, |
| 53 | + CoolBlues, |
| 54 | + Memariani, |
| 55 | + AzurLane, |
| 56 | + KyeMeh, |
| 57 | + ByDesign, |
| 58 | + EveningNight, |
| 59 | + RedSunset, |
| 60 | + WeddingDayBlues, |
| 61 | + Quepal, |
| 62 | + PunYeta, |
| 63 | + Ohhappiness, |
| 64 | + DigitalWater, |
| 65 | + Subu, |
| 66 | + Telegram, |
| 67 | + AzurePop, |
| 68 | + Dania, |
| 69 | + Transfile, |
| 70 | + Timber, |
| 71 | + Atlas, |
| 72 | + RoseWater, |
| 73 | + Sky, |
| 74 | + BlackRosé, |
| 75 | + ] |
| 76 | +} |
| 77 | + |
| 78 | +public extension UIView { |
| 79 | + enum Direction { |
| 80 | + case topLeft |
| 81 | + case left |
| 82 | + case bottomLeft |
| 83 | + case top |
| 84 | + case topRight |
| 85 | + case right |
| 86 | + case bottomRight |
| 87 | + |
| 88 | + var point: CGPoint { |
| 89 | + switch self { |
| 90 | + case .topLeft: |
| 91 | + return CGPoint(x: 0, y: 0) |
| 92 | + case .left: |
| 93 | + return CGPoint(x: 0, y: 0.5) |
| 94 | + case .bottomLeft: |
| 95 | + return CGPoint(x: 0, y: 1) |
| 96 | + case .top: |
| 97 | + return CGPoint(x: 0.5, y: 0) |
| 98 | + case .topRight: |
| 99 | + return CGPoint(x: 1, y: 0) |
| 100 | + case .right: |
| 101 | + return CGPoint(x: 1, y: 0.5) |
| 102 | + case .bottomRight: |
| 103 | + return CGPoint(x: 1, y: 1) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + func configGradientColor(_ color: GradientColor?, directions: (start: Direction, end: Direction) = (.bottomLeft, .topRight)) { |
| 109 | + if let layer = layer as? CAGradientLayer, let color = color { |
| 110 | + layer.colors = color.colors.map(\.cgColor) |
| 111 | + layer.startPoint = directions.start.point |
| 112 | + layer.endPoint = directions.end.point |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +private extension UIColor { |
| 118 | + /// SwifterSwift: Create Color from hexadecimal string with optional transparency (if applicable). |
| 119 | + /// |
| 120 | + /// - Parameters: |
| 121 | + /// - hexString: hexadecimal string (examples: EDE7F6, 0xEDE7F6, #EDE7F6, #0ff, 0xF0F, ..). |
| 122 | + /// - transparency: optional transparency value (default is 1). |
| 123 | + convenience init?(hexString: String, transparency: CGFloat = 1) { |
| 124 | + var string = "" |
| 125 | + if hexString.lowercased().hasPrefix("0x") { |
| 126 | + string = hexString.replacingOccurrences(of: "0x", with: "") |
| 127 | + } else if hexString.hasPrefix("#") { |
| 128 | + string = hexString.replacingOccurrences(of: "#", with: "") |
| 129 | + } else { |
| 130 | + string = hexString |
| 131 | + } |
| 132 | + |
| 133 | + if string.count == 3 { // convert hex to 6 digit format if in short format |
| 134 | + var str = "" |
| 135 | + string.forEach { str.append(String(repeating: String($0), count: 2)) } |
| 136 | + string = str |
| 137 | + } |
| 138 | + |
| 139 | + guard let hexValue = Int(string, radix: 16) else { return nil } |
| 140 | + |
| 141 | + var trans = transparency |
| 142 | + if trans < 0 { trans = 0 } |
| 143 | + if trans > 1 { trans = 1 } |
| 144 | + |
| 145 | + let red = (hexValue >> 16) & 0xff |
| 146 | + let green = (hexValue >> 8) & 0xff |
| 147 | + let blue = hexValue & 0xff |
| 148 | + self.init(red: red, green: green, blue: blue, transparency: trans) |
| 149 | + } |
| 150 | + /// SwifterSwift: Create Color from RGB values with optional transparency. |
| 151 | + /// |
| 152 | + /// - Parameters: |
| 153 | + /// - red: red component. |
| 154 | + /// - green: green component. |
| 155 | + /// - blue: blue component. |
| 156 | + /// - transparency: optional transparency value (default is 1). |
| 157 | + convenience init?(red: Int, green: Int, blue: Int, transparency: CGFloat = 1) { |
| 158 | + guard red >= 0 && red <= 255 else { return nil } |
| 159 | + guard green >= 0 && green <= 255 else { return nil } |
| 160 | + guard blue >= 0 && blue <= 255 else { return nil } |
| 161 | + |
| 162 | + var trans = transparency |
| 163 | + if trans < 0 { trans = 0 } |
| 164 | + if trans > 1 { trans = 1 } |
| 165 | + |
| 166 | + self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: trans) |
| 167 | + } |
| 168 | +} |
0 commit comments