├── .codecov.yml ├── .github └── workflows │ └── swift.yml ├── .gitignore ├── .swiftpm └── xcode │ ├── package.xcworkspace │ └── contents.xcworkspacedata │ └── xcshareddata │ └── xcschemes │ └── Rainbow.xcscheme ├── LICENSE ├── Package.swift ├── README.md ├── RainbowPod.podspec ├── Sources └── Rainbow │ ├── Color+Cocoa.swift │ ├── Color.swift │ ├── Colors.swift │ └── Utils │ ├── Double.swift │ └── NSRegularExpression.swift └── Tests ├── LinuxMain.swift └── RainbowTests ├── RainbowTests.swift └── XCTestManifests.swift /.codecov.yml: -------------------------------------------------------------------------------- 1 | ignore: 2 | - "Tests/" 3 | 4 | comment: 5 | layout: header, changes, diff -------------------------------------------------------------------------------- /.github/workflows/swift.yml: -------------------------------------------------------------------------------- 1 | name: Swift 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: macos-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: Build 17 | run: swift build -v 18 | - name: Run tests 19 | run: swift test -v 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | -------------------------------------------------------------------------------- /.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.swiftpm/xcode/xcshareddata/xcschemes/Rainbow.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 29 | 35 | 36 | 37 | 38 | 39 | 45 | 46 | 52 | 53 | 54 | 55 | 57 | 63 | 64 | 65 | 66 | 67 | 77 | 78 | 84 | 85 | 91 | 92 | 93 | 94 | 96 | 97 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Quentin Jin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.1 2 | 3 | import PackageDescription 4 | 5 | let package = Package( 6 | name: "Rainbow", 7 | products: [ 8 | .library(name: "Rainbow", targets: ["Rainbow"]), 9 | ], 10 | targets: [ 11 | .target(name: "Rainbow", dependencies: []), 12 | .testTarget(name: "RainbowTests", dependencies: ["Rainbow"]), 13 | ] 14 | ) 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rainbow 2 | 3 |

4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

13 |

Color conversion and manipulation library for Swift, with no rely on UIKit/AppKit.

14 | 15 | ## Highlights 16 | 17 | - Full featured 18 | - rgba 19 | - hsla 20 | - hsva 21 | - hex 22 | - ansi 23 | - ... 24 | - No rely on UIKit/AppKit 25 | - Expressive API 26 | - Complete Documentation 27 | - Comprehensive Test Coverage 28 | - from&to UIColor&NSColor 29 | - Material & CSS Colors 30 | 31 | ## API 32 | 33 | ### Creation 34 | 35 | ```swift 36 | let color = Color(red: 50, green: 100, blue: 150, alpha: 1) 37 | let color = Color(hue: 150, saturation: 50, lightness: 50, alpha: 1) 38 | let color = Color(hue: 300, saturation: 70, value: 70, alpha: 1) 39 | 40 | let color = Color(hex: "#aaa") // "#" is optional 41 | let color = Color(hex: "#ABCF") 42 | let color = Color(hex: "#010203") 43 | let color = Color(hex: "#01020304") 44 | 45 | let color = Color(hex: 0x0366D6) 46 | 47 | let color = Color(rgb: "rgb(0, 100, 200)") // the same as `Color(rgba:)" 48 | let color = Color(rgba: "rgba(0, 100, 200, 0.3)") 49 | 50 | let color = Color(hsv: "hsv(100, 20%, 30%)") // the same as `Color(hsva:)" 51 | let color = Color(hsva: "hsva(100, 20%, 30%, 0.3)") 52 | 53 | let color = Color(hsl: "hsl(100, 20%, 30%)") // the same as `Color(hsla:)" 54 | let color = Color(hsla: "hsla(100, 20%, 30%, 0.3)") 55 | 56 | let color = Color.rgb(100, 200, 300) 57 | let color = Color.hsl(100, 20, 30) 58 | let color = Color.hsv(100, 20, 30) 59 | let color = Color.hex("#0a0b0c") 60 | let color = Color.hex(0x0366D6) 61 | 62 | let color = Color.Material.red50 63 | let color = Color.Material.purple500 64 | let color = Color.CSS.navy 65 | let color = Color.CSS.tomato 66 | // ... 67 | ``` 68 | 69 | ### properties 70 | 71 | ```swift 72 | let color = Color(hex: "#010203") 73 | color.rgba // (red: Int, green: Int, blue: Int, alpha: Double) 74 | color.hsva // (hue: Int, saturation: Double, value: Double, alpha: Double) 75 | color.hsla // (hue: Int, saturation: Double, lightness: Double, alpha: Double) 76 | 77 | color.isDark // true or false 78 | color.isLight // true or false 79 | color.isWhite // true or false 80 | color.isBlack // true or false 81 | color.negate 82 | 83 | color.ansi16 84 | color.ansi256 85 | 86 | color.random // a random color 87 | ``` 88 | 89 | ## Install 90 | 91 | ```swift 92 | dependencies: [ 93 | .package(url: "https://github.com/luoxiu/Rainbow", from("0.1.0")) 94 | ] 95 | ``` 96 | 97 | ```ruby 98 | pod 'RainbowPod', '~> 0.1.0' 99 | ``` 100 | 101 | ## Acknowledge 102 | 103 | Inspired by the awesome javascript library [color](https://github.com/Qix-/color). 104 | 105 | ## Related 106 | 107 | - [Chalk](https://github.com/luoxiu/Chalk) - 🖍 Expressive styling on terminal string. 108 | 109 | ## Contribute 110 | 111 | If you find a bug, open an issue, if you want to add new features, feel free to submit a pull request. Any contributing is welcome at all times! 112 | 113 | -------------------------------------------------------------------------------- /RainbowPod.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = 'RainbowPod' 3 | s.version = '0.1.1' 4 | s.summary = 'Color conversion and manipulation library for Swift, with no rely on UIKit/AppKit.' 5 | s.homepage = 'https://github.com/luoxiu/Rainbow' 6 | 7 | s.license = { type: 'MIT', file: 'LICENSE' } 8 | 9 | s.author = { 'luoxiu' => 'luoxiustm@gmail.com' } 10 | 11 | s.ios.deployment_target = '10.0' 12 | s.osx.deployment_target = '10.12' 13 | s.tvos.deployment_target = '10.0' 14 | s.watchos.deployment_target = '3.0' 15 | 16 | s.module_name = 'Rainbow' 17 | 18 | s.swift_version = '5.0' 19 | 20 | s.source = { git: s.homepage + '.git', tag: s.version } 21 | s.source_files = 'Sources/Rainbow/**/*.swift' 22 | end 23 | -------------------------------------------------------------------------------- /Sources/Rainbow/Color+Cocoa.swift: -------------------------------------------------------------------------------- 1 | // 2 | // File.swift 3 | // 4 | // 5 | // Created by jinxiangqiu on 15/3/2020. 6 | // 7 | 8 | #if canImport(AppKit) 9 | import AppKit 10 | 11 | public extension Color { 12 | 13 | func toNSColor() -> NSColor { 14 | return NSColor(red: CGFloat(r), green: CGFloat(g), blue: CGFloat(b), alpha: CGFloat(a)) 15 | } 16 | 17 | static func fromNSColor(_ ns: NSColor) -> Color { 18 | return Color(r: Double(ns.redComponent), g: Double(ns.greenComponent), b: Double(ns.blueComponent), a: Double(ns.alphaComponent)) 19 | } 20 | } 21 | 22 | #endif 23 | 24 | #if canImport(UIKit) 25 | import UIKit 26 | 27 | public extension Color { 28 | 29 | func toUIColor() -> UIColor { 30 | return UIColor(red: CGFloat(r), green: CGFloat(g), blue: CGFloat(b), alpha: CGFloat(a)) 31 | } 32 | 33 | static func fromUIColor(_ ui: UIColor) -> Color { 34 | var r: CGFloat = 0 35 | var g: CGFloat = 0 36 | var b: CGFloat = 0 37 | var a: CGFloat = 0 38 | ui.getRed(&r, green: &g, blue: &b, alpha: &a) 39 | return Color(r: Double(r), g: Double(g), b: Double(b), a: Double(a)) 40 | } 41 | } 42 | #endif 43 | -------------------------------------------------------------------------------- /Sources/Rainbow/Color.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | public struct Color { 4 | 5 | let r: Double 6 | let g: Double 7 | let b: Double 8 | let a: Double 9 | 10 | init(r: Double, g: Double, b: Double, a: Double) { 11 | self.r = r 12 | self.g = g 13 | self.b = b 14 | self.a = a 15 | } 16 | 17 | /// Creates a color using the specified opacity and RGB component values. 18 | /// 19 | /// Color(red: 3, green: 102, blue: 214) 20 | /// Color(red: 3, green: 102, blue: 214, alpha: 0.5) 21 | /// 22 | /// `red`, `green` and `blue` will be clamped into 0...255; `alpha` will be 23 | /// clamped into 0...1. 24 | public init(red: Int, green: Int, blue: Int, alpha: Double = 1) { 25 | r = Double(red).clamp(min: 0, max: 255) / 255 26 | g = Double(green).clamp(min: 0, max: 255) / 255 27 | b = Double(blue).clamp(min: 0, max: 255) / 255 28 | a = alpha.clamp(min: 0, max: 1) 29 | } 30 | 31 | /// Creates a color using the specified opacity and HSV(HSB) component values. 32 | /// 33 | /// Color(hue: 300, saturation: 20, value: 10) 34 | /// Color(hue: 300, saturation: 20, value: 10, alpha: 0.5) 35 | /// 36 | /// `hue` will be clamped into 0...360; `saturation` and `value` will be 37 | /// clamped into 1...100, `alpha` will be clamped into 0...1. 38 | public init(hue: Int, saturation: Int, value: Int, alpha: Double) { 39 | let h = Double(hue).clamp(min: 0, max: 360) / 60 40 | let s = Double(saturation).clamp(min: 0, max: 100) / 100 41 | let v = Double(value).clamp(min: 0, max: 100) / 100 42 | let a = alpha.clamp(min: 0, max: 1) 43 | let hi = fmod(floor(h), 6) 44 | 45 | let f = h - floor(h) 46 | let p = v * (1 - s) 47 | let q = v * (1 - (s * f)) 48 | let t = v * (1 - (s * (1 - f))) 49 | 50 | switch hi { 51 | case 0: self.init(r: v, g: t, b: p, a: a) 52 | case 1: self.init(r: q, g: v, b: p, a: a) 53 | case 2: self.init(r: p, g: v, b: t, a: a) 54 | case 3: self.init(r: p, g: q, b: v, a: a) 55 | case 4: self.init(r: t, g: p, b: v, a: a) 56 | case 5: self.init(r: v, g: p, b: q, a: a) 57 | default: self.init(r: 0, g: 0, b: 0, a: 0) 58 | } 59 | } 60 | 61 | /// Creates a color using the specified opacity and HSL component values. 62 | /// 63 | /// Color(hue: 300, saturation: 20, lightness: 10) 64 | /// Color(hue: 300, saturation: 20, lightness: 10, alpha: 0.5) 65 | /// 66 | /// `hue` will be clamped into 0...360; `saturation`and `lightness` will be 67 | /// clamped into 1...100, `alpha` will be clamped into 0...1. 68 | public init(hue: Int, saturation: Int, lightness: Int, alpha: Double) { 69 | let h = Double(hue).clamp(min: 0, max: 360) / 360 70 | let s = Double(saturation).clamp(min: 0, max: 100) / 100 71 | let l = Double(lightness).clamp(min: 0, max: 100) / 100 72 | let a = alpha.clamp(min: 0, max: 1) 73 | 74 | if (s == 0) { 75 | self.init(r: l, g: l, b: l, a: a) 76 | return 77 | } 78 | 79 | let t2 = l < 0.5 ? (l + l * s) : (l + s - l * s) 80 | let t1 = 2 * l - t2 81 | 82 | var rgb = [0.0, 0.0, 0.0] 83 | 84 | for i in 0..<3 { 85 | var t3 = h - Double(i - 1) * 1 / 3 86 | if t3 < 0 { t3 += 1 } 87 | if t3 > 1 { t3 -= 1 } 88 | 89 | var v = 0.0 90 | if 6 * t3 < 1 { 91 | v = t1 + (t2 - t1) * 6 * t3 92 | } else if 2 * t3 < 1 { 93 | v = t2 94 | } else if 3 * t3 < 2 { 95 | v = t1 + (t2 - t1) * (2.0 / 3 - t3) * 6 96 | } else { 97 | v = t1 98 | } 99 | rgb[i] = v 100 | } 101 | 102 | self.init(r: rgb[0], g: rgb[1], b: rgb[2], a: a) 103 | } 104 | 105 | /// Creates a color using the specified hexadecimal string. 106 | /// 107 | /// "#0366d6" "0366d6" 108 | /// "#139" "139" 109 | /// "#0366d607" "0366d607" 110 | /// "#139f" "139f" 111 | public init?(hex s: String) { 112 | var hex = s.lowercased() 113 | if hex.hasPrefix("#") { 114 | hex.removeFirst() 115 | } 116 | if hex.count == 3 || hex.count == 4 { 117 | var s = "" 118 | hex.forEach { s.append($0); s.append($0) } 119 | hex = s 120 | } 121 | guard var n = Int(hex, radix: 16) else { 122 | return nil 123 | } 124 | 125 | var a = 1.0 126 | if hex.count == 8 { 127 | a = Double(n & 0xff) / 255 128 | n >>= 8 129 | } 130 | let r = (n >> 16) & 0xff 131 | let g = (n >> 8) & 0xff 132 | let b = n & 0xff 133 | self.init(red: r, green: g, blue: b, alpha: a) 134 | } 135 | 136 | /// Creates a color using the specified hexadecimal value. 137 | /// 138 | /// let color = Color(hex: 0x0366D6) 139 | public init(hex: Int) { 140 | let r = (hex >> 16) & 0xff 141 | let g = (hex >> 8) & 0xff 142 | let b = hex & 0xff 143 | self.init(red: r, green: g, blue: b) 144 | } 145 | 146 | /// Creates a color using the specified rgba string. 147 | /// 148 | /// let color = Color("rgb(3, 102, 214)") 149 | /// let color = Color("rgba(3, 102, 214, 1)") 150 | /// 151 | /// Same as `init?(rgba:)`. 152 | public init?(rgb s: String) { 153 | let pattern = "^rgba?\\((\\d+),\\s?(\\d+),\\s?(\\d+)(?:,\\s*((?:0\\.)?\\d+))?\\)$" 154 | 155 | let captures = NSRegularExpression.match(s, pattern: pattern) 156 | guard captures.count >= 4, 157 | let r = Int(captures[1]), 158 | let g = Int(captures[2]), 159 | let b = Int(captures[3]) else { 160 | return nil 161 | } 162 | 163 | var alpha = 1.0 164 | if captures.count > 4, let a = Double(captures[4]) { 165 | alpha = a 166 | } 167 | 168 | self.init(red: r, green: g, blue: b, alpha: alpha) 169 | } 170 | 171 | /// Creates a color using the specified rgba string. 172 | /// 173 | /// let color = Color("rgb(3, 102, 214)") 174 | /// let color = Color("rgba(3, 102, 214, 1)") 175 | /// 176 | /// Same as `init?(rgb:)`. 177 | public init?(rgba s: String) { 178 | self.init(rgb: s) 179 | } 180 | 181 | /// Creates a color using the specified hsva string. 182 | /// 183 | /// let color = Color("hsv(245, 100%, 25%)") 184 | /// let color = Color("hsva(245, 100%, 25%, 0.6)") 185 | /// 186 | /// Same as `init?(hsva:)`. 187 | public init?(hsv s: String) { 188 | let pattern = "^hsva?\\((\\d+)(?:deg)?,\\s?(\\d+)%,\\s?(\\d+)%(?:,\\s*((?:0\\.)?\\d+))?\\)$" 189 | 190 | let captures = NSRegularExpression.match(s, pattern: pattern) 191 | guard captures.count >= 4, 192 | let h = Int(captures[1]), 193 | let s = Int(captures[2]), 194 | let v = Int(captures[3]) else { 195 | return nil 196 | } 197 | 198 | var alpha = 1.0 199 | if captures.count > 4, let a = Double(captures[4]) { 200 | alpha = a 201 | } 202 | 203 | self.init(hue: h, saturation: s, value: v, alpha: alpha) 204 | } 205 | 206 | /// Creates a color using the specified hsva string. 207 | /// 208 | /// let color = Color("hsv(245, 100%, 25%)") 209 | /// let color = Color("hsva(245, 100%, 25%, 0.6)") 210 | /// 211 | /// Same as `init?(hsv:)`. 212 | public init?(hsva s: String) { 213 | self.init(hsv: s) 214 | } 215 | 216 | /// Creates a color using the specified hsla string. 217 | /// 218 | /// let color = Color("hsl(245, 100%, 25%)") 219 | /// let color = Color("hsla(245, 100%, 25%, 0.6)") 220 | /// 221 | /// Same as `init?(hsla:)`. 222 | public init?(hsl s: String) { 223 | let pattern = "^hsla?\\((\\d+)(?:deg)?,\\s?(\\d+)%,\\s?(\\d+)%(?:,\\s*((?:0\\.)?\\d+))?\\)$" 224 | 225 | let captures = NSRegularExpression.match(s, pattern: pattern) 226 | guard captures.count >= 4, 227 | let h = Int(captures[1]), 228 | let s = Int(captures[2]), 229 | let l = Int(captures[3]) else { 230 | return nil 231 | } 232 | 233 | var alpha = 1.0 234 | if captures.count > 4, let a = Double(captures[4]) { 235 | alpha = a 236 | } 237 | 238 | self.init(hue: h, saturation: s, lightness: l, alpha: alpha) 239 | } 240 | 241 | /// Creates a color using the specified hsla string. 242 | /// 243 | /// let color = Color("hsl(245, 100%, 25%)") 244 | /// let color = Color("hsla(245, 100%, 25%, 0.6)") 245 | /// 246 | /// Same as `init?(hsl:)`. 247 | public init?(hsla s: String) { 248 | self.init(hsl: s) 249 | } 250 | } 251 | 252 | extension Color { 253 | public static func rgb(_ red: Int, _ green: Int, _ blue: Int, _ alpha: Double = 1) -> Color { 254 | return Color(red: red, green: green, blue: blue, alpha: alpha) 255 | } 256 | 257 | public static func hsl(_ hue: Int, _ saturation: Int, _ lightness: Int, _ alpha: Double = 1) -> Color { 258 | return Color(hue: hue, saturation: saturation, lightness: lightness, alpha: alpha) 259 | } 260 | 261 | public static func hsv(_ hue: Int, _ saturation: Int, _ value: Int, _ alpha: Double = 1) -> Color { 262 | return Color(hue: hue, saturation: saturation, value: value, alpha: alpha) 263 | } 264 | 265 | public static func hex(_ s: String) -> Color? { 266 | return Color(hex: s) 267 | } 268 | 269 | public static func hex(_ n: Int) -> Color { 270 | return Color(hex: n) 271 | } 272 | } 273 | 274 | // MARK: - Properties 275 | extension Color { 276 | 277 | public var rgba: (red: Int, green: Int, blue: Int, alpha: Double) { 278 | return (red: Int(r * 255), green: Int(g * 255), blue: Int(b * 255), alpha: a) 279 | } 280 | 281 | public var hsva: (hue: Int, saturation: Int, value: Int, alpha: Double) { 282 | let max = Swift.max(r, g, b) 283 | let min = Swift.min(r, g, b) 284 | 285 | let v = max 286 | let d = max - min 287 | 288 | var h = 0.0 289 | var s = 0.0 290 | 291 | let dc = { (c: Double) in 292 | return (v - c) / 6 / d + 0.5 293 | } 294 | 295 | if d != 0 { 296 | s = d / v 297 | let rd = dc(r) 298 | let gd = dc(g) 299 | let bd = dc(b) 300 | 301 | if r == v { 302 | h = bd - gd 303 | } else if (g == v) { 304 | h = 1.0 / 3 + rd - bd 305 | } else if (b == v) { 306 | h = 2.0 / 3 + gd - rd 307 | } 308 | if h < 0 { h += 1 } 309 | if h > 1 { h -= 1 } 310 | } 311 | 312 | return (hue: Int(h * 360), saturation: Int(s * 100), value: Int(v * 100), alpha: a) 313 | } 314 | 315 | public var hsla: (hue: Int, saturation: Int, lightness: Int, alpha: Double) { 316 | let max = Swift.max(r, g, b) 317 | let min = Swift.min(r, g, b) 318 | 319 | // calc lightness 320 | let l = (max + min) / 2.0 321 | 322 | // calc saturation 323 | var s = 0.0 324 | 325 | if (max == min) { 326 | s = 0.0 327 | } else if (l < 0.5) { 328 | s = (max - min) / (max + min) 329 | } else if (l >= 0.5) { 330 | s = (max - min) / (2.0 - max - min) 331 | } 332 | 333 | // calc hue 334 | var h = 0.0 335 | // if saturation is 0, hue is undefined, set to 0 336 | if s == 0.0 { 337 | h = 0.0 338 | } else if r == max { 339 | h = (g - b) / (max - min) 340 | } else if g == max { 341 | h = 2 + (b - r) / (max - min) 342 | } else if b == max { 343 | h = 4 + (r - g) / (max - min) 344 | } 345 | 346 | h = Swift.min(h * 60, 360) 347 | if h < 0 { h += 360 } 348 | 349 | return (hue: Int(h), saturation: Int(s * 100), lightness: Int(l * 100), alpha: a) 350 | } 351 | 352 | public var isDark: Bool { 353 | let rgba = self.rgba 354 | let yiq = Double(rgba.red * 299 + rgba.green * 587 + rgba.blue * 114) / 1000 355 | return yiq < 218 356 | } 357 | 358 | public var isLight: Bool { 359 | return !isDark 360 | } 361 | 362 | public var negate: Color { 363 | let rgba = self.rgba 364 | return Color(red: 255 - rgba.red, green: 255 - rgba.green, blue: 255 - rgba.blue, alpha: rgba.alpha) 365 | } 366 | 367 | public var isBlack: Bool { 368 | return r < 0.09 && g < 0.09 && b < 0.09 369 | } 370 | 371 | public var isWhite: Bool { 372 | return r > 0.91 && g > 0.91 && b > 0.91 373 | } 374 | 375 | /// Roughly 376 | public var ansi256: UInt8 { 377 | if r == g && g == b { 378 | let red = r * 255 379 | if red < 8 { return 16 } 380 | if red > 248 { return 231 } 381 | 382 | return UInt8(round((red - 8) / 247 * 24) + 232) 383 | } 384 | 385 | let v0 = 16.0 386 | let v1 = 36 * round(r * 5) 387 | let v2 = 6 * round(g * 5) 388 | let v3 = round(b * 5) 389 | 390 | return UInt8(v0 + v1 + v2 + v3) 391 | } 392 | 393 | /// Roughly 394 | public var ansi16: UInt8 { 395 | let v = round(Double(self.hsva.value) / 50) 396 | 397 | if v == 0 { 398 | return 30 399 | } 400 | 401 | let i0: UInt8 = 30 402 | let i1 = UInt8(round(b)) << 2 403 | let i2 = UInt8(round(g)) << 1 404 | let i3 = UInt8(round(r)) 405 | 406 | var c = i0 + i1 | i2 | i3 407 | 408 | if v == 2 { 409 | c += 60 410 | } 411 | 412 | return c 413 | } 414 | } 415 | 416 | // MARK: - Hashable & Equatable 417 | extension Color: Hashable { } 418 | 419 | // MARK: - Misc 420 | extension Color { 421 | 422 | public static var random: Color { 423 | let randomRGB = { Int.random(in: 0...255) } 424 | let randomA = { Double.random(in: 0...1) } 425 | return Color(red: randomRGB(), green: randomRGB(), blue: randomRGB(), alpha: randomA()) 426 | } 427 | } 428 | -------------------------------------------------------------------------------- /Sources/Rainbow/Colors.swift: -------------------------------------------------------------------------------- 1 | /* 2 | https://github.com/SwifterSwift/SwifterSwift/blob/master/Sources/SwifterSwift/Shared/ColorExtensions.swift 3 | */ 4 | 5 | // MARK: - Material colors 6 | public extension Color { 7 | 8 | // swiftlint:disable type_body_length 9 | /// SwifterSwift: Google Material design colors palette. 10 | struct Material { 11 | // https://material.google.com/style/color.html 12 | 13 | private init() {} 14 | 15 | /// SwifterSwift: color red500 16 | public static let red = red500 17 | 18 | /// SwifterSwift: hex #FFEBEE 19 | public static let red50 = Color(hex: 0xFFEBEE) 20 | 21 | /// SwifterSwift: hex #FFCDD2 22 | public static let red100 = Color(hex: 0xFFCDD2) 23 | 24 | /// SwifterSwift: hex #EF9A9A 25 | public static let red200 = Color(hex: 0xEF9A9A) 26 | 27 | /// SwifterSwift: hex #E57373 28 | public static let red300 = Color(hex: 0xE57373) 29 | 30 | /// SwifterSwift: hex #EF5350 31 | public static let red400 = Color(hex: 0xEF5350) 32 | 33 | /// SwifterSwift: hex #F44336 34 | public static let red500 = Color(hex: 0xF44336) 35 | 36 | /// SwifterSwift: hex #E53935 37 | public static let red600 = Color(hex: 0xE53935) 38 | 39 | /// SwifterSwift: hex #D32F2F 40 | public static let red700 = Color(hex: 0xD32F2F) 41 | 42 | /// SwifterSwift: hex #C62828 43 | public static let red800 = Color(hex: 0xC62828) 44 | 45 | /// SwifterSwift: hex #B71C1C 46 | public static let red900 = Color(hex: 0xB71C1C) 47 | 48 | /// SwifterSwift: hex #FF8A80 49 | public static let redA100 = Color(hex: 0xFF8A80) 50 | 51 | /// SwifterSwift: hex #FF5252 52 | public static let redA200 = Color(hex: 0xFF5252) 53 | 54 | /// SwifterSwift: hex #FF1744 55 | public static let redA400 = Color(hex: 0xFF1744) 56 | 57 | /// SwifterSwift: hex #D50000 58 | public static let redA700 = Color(hex: 0xD50000) 59 | 60 | /// SwifterSwift: color pink500 61 | public static let pink = pink500 62 | 63 | /// SwifterSwift: hex #FCE4EC 64 | public static let pink50 = Color(hex: 0xFCE4EC) 65 | 66 | /// SwifterSwift: hex #F8BBD0 67 | public static let pink100 = Color(hex: 0xF8BBD0) 68 | 69 | /// SwifterSwift: hex #F48FB1 70 | public static let pink200 = Color(hex: 0xF48FB1) 71 | 72 | /// SwifterSwift: hex #F06292 73 | public static let pink300 = Color(hex: 0xF06292) 74 | 75 | /// SwifterSwift: hex #EC407A 76 | public static let pink400 = Color(hex: 0xEC407A) 77 | 78 | /// SwifterSwift: hex #E91E63 79 | public static let pink500 = Color(hex: 0xE91E63) 80 | 81 | /// SwifterSwift: hex #D81B60 82 | public static let pink600 = Color(hex: 0xD81B60) 83 | 84 | /// SwifterSwift: hex #C2185B 85 | public static let pink700 = Color(hex: 0xC2185B) 86 | 87 | /// SwifterSwift: hex #AD1457 88 | public static let pink800 = Color(hex: 0xAD1457) 89 | 90 | /// SwifterSwift: hex #880E4F 91 | public static let pink900 = Color(hex: 0x880E4F) 92 | 93 | /// SwifterSwift: hex #FF80AB 94 | public static let pinkA100 = Color(hex: 0xFF80AB) 95 | 96 | /// SwifterSwift: hex #FF4081 97 | public static let pinkA200 = Color(hex: 0xFF4081) 98 | 99 | /// SwifterSwift: hex #F50057 100 | public static let pinkA400 = Color(hex: 0xF50057) 101 | 102 | /// SwifterSwift: hex #C51162 103 | public static let pinkA700 = Color(hex: 0xC51162) 104 | 105 | /// SwifterSwift: color purple500 106 | public static let purple = purple500 107 | 108 | /// SwifterSwift: hex #F3E5F5 109 | public static let purple50 = Color(hex: 0xF3E5F5) 110 | 111 | /// SwifterSwift: hex #E1BEE7 112 | public static let purple100 = Color(hex: 0xE1BEE7) 113 | 114 | /// SwifterSwift: hex #CE93D8 115 | public static let purple200 = Color(hex: 0xCE93D8) 116 | 117 | /// SwifterSwift: hex #BA68C8 118 | public static let purple300 = Color(hex: 0xBA68C8) 119 | 120 | /// SwifterSwift: hex #AB47BC 121 | public static let purple400 = Color(hex: 0xAB47BC) 122 | 123 | /// SwifterSwift: hex #9C27B0 124 | public static let purple500 = Color(hex: 0x9C27B0) 125 | 126 | /// SwifterSwift: hex #8E24AA 127 | public static let purple600 = Color(hex: 0x8E24AA) 128 | 129 | /// SwifterSwift: hex #7B1FA2 130 | public static let purple700 = Color(hex: 0x7B1FA2) 131 | 132 | /// SwifterSwift: hex #6A1B9A 133 | public static let purple800 = Color(hex: 0x6A1B9A) 134 | 135 | /// SwifterSwift: hex #4A148C 136 | public static let purple900 = Color(hex: 0x4A148C) 137 | 138 | /// SwifterSwift: hex #EA80FC 139 | public static let purpleA100 = Color(hex: 0xEA80FC) 140 | 141 | /// SwifterSwift: hex #E040FB 142 | public static let purpleA200 = Color(hex: 0xE040FB) 143 | 144 | /// SwifterSwift: hex #D500F9 145 | public static let purpleA400 = Color(hex: 0xD500F9) 146 | 147 | /// SwifterSwift: hex #AA00FF 148 | public static let purpleA700 = Color(hex: 0xAA00FF) 149 | 150 | /// SwifterSwift: color deepPurple500 151 | public static let deepPurple = deepPurple500 152 | 153 | /// SwifterSwift: hex #EDE7F6 154 | public static let deepPurple50 = Color(hex: 0xEDE7F6) 155 | 156 | /// SwifterSwift: hex #D1C4E9 157 | public static let deepPurple100 = Color(hex: 0xD1C4E9) 158 | 159 | /// SwifterSwift: hex #B39DDB 160 | public static let deepPurple200 = Color(hex: 0xB39DDB) 161 | 162 | /// SwifterSwift: hex #9575CD 163 | public static let deepPurple300 = Color(hex: 0x9575CD) 164 | 165 | /// SwifterSwift: hex #7E57C2 166 | public static let deepPurple400 = Color(hex: 0x7E57C2) 167 | 168 | /// SwifterSwift: hex #673AB7 169 | public static let deepPurple500 = Color(hex: 0x673AB7) 170 | 171 | /// SwifterSwift: hex #5E35B1 172 | public static let deepPurple600 = Color(hex: 0x5E35B1) 173 | 174 | /// SwifterSwift: hex #512DA8 175 | public static let deepPurple700 = Color(hex: 0x512DA8) 176 | 177 | /// SwifterSwift: hex #4527A0 178 | public static let deepPurple800 = Color(hex: 0x4527A0) 179 | 180 | /// SwifterSwift: hex #311B92 181 | public static let deepPurple900 = Color(hex: 0x311B92) 182 | 183 | /// SwifterSwift: hex #B388FF 184 | public static let deepPurpleA100 = Color(hex: 0xB388FF) 185 | 186 | /// SwifterSwift: hex #7C4DFF 187 | public static let deepPurpleA200 = Color(hex: 0x7C4DFF) 188 | 189 | /// SwifterSwift: hex #651FFF 190 | public static let deepPurpleA400 = Color(hex: 0x651FFF) 191 | 192 | /// SwifterSwift: hex #6200EA 193 | public static let deepPurpleA700 = Color(hex: 0x6200EA) 194 | 195 | /// SwifterSwift: color indigo500 196 | public static let indigo = indigo500 197 | 198 | /// SwifterSwift: hex #E8EAF6 199 | public static let indigo50 = Color(hex: 0xE8EAF6) 200 | 201 | /// SwifterSwift: hex #C5CAE9 202 | public static let indigo100 = Color(hex: 0xC5CAE9) 203 | 204 | /// SwifterSwift: hex #9FA8DA 205 | public static let indigo200 = Color(hex: 0x9FA8DA) 206 | 207 | /// SwifterSwift: hex #7986CB 208 | public static let indigo300 = Color(hex: 0x7986CB) 209 | 210 | /// SwifterSwift: hex #5C6BC0 211 | public static let indigo400 = Color(hex: 0x5C6BC0) 212 | 213 | /// SwifterSwift: hex #3F51B5 214 | public static let indigo500 = Color(hex: 0x3F51B5) 215 | 216 | /// SwifterSwift: hex #3949AB 217 | public static let indigo600 = Color(hex: 0x3949AB) 218 | 219 | /// SwifterSwift: hex #303F9F 220 | public static let indigo700 = Color(hex: 0x303F9F) 221 | 222 | /// SwifterSwift: hex #283593 223 | public static let indigo800 = Color(hex: 0x283593) 224 | 225 | /// SwifterSwift: hex #1A237E 226 | public static let indigo900 = Color(hex: 0x1A237E) 227 | 228 | /// SwifterSwift: hex #8C9EFF 229 | public static let indigoA100 = Color(hex: 0x8C9EFF) 230 | 231 | /// SwifterSwift: hex #536DFE 232 | public static let indigoA200 = Color(hex: 0x536DFE) 233 | 234 | /// SwifterSwift: hex #3D5AFE 235 | public static let indigoA400 = Color(hex: 0x3D5AFE) 236 | 237 | /// SwifterSwift: hex #304FFE 238 | public static let indigoA700 = Color(hex: 0x304FFE) 239 | 240 | /// SwifterSwift: color blue500 241 | public static let blue = blue500 242 | 243 | /// SwifterSwift: hex #E3F2FD 244 | public static let blue50 = Color(hex: 0xE3F2FD) 245 | 246 | /// SwifterSwift: hex #BBDEFB 247 | public static let blue100 = Color(hex: 0xBBDEFB) 248 | 249 | /// SwifterSwift: hex #90CAF9 250 | public static let blue200 = Color(hex: 0x90CAF9) 251 | 252 | /// SwifterSwift: hex #64B5F6 253 | public static let blue300 = Color(hex: 0x64B5F6) 254 | 255 | /// SwifterSwift: hex #42A5F5 256 | public static let blue400 = Color(hex: 0x42A5F5) 257 | 258 | /// SwifterSwift: hex #2196F3 259 | public static let blue500 = Color(hex: 0x2196F3) 260 | 261 | /// SwifterSwift: hex #1E88E5 262 | public static let blue600 = Color(hex: 0x1E88E5) 263 | 264 | /// SwifterSwift: hex #1976D2 265 | public static let blue700 = Color(hex: 0x1976D2) 266 | 267 | /// SwifterSwift: hex #1565C0 268 | public static let blue800 = Color(hex: 0x1565C0) 269 | 270 | /// SwifterSwift: hex #0D47A1 271 | public static let blue900 = Color(hex: 0x0D47A1) 272 | 273 | /// SwifterSwift: hex #82B1FF 274 | public static let blueA100 = Color(hex: 0x82B1FF) 275 | 276 | /// SwifterSwift: hex #448AFF 277 | public static let blueA200 = Color(hex: 0x448AFF) 278 | 279 | /// SwifterSwift: hex #2979FF 280 | public static let blueA400 = Color(hex: 0x2979FF) 281 | 282 | /// SwifterSwift: hex #2962FF 283 | public static let blueA700 = Color(hex: 0x2962FF) 284 | 285 | /// SwifterSwift: color lightBlue500 286 | public static let lightBlue = lightBlue500 287 | 288 | /// SwifterSwift: hex #E1F5FE 289 | public static let lightBlue50 = Color(hex: 0xE1F5FE) 290 | 291 | /// SwifterSwift: hex #B3E5FC 292 | public static let lightBlue100 = Color(hex: 0xB3E5FC) 293 | 294 | /// SwifterSwift: hex #81D4FA 295 | public static let lightBlue200 = Color(hex: 0x81D4FA) 296 | 297 | /// SwifterSwift: hex #4FC3F7 298 | public static let lightBlue300 = Color(hex: 0x4FC3F7) 299 | 300 | /// SwifterSwift: hex #29B6F6 301 | public static let lightBlue400 = Color(hex: 0x29B6F6) 302 | 303 | /// SwifterSwift: hex #03A9F4 304 | public static let lightBlue500 = Color(hex: 0x03A9F4) 305 | 306 | /// SwifterSwift: hex #039BE5 307 | public static let lightBlue600 = Color(hex: 0x039BE5) 308 | 309 | /// SwifterSwift: hex #0288D1 310 | public static let lightBlue700 = Color(hex: 0x0288D1) 311 | 312 | /// SwifterSwift: hex #0277BD 313 | public static let lightBlue800 = Color(hex: 0x0277BD) 314 | 315 | /// SwifterSwift: hex #01579B 316 | public static let lightBlue900 = Color(hex: 0x01579B) 317 | 318 | /// SwifterSwift: hex #80D8FF 319 | public static let lightBlueA100 = Color(hex: 0x80D8FF) 320 | 321 | /// SwifterSwift: hex #40C4FF 322 | public static let lightBlueA200 = Color(hex: 0x40C4FF) 323 | 324 | /// SwifterSwift: hex #00B0FF 325 | public static let lightBlueA400 = Color(hex: 0x00B0FF) 326 | 327 | /// SwifterSwift: hex #0091EA 328 | public static let lightBlueA700 = Color(hex: 0x0091EA) 329 | 330 | /// SwifterSwift: color cyan500 331 | public static let cyan = cyan500 332 | 333 | /// SwifterSwift: hex #E0F7FA 334 | public static let cyan50 = Color(hex: 0xE0F7FA) 335 | 336 | /// SwifterSwift: hex #B2EBF2 337 | public static let cyan100 = Color(hex: 0xB2EBF2) 338 | 339 | /// SwifterSwift: hex #80DEEA 340 | public static let cyan200 = Color(hex: 0x80DEEA) 341 | 342 | /// SwifterSwift: hex #4DD0E1 343 | public static let cyan300 = Color(hex: 0x4DD0E1) 344 | 345 | /// SwifterSwift: hex #26C6DA 346 | public static let cyan400 = Color(hex: 0x26C6DA) 347 | 348 | /// SwifterSwift: hex #00BCD4 349 | public static let cyan500 = Color(hex: 0x00BCD4) 350 | 351 | /// SwifterSwift: hex #00ACC1 352 | public static let cyan600 = Color(hex: 0x00ACC1) 353 | 354 | /// SwifterSwift: hex #0097A7 355 | public static let cyan700 = Color(hex: 0x0097A7) 356 | 357 | /// SwifterSwift: hex #00838F 358 | public static let cyan800 = Color(hex: 0x00838F) 359 | 360 | /// SwifterSwift: hex #006064 361 | public static let cyan900 = Color(hex: 0x006064) 362 | 363 | /// SwifterSwift: hex #84FFFF 364 | public static let cyanA100 = Color(hex: 0x84FFFF) 365 | 366 | /// SwifterSwift: hex #18FFFF 367 | public static let cyanA200 = Color(hex: 0x18FFFF) 368 | 369 | /// SwifterSwift: hex #00E5FF 370 | public static let cyanA400 = Color(hex: 0x00E5FF) 371 | 372 | /// SwifterSwift: hex #00B8D4 373 | public static let cyanA700 = Color(hex: 0x00B8D4) 374 | 375 | /// SwifterSwift: color teal500 376 | public static let teal = teal500 377 | 378 | /// SwifterSwift: hex #E0F2F1 379 | public static let teal50 = Color(hex: 0xE0F2F1) 380 | 381 | /// SwifterSwift: hex #B2DFDB 382 | public static let teal100 = Color(hex: 0xB2DFDB) 383 | 384 | /// SwifterSwift: hex #80CBC4 385 | public static let teal200 = Color(hex: 0x80CBC4) 386 | 387 | /// SwifterSwift: hex #4DB6AC 388 | public static let teal300 = Color(hex: 0x4DB6AC) 389 | 390 | /// SwifterSwift: hex #26A69A 391 | public static let teal400 = Color(hex: 0x26A69A) 392 | 393 | /// SwifterSwift: hex #009688 394 | public static let teal500 = Color(hex: 0x009688) 395 | 396 | /// SwifterSwift: hex #00897B 397 | public static let teal600 = Color(hex: 0x00897B) 398 | 399 | /// SwifterSwift: hex #00796B 400 | public static let teal700 = Color(hex: 0x00796B) 401 | 402 | /// SwifterSwift: hex #00695C 403 | public static let teal800 = Color(hex: 0x00695C) 404 | 405 | /// SwifterSwift: hex #004D40 406 | public static let teal900 = Color(hex: 0x004D40) 407 | 408 | /// SwifterSwift: hex #A7FFEB 409 | public static let tealA100 = Color(hex: 0xA7FFEB) 410 | 411 | /// SwifterSwift: hex #64FFDA 412 | public static let tealA200 = Color(hex: 0x64FFDA) 413 | 414 | /// SwifterSwift: hex #1DE9B6 415 | public static let tealA400 = Color(hex: 0x1DE9B6) 416 | 417 | /// SwifterSwift: hex #00BFA5 418 | public static let tealA700 = Color(hex: 0x00BFA5) 419 | 420 | /// SwifterSwift: color green500 421 | public static let green = green500 422 | 423 | /// SwifterSwift: hex #E8F5E9 424 | public static let green50 = Color(hex: 0xE8F5E9) 425 | 426 | /// SwifterSwift: hex #C8E6C9 427 | public static let green100 = Color(hex: 0xC8E6C9) 428 | 429 | /// SwifterSwift: hex #A5D6A7 430 | public static let green200 = Color(hex: 0xA5D6A7) 431 | 432 | /// SwifterSwift: hex #81C784 433 | public static let green300 = Color(hex: 0x81C784) 434 | 435 | /// SwifterSwift: hex #66BB6A 436 | public static let green400 = Color(hex: 0x66BB6A) 437 | 438 | /// SwifterSwift: hex #4CAF50 439 | public static let green500 = Color(hex: 0x4CAF50) 440 | 441 | /// SwifterSwift: hex #43A047 442 | public static let green600 = Color(hex: 0x43A047) 443 | 444 | /// SwifterSwift: hex #388E3C 445 | public static let green700 = Color(hex: 0x388E3C) 446 | 447 | /// SwifterSwift: hex #2E7D32 448 | public static let green800 = Color(hex: 0x2E7D32) 449 | 450 | /// SwifterSwift: hex #1B5E20 451 | public static let green900 = Color(hex: 0x1B5E20) 452 | 453 | /// SwifterSwift: hex #B9F6CA 454 | public static let greenA100 = Color(hex: 0xB9F6CA) 455 | 456 | /// SwifterSwift: hex #69F0AE 457 | public static let greenA200 = Color(hex: 0x69F0AE) 458 | 459 | /// SwifterSwift: hex #00E676 460 | public static let greenA400 = Color(hex: 0x00E676) 461 | 462 | /// SwifterSwift: hex #00C853 463 | public static let greenA700 = Color(hex: 0x00C853) 464 | 465 | /// SwifterSwift: color lightGreen500 466 | public static let lightGreen = lightGreen500 467 | 468 | /// SwifterSwift: hex #F1F8E9 469 | public static let lightGreen50 = Color(hex: 0xF1F8E9) 470 | 471 | /// SwifterSwift: hex #DCEDC8 472 | public static let lightGreen100 = Color(hex: 0xDCEDC8) 473 | 474 | /// SwifterSwift: hex #C5E1A5 475 | public static let lightGreen200 = Color(hex: 0xC5E1A5) 476 | 477 | /// SwifterSwift: hex #AED581 478 | public static let lightGreen300 = Color(hex: 0xAED581) 479 | 480 | /// SwifterSwift: hex #9CCC65 481 | public static let lightGreen400 = Color(hex: 0x9CCC65) 482 | 483 | /// SwifterSwift: hex #8BC34A 484 | public static let lightGreen500 = Color(hex: 0x8BC34A) 485 | 486 | /// SwifterSwift: hex #7CB342 487 | public static let lightGreen600 = Color(hex: 0x7CB342) 488 | 489 | /// SwifterSwift: hex #689F38 490 | public static let lightGreen700 = Color(hex: 0x689F38) 491 | 492 | /// SwifterSwift: hex #558B2F 493 | public static let lightGreen800 = Color(hex: 0x558B2F) 494 | 495 | /// SwifterSwift: hex #33691E 496 | public static let lightGreen900 = Color(hex: 0x33691E) 497 | 498 | /// SwifterSwift: hex #CCFF90 499 | public static let lightGreenA100 = Color(hex: 0xCCFF90) 500 | 501 | /// SwifterSwift: hex #B2FF59 502 | public static let lightGreenA200 = Color(hex: 0xB2FF59) 503 | 504 | /// SwifterSwift: hex #76FF03 505 | public static let lightGreenA400 = Color(hex: 0x76FF03) 506 | 507 | /// SwifterSwift: hex #64DD17 508 | public static let lightGreenA700 = Color(hex: 0x64DD17) 509 | 510 | /// SwifterSwift: color lime500 511 | public static let lime = lime500 512 | 513 | /// SwifterSwift: hex #F9FBE7 514 | public static let lime50 = Color(hex: 0xF9FBE7) 515 | 516 | /// SwifterSwift: hex #F0F4C3 517 | public static let lime100 = Color(hex: 0xF0F4C3) 518 | 519 | /// SwifterSwift: hex #E6EE9C 520 | public static let lime200 = Color(hex: 0xE6EE9C) 521 | 522 | /// SwifterSwift: hex #DCE775 523 | public static let lime300 = Color(hex: 0xDCE775) 524 | 525 | /// SwifterSwift: hex #D4E157 526 | public static let lime400 = Color(hex: 0xD4E157) 527 | 528 | /// SwifterSwift: hex #CDDC39 529 | public static let lime500 = Color(hex: 0xCDDC39) 530 | 531 | /// SwifterSwift: hex #C0CA33 532 | public static let lime600 = Color(hex: 0xC0CA33) 533 | 534 | /// SwifterSwift: hex #AFB42B 535 | public static let lime700 = Color(hex: 0xAFB42B) 536 | 537 | /// SwifterSwift: hex #9E9D24 538 | public static let lime800 = Color(hex: 0x9E9D24) 539 | 540 | /// SwifterSwift: hex #827717 541 | public static let lime900 = Color(hex: 0x827717) 542 | 543 | /// SwifterSwift: hex #F4FF81 544 | public static let limeA100 = Color(hex: 0xF4FF81) 545 | 546 | /// SwifterSwift: hex #EEFF41 547 | public static let limeA200 = Color(hex: 0xEEFF41) 548 | 549 | /// SwifterSwift: hex #C6FF00 550 | public static let limeA400 = Color(hex: 0xC6FF00) 551 | 552 | /// SwifterSwift: hex #AEEA00 553 | public static let limeA700 = Color(hex: 0xAEEA00) 554 | 555 | /// SwifterSwift: color yellow500 556 | public static let yellow = yellow500 557 | 558 | /// SwifterSwift: hex #FFFDE7 559 | public static let yellow50 = Color(hex: 0xFFFDE7) 560 | 561 | /// SwifterSwift: hex #FFF9C4 562 | public static let yellow100 = Color(hex: 0xFFF9C4) 563 | 564 | /// SwifterSwift: hex #FFF59D 565 | public static let yellow200 = Color(hex: 0xFFF59D) 566 | 567 | /// SwifterSwift: hex #FFF176 568 | public static let yellow300 = Color(hex: 0xFFF176) 569 | 570 | /// SwifterSwift: hex #FFEE58 571 | public static let yellow400 = Color(hex: 0xFFEE58) 572 | 573 | /// SwifterSwift: hex #FFEB3B 574 | public static let yellow500 = Color(hex: 0xFFEB3B) 575 | 576 | /// SwifterSwift: hex #FDD835 577 | public static let yellow600 = Color(hex: 0xFDD835) 578 | 579 | /// SwifterSwift: hex #FBC02D 580 | public static let yellow700 = Color(hex: 0xFBC02D) 581 | 582 | /// SwifterSwift: hex #F9A825 583 | public static let yellow800 = Color(hex: 0xF9A825) 584 | 585 | /// SwifterSwift: hex #F57F17 586 | public static let yellow900 = Color(hex: 0xF57F17) 587 | 588 | /// SwifterSwift: hex #FFFF8D 589 | public static let yellowA100 = Color(hex: 0xFFFF8D) 590 | 591 | /// SwifterSwift: hex #FFFF00 592 | public static let yellowA200 = Color(hex: 0xFFFF00) 593 | 594 | /// SwifterSwift: hex #FFEA00 595 | public static let yellowA400 = Color(hex: 0xFFEA00) 596 | 597 | /// SwifterSwift: hex #FFD600 598 | public static let yellowA700 = Color(hex: 0xFFD600) 599 | 600 | /// SwifterSwift: color amber500 601 | public static let amber = amber500 602 | 603 | /// SwifterSwift: hex #FFF8E1 604 | public static let amber50 = Color(hex: 0xFFF8E1) 605 | 606 | /// SwifterSwift: hex #FFECB3 607 | public static let amber100 = Color(hex: 0xFFECB3) 608 | 609 | /// SwifterSwift: hex #FFE082 610 | public static let amber200 = Color(hex: 0xFFE082) 611 | 612 | /// SwifterSwift: hex #FFD54F 613 | public static let amber300 = Color(hex: 0xFFD54F) 614 | 615 | /// SwifterSwift: hex #FFCA28 616 | public static let amber400 = Color(hex: 0xFFCA28) 617 | 618 | /// SwifterSwift: hex #FFC107 619 | public static let amber500 = Color(hex: 0xFFC107) 620 | 621 | /// SwifterSwift: hex #FFB300 622 | public static let amber600 = Color(hex: 0xFFB300) 623 | 624 | /// SwifterSwift: hex #FFA000 625 | public static let amber700 = Color(hex: 0xFFA000) 626 | 627 | /// SwifterSwift: hex #FF8F00 628 | public static let amber800 = Color(hex: 0xFF8F00) 629 | 630 | /// SwifterSwift: hex #FF6F00 631 | public static let amber900 = Color(hex: 0xFF6F00) 632 | 633 | /// SwifterSwift: hex #FFE57F 634 | public static let amberA100 = Color(hex: 0xFFE57F) 635 | 636 | /// SwifterSwift: hex #FFD740 637 | public static let amberA200 = Color(hex: 0xFFD740) 638 | 639 | /// SwifterSwift: hex #FFC400 640 | public static let amberA400 = Color(hex: 0xFFC400) 641 | 642 | /// SwifterSwift: hex #FFAB00 643 | public static let amberA700 = Color(hex: 0xFFAB00) 644 | 645 | /// SwifterSwift: color orange500 646 | public static let orange = orange500 647 | 648 | /// SwifterSwift: hex #FFF3E0 649 | public static let orange50 = Color(hex: 0xFFF3E0) 650 | 651 | /// SwifterSwift: hex #FFE0B2 652 | public static let orange100 = Color(hex: 0xFFE0B2) 653 | 654 | /// SwifterSwift: hex #FFCC80 655 | public static let orange200 = Color(hex: 0xFFCC80) 656 | 657 | /// SwifterSwift: hex #FFB74D 658 | public static let orange300 = Color(hex: 0xFFB74D) 659 | 660 | /// SwifterSwift: hex #FFA726 661 | public static let orange400 = Color(hex: 0xFFA726) 662 | 663 | /// SwifterSwift: hex #FF9800 664 | public static let orange500 = Color(hex: 0xFF9800) 665 | 666 | /// SwifterSwift: hex #FB8C00 667 | public static let orange600 = Color(hex: 0xFB8C00) 668 | 669 | /// SwifterSwift: hex #F57C00 670 | public static let orange700 = Color(hex: 0xF57C00) 671 | 672 | /// SwifterSwift: hex #EF6C00 673 | public static let orange800 = Color(hex: 0xEF6C00) 674 | 675 | /// SwifterSwift: hex #E65100 676 | public static let orange900 = Color(hex: 0xE65100) 677 | 678 | /// SwifterSwift: hex #FFD180 679 | public static let orangeA100 = Color(hex: 0xFFD180) 680 | 681 | /// SwifterSwift: hex #FFAB40 682 | public static let orangeA200 = Color(hex: 0xFFAB40) 683 | 684 | /// SwifterSwift: hex #FF9100 685 | public static let orangeA400 = Color(hex: 0xFF9100) 686 | 687 | /// SwifterSwift: hex #FF6D00 688 | public static let orangeA700 = Color(hex: 0xFF6D00) 689 | 690 | /// SwifterSwift: color deepOrange500 691 | public static let deepOrange = deepOrange500 692 | 693 | /// SwifterSwift: hex #FBE9E7 694 | public static let deepOrange50 = Color(hex: 0xFBE9E7) 695 | 696 | /// SwifterSwift: hex #FFCCBC 697 | public static let deepOrange100 = Color(hex: 0xFFCCBC) 698 | 699 | /// SwifterSwift: hex #FFAB91 700 | public static let deepOrange200 = Color(hex: 0xFFAB91) 701 | 702 | /// SwifterSwift: hex #FF8A65 703 | public static let deepOrange300 = Color(hex: 0xFF8A65) 704 | 705 | /// SwifterSwift: hex #FF7043 706 | public static let deepOrange400 = Color(hex: 0xFF7043) 707 | 708 | /// SwifterSwift: hex #FF5722 709 | public static let deepOrange500 = Color(hex: 0xFF5722) 710 | 711 | /// SwifterSwift: hex #F4511E 712 | public static let deepOrange600 = Color(hex: 0xF4511E) 713 | 714 | /// SwifterSwift: hex #E64A19 715 | public static let deepOrange700 = Color(hex: 0xE64A19) 716 | 717 | /// SwifterSwift: hex #D84315 718 | public static let deepOrange800 = Color(hex: 0xD84315) 719 | 720 | /// SwifterSwift: hex #BF360C 721 | public static let deepOrange900 = Color(hex: 0xBF360C) 722 | 723 | /// SwifterSwift: hex #FF9E80 724 | public static let deepOrangeA100 = Color(hex: 0xFF9E80) 725 | 726 | /// SwifterSwift: hex #FF6E40 727 | public static let deepOrangeA200 = Color(hex: 0xFF6E40) 728 | 729 | /// SwifterSwift: hex #FF3D00 730 | public static let deepOrangeA400 = Color(hex: 0xFF3D00) 731 | 732 | /// SwifterSwift: hex #DD2C00 733 | public static let deepOrangeA700 = Color(hex: 0xDD2C00) 734 | 735 | /// SwifterSwift: color brown500 736 | public static let brown = brown500 737 | 738 | /// SwifterSwift: hex #EFEBE9 739 | public static let brown50 = Color(hex: 0xEFEBE9) 740 | 741 | /// SwifterSwift: hex #D7CCC8 742 | public static let brown100 = Color(hex: 0xD7CCC8) 743 | 744 | /// SwifterSwift: hex #BCAAA4 745 | public static let brown200 = Color(hex: 0xBCAAA4) 746 | 747 | /// SwifterSwift: hex #A1887F 748 | public static let brown300 = Color(hex: 0xA1887F) 749 | 750 | /// SwifterSwift: hex #8D6E63 751 | public static let brown400 = Color(hex: 0x8D6E63) 752 | 753 | /// SwifterSwift: hex #795548 754 | public static let brown500 = Color(hex: 0x795548) 755 | 756 | /// SwifterSwift: hex #6D4C41 757 | public static let brown600 = Color(hex: 0x6D4C41) 758 | 759 | /// SwifterSwift: hex #5D4037 760 | public static let brown700 = Color(hex: 0x5D4037) 761 | 762 | /// SwifterSwift: hex #4E342E 763 | public static let brown800 = Color(hex: 0x4E342E) 764 | 765 | /// SwifterSwift: hex #3E2723 766 | public static let brown900 = Color(hex: 0x3E2723) 767 | 768 | /// SwifterSwift: color grey500 769 | public static let grey = grey500 770 | 771 | /// SwifterSwift: hex #FAFAFA 772 | public static let grey50 = Color(hex: 0xFAFAFA) 773 | 774 | /// SwifterSwift: hex #F5F5F5 775 | public static let grey100 = Color(hex: 0xF5F5F5) 776 | 777 | /// SwifterSwift: hex #EEEEEE 778 | public static let grey200 = Color(hex: 0xEEEEEE) 779 | 780 | /// SwifterSwift: hex #E0E0E0 781 | public static let grey300 = Color(hex: 0xE0E0E0) 782 | 783 | /// SwifterSwift: hex #BDBDBD 784 | public static let grey400 = Color(hex: 0xBDBDBD) 785 | 786 | /// SwifterSwift: hex #9E9E9E 787 | public static let grey500 = Color(hex: 0x9E9E9E) 788 | 789 | /// SwifterSwift: hex #757575 790 | public static let grey600 = Color(hex: 0x757575) 791 | 792 | /// SwifterSwift: hex #616161 793 | public static let grey700 = Color(hex: 0x616161) 794 | 795 | /// SwifterSwift: hex #424242 796 | public static let grey800 = Color(hex: 0x424242) 797 | 798 | /// SwifterSwift: hex #212121 799 | public static let grey900 = Color(hex: 0x212121) 800 | 801 | /// SwifterSwift: color blueGrey500 802 | public static let blueGrey = blueGrey500 803 | 804 | /// SwifterSwift: hex #ECEFF1 805 | public static let blueGrey50 = Color(hex: 0xECEFF1) 806 | 807 | /// SwifterSwift: hex #CFD8DC 808 | public static let blueGrey100 = Color(hex: 0xCFD8DC) 809 | 810 | /// SwifterSwift: hex #B0BEC5 811 | public static let blueGrey200 = Color(hex: 0xB0BEC5) 812 | 813 | /// SwifterSwift: hex #90A4AE 814 | public static let blueGrey300 = Color(hex: 0x90A4AE) 815 | 816 | /// SwifterSwift: hex #78909C 817 | public static let blueGrey400 = Color(hex: 0x78909C) 818 | 819 | /// SwifterSwift: hex #607D8B 820 | public static let blueGrey500 = Color(hex: 0x607D8B) 821 | 822 | /// SwifterSwift: hex #546E7A 823 | public static let blueGrey600 = Color(hex: 0x546E7A) 824 | 825 | /// SwifterSwift: hex #455A64 826 | public static let blueGrey700 = Color(hex: 0x455A64) 827 | 828 | /// SwifterSwift: hex #37474F 829 | public static let blueGrey800 = Color(hex: 0x37474F) 830 | 831 | /// SwifterSwift: hex #263238 832 | public static let blueGrey900 = Color(hex: 0x263238) 833 | 834 | /// SwifterSwift: hex #000000 835 | public static let black = Color(hex: 0x000000) 836 | 837 | /// SwifterSwift: hex #FFFFFF 838 | public static let white = Color(hex: 0xFFFFFF) 839 | } 840 | 841 | } 842 | 843 | // MARK: - CSS colors 844 | public extension Color { 845 | 846 | /// SwifterSwift: CSS colors. 847 | struct CSS { 848 | // http://www.w3schools.com/colors/colors_names.asp 849 | 850 | private init() {} 851 | 852 | /// SwifterSwift: hex #F0F8FF 853 | public static let aliceBlue = Color(hex: 0xF0F8FF) 854 | 855 | /// SwifterSwift: hex #FAEBD7 856 | public static let antiqueWhite = Color(hex: 0xFAEBD7) 857 | 858 | /// SwifterSwift: hex #00FFFF 859 | public static let aqua = Color(hex: 0x00FFFF) 860 | 861 | /// SwifterSwift: hex #7FFFD4 862 | public static let aquamarine = Color(hex: 0x7FFFD4) 863 | 864 | /// SwifterSwift: hex #F0FFFF 865 | public static let azure = Color(hex: 0xF0FFFF) 866 | 867 | /// SwifterSwift: hex #F5F5DC 868 | public static let beige = Color(hex: 0xF5F5DC) 869 | 870 | /// SwifterSwift: hex #FFE4C4 871 | public static let bisque = Color(hex: 0xFFE4C4) 872 | 873 | /// SwifterSwift: hex #000000 874 | public static let black = Color(hex: 0x000000) 875 | 876 | /// SwifterSwift: hex #FFEBCD 877 | public static let blanchedAlmond = Color(hex: 0xFFEBCD) 878 | 879 | /// SwifterSwift: hex #0000FF 880 | public static let blue = Color(hex: 0x0000FF) 881 | 882 | /// SwifterSwift: hex #8A2BE2 883 | public static let blueViolet = Color(hex: 0x8A2BE2) 884 | 885 | /// SwifterSwift: hex #A52A2A 886 | public static let brown = Color(hex: 0xA52A2A) 887 | 888 | /// SwifterSwift: hex #DEB887 889 | public static let burlyWood = Color(hex: 0xDEB887) 890 | 891 | /// SwifterSwift: hex #5F9EA0 892 | public static let cadetBlue = Color(hex: 0x5F9EA0) 893 | 894 | /// SwifterSwift: hex #7FFF00 895 | public static let chartreuse = Color(hex: 0x7FFF00) 896 | 897 | /// SwifterSwift: hex #D2691E 898 | public static let chocolate = Color(hex: 0xD2691E) 899 | 900 | /// SwifterSwift: hex #FF7F50 901 | public static let coral = Color(hex: 0xFF7F50) 902 | 903 | /// SwifterSwift: hex #6495ED 904 | public static let cornflowerBlue = Color(hex: 0x6495ED) 905 | 906 | /// SwifterSwift: hex #FFF8DC 907 | public static let cornsilk = Color(hex: 0xFFF8DC) 908 | 909 | /// SwifterSwift: hex #DC143C 910 | public static let crimson = Color(hex: 0xDC143C) 911 | 912 | /// SwifterSwift: hex #00FFFF 913 | public static let cyan = Color(hex: 0x00FFFF) 914 | 915 | /// SwifterSwift: hex #00008B 916 | public static let darkBlue = Color(hex: 0x00008B) 917 | 918 | /// SwifterSwift: hex #008B8B 919 | public static let darkCyan = Color(hex: 0x008B8B) 920 | 921 | /// SwifterSwift: hex #B8860B 922 | public static let darkGoldenRod = Color(hex: 0xB8860B) 923 | 924 | /// SwifterSwift: hex #A9A9A9 925 | public static let darkGray = Color(hex: 0xA9A9A9) 926 | 927 | /// SwifterSwift: hex #A9A9A9 928 | public static let darkGrey = Color(hex: 0xA9A9A9) 929 | 930 | /// SwifterSwift: hex #006400 931 | public static let darkGreen = Color(hex: 0x006400) 932 | 933 | /// SwifterSwift: hex #BDB76B 934 | public static let darkKhaki = Color(hex: 0xBDB76B) 935 | 936 | /// SwifterSwift: hex #8B008B 937 | public static let darkMagenta = Color(hex: 0x8B008B) 938 | 939 | /// SwifterSwift: hex #556B2F 940 | public static let darkOliveGreen = Color(hex: 0x556B2F) 941 | 942 | /// SwifterSwift: hex #FF8C00 943 | public static let darkOrange = Color(hex: 0xFF8C00) 944 | 945 | /// SwifterSwift: hex #9932CC 946 | public static let darkOrchid = Color(hex: 0x9932CC) 947 | 948 | /// SwifterSwift: hex #8B0000 949 | public static let darkRed = Color(hex: 0x8B0000) 950 | 951 | /// SwifterSwift: hex #E9967A 952 | public static let darkSalmon = Color(hex: 0xE9967A) 953 | 954 | /// SwifterSwift: hex #8FBC8F 955 | public static let darkSeaGreen = Color(hex: 0x8FBC8F) 956 | 957 | /// SwifterSwift: hex #483D8B 958 | public static let darkSlateBlue = Color(hex: 0x483D8B) 959 | 960 | /// SwifterSwift: hex #2F4F4F 961 | public static let darkSlateGray = Color(hex: 0x2F4F4F) 962 | 963 | /// SwifterSwift: hex #2F4F4F 964 | public static let darkSlateGrey = Color(hex: 0x2F4F4F) 965 | 966 | /// SwifterSwift: hex #00CED1 967 | public static let darkTurquoise = Color(hex: 0x00CED1) 968 | 969 | /// SwifterSwift: hex #9400D3 970 | public static let darkViolet = Color(hex: 0x9400D3) 971 | 972 | /// SwifterSwift: hex #FF1493 973 | public static let deepPink = Color(hex: 0xFF1493) 974 | 975 | /// SwifterSwift: hex #00BFFF 976 | public static let deepSkyBlue = Color(hex: 0x00BFFF) 977 | 978 | /// SwifterSwift: hex #696969 979 | public static let dimGray = Color(hex: 0x696969) 980 | 981 | /// SwifterSwift: hex #696969 982 | public static let dimGrey = Color(hex: 0x696969) 983 | 984 | /// SwifterSwift: hex #1E90FF 985 | public static let dodgerBlue = Color(hex: 0x1E90FF) 986 | 987 | /// SwifterSwift: hex #B22222 988 | public static let fireBrick = Color(hex: 0xB22222) 989 | 990 | /// SwifterSwift: hex #FFFAF0 991 | public static let floralWhite = Color(hex: 0xFFFAF0) 992 | 993 | /// SwifterSwift: hex #228B22 994 | public static let forestGreen = Color(hex: 0x228B22) 995 | 996 | /// SwifterSwift: hex #FF00FF 997 | public static let fuchsia = Color(hex: 0xFF00FF) 998 | 999 | /// SwifterSwift: hex #DCDCDC 1000 | public static let gainsboro = Color(hex: 0xDCDCDC) 1001 | 1002 | /// SwifterSwift: hex #F8F8FF 1003 | public static let ghostWhite = Color(hex: 0xF8F8FF) 1004 | 1005 | /// SwifterSwift: hex #FFD700 1006 | public static let gold = Color(hex: 0xFFD700) 1007 | 1008 | /// SwifterSwift: hex #DAA520 1009 | public static let goldenRod = Color(hex: 0xDAA520) 1010 | 1011 | /// SwifterSwift: hex #808080 1012 | public static let gray = Color(hex: 0x808080) 1013 | 1014 | /// SwifterSwift: hex #808080 1015 | public static let grey = Color(hex: 0x808080) 1016 | 1017 | /// SwifterSwift: hex #008000 1018 | public static let green = Color(hex: 0x008000) 1019 | 1020 | /// SwifterSwift: hex #ADFF2F 1021 | public static let greenYellow = Color(hex: 0xADFF2F) 1022 | 1023 | /// SwifterSwift: hex #F0FFF0 1024 | public static let honeyDew = Color(hex: 0xF0FFF0) 1025 | 1026 | /// SwifterSwift: hex #FF69B4 1027 | public static let hotPink = Color(hex: 0xFF69B4) 1028 | 1029 | /// SwifterSwift: hex #CD5C5C 1030 | public static let indianRed = Color(hex: 0xCD5C5C) 1031 | 1032 | /// SwifterSwift: hex #4B0082 1033 | public static let indigo = Color(hex: 0x4B0082) 1034 | 1035 | /// SwifterSwift: hex #FFFFF0 1036 | public static let ivory = Color(hex: 0xFFFFF0) 1037 | 1038 | /// SwifterSwift: hex #F0E68C 1039 | public static let khaki = Color(hex: 0xF0E68C) 1040 | 1041 | /// SwifterSwift: hex #E6E6FA 1042 | public static let lavender = Color(hex: 0xE6E6FA) 1043 | 1044 | /// SwifterSwift: hex #FFF0F5 1045 | public static let lavenderBlush = Color(hex: 0xFFF0F5) 1046 | 1047 | /// SwifterSwift: hex #7CFC00 1048 | public static let lawnGreen = Color(hex: 0x7CFC00) 1049 | 1050 | /// SwifterSwift: hex #FFFACD 1051 | public static let lemonChiffon = Color(hex: 0xFFFACD) 1052 | 1053 | /// SwifterSwift: hex #ADD8E6 1054 | public static let lightBlue = Color(hex: 0xADD8E6) 1055 | 1056 | /// SwifterSwift: hex #F08080 1057 | public static let lightCoral = Color(hex: 0xF08080) 1058 | 1059 | /// SwifterSwift: hex #E0FFFF 1060 | public static let lightCyan = Color(hex: 0xE0FFFF) 1061 | 1062 | /// SwifterSwift: hex #FAFAD2 1063 | public static let lightGoldenRodYellow = Color(hex: 0xFAFAD2) 1064 | 1065 | /// SwifterSwift: hex #D3D3D3 1066 | public static let lightGray = Color(hex: 0xD3D3D3) 1067 | 1068 | /// SwifterSwift: hex #D3D3D3 1069 | public static let lightGrey = Color(hex: 0xD3D3D3) 1070 | 1071 | /// SwifterSwift: hex #90EE90 1072 | public static let lightGreen = Color(hex: 0x90EE90) 1073 | 1074 | /// SwifterSwift: hex #FFB6C1 1075 | public static let lightPink = Color(hex: 0xFFB6C1) 1076 | 1077 | /// SwifterSwift: hex #FFA07A 1078 | public static let lightSalmon = Color(hex: 0xFFA07A) 1079 | 1080 | /// SwifterSwift: hex #20B2AA 1081 | public static let lightSeaGreen = Color(hex: 0x20B2AA) 1082 | 1083 | /// SwifterSwift: hex #87CEFA 1084 | public static let lightSkyBlue = Color(hex: 0x87CEFA) 1085 | 1086 | /// SwifterSwift: hex #778899 1087 | public static let lightSlateGray = Color(hex: 0x778899) 1088 | 1089 | /// SwifterSwift: hex #778899 1090 | public static let lightSlateGrey = Color(hex: 0x778899) 1091 | 1092 | /// SwifterSwift: hex #B0C4DE 1093 | public static let lightSteelBlue = Color(hex: 0xB0C4DE) 1094 | 1095 | /// SwifterSwift: hex #FFFFE0 1096 | public static let lightYellow = Color(hex: 0xFFFFE0) 1097 | 1098 | /// SwifterSwift: hex #00FF00 1099 | public static let lime = Color(hex: 0x00FF00) 1100 | 1101 | /// SwifterSwift: hex #32CD32 1102 | public static let limeGreen = Color(hex: 0x32CD32) 1103 | 1104 | /// SwifterSwift: hex #FAF0E6 1105 | public static let linen = Color(hex: 0xFAF0E6) 1106 | 1107 | /// SwifterSwift: hex #FF00FF 1108 | public static let magenta = Color(hex: 0xFF00FF) 1109 | 1110 | /// SwifterSwift: hex #800000 1111 | public static let maroon = Color(hex: 0x800000) 1112 | 1113 | /// SwifterSwift: hex #66CDAA 1114 | public static let mediumAquaMarine = Color(hex: 0x66CDAA) 1115 | 1116 | /// SwifterSwift: hex #0000CD 1117 | public static let mediumBlue = Color(hex: 0x0000CD) 1118 | 1119 | /// SwifterSwift: hex #BA55D3 1120 | public static let mediumOrchid = Color(hex: 0xBA55D3) 1121 | 1122 | /// SwifterSwift: hex #9370DB 1123 | public static let mediumPurple = Color(hex: 0x9370DB) 1124 | 1125 | /// SwifterSwift: hex #3CB371 1126 | public static let mediumSeaGreen = Color(hex: 0x3CB371) 1127 | 1128 | /// SwifterSwift: hex #7B68EE 1129 | public static let mediumSlateBlue = Color(hex: 0x7B68EE) 1130 | 1131 | /// SwifterSwift: hex #00FA9A 1132 | public static let mediumSpringGreen = Color(hex: 0x00FA9A) 1133 | 1134 | /// SwifterSwift: hex #48D1CC 1135 | public static let mediumTurquoise = Color(hex: 0x48D1CC) 1136 | 1137 | /// SwifterSwift: hex #C71585 1138 | public static let mediumVioletRed = Color(hex: 0xC71585) 1139 | 1140 | /// SwifterSwift: hex #191970 1141 | public static let midnightBlue = Color(hex: 0x191970) 1142 | 1143 | /// SwifterSwift: hex #F5FFFA 1144 | public static let mintCream = Color(hex: 0xF5FFFA) 1145 | 1146 | /// SwifterSwift: hex #FFE4E1 1147 | public static let mistyRose = Color(hex: 0xFFE4E1) 1148 | 1149 | /// SwifterSwift: hex #FFE4B5 1150 | public static let moccasin = Color(hex: 0xFFE4B5) 1151 | 1152 | /// SwifterSwift: hex #FFDEAD 1153 | public static let navajoWhite = Color(hex: 0xFFDEAD) 1154 | 1155 | /// SwifterSwift: hex #000080 1156 | public static let navy = Color(hex: 0x000080) 1157 | 1158 | /// SwifterSwift: hex #FDF5E6 1159 | public static let oldLace = Color(hex: 0xFDF5E6) 1160 | 1161 | /// SwifterSwift: hex #808000 1162 | public static let olive = Color(hex: 0x808000) 1163 | 1164 | /// SwifterSwift: hex #6B8E23 1165 | public static let oliveDrab = Color(hex: 0x6B8E23) 1166 | 1167 | /// SwifterSwift: hex #FFA500 1168 | public static let orange = Color(hex: 0xFFA500) 1169 | 1170 | /// SwifterSwift: hex #FF4500 1171 | public static let orangeRed = Color(hex: 0xFF4500) 1172 | 1173 | /// SwifterSwift: hex #DA70D6 1174 | public static let orchid = Color(hex: 0xDA70D6) 1175 | 1176 | /// SwifterSwift: hex #EEE8AA 1177 | public static let paleGoldenRod = Color(hex: 0xEEE8AA) 1178 | 1179 | /// SwifterSwift: hex #98FB98 1180 | public static let paleGreen = Color(hex: 0x98FB98) 1181 | 1182 | /// SwifterSwift: hex #AFEEEE 1183 | public static let paleTurquoise = Color(hex: 0xAFEEEE) 1184 | 1185 | /// SwifterSwift: hex #DB7093 1186 | public static let paleVioletRed = Color(hex: 0xDB7093) 1187 | 1188 | /// SwifterSwift: hex #FFEFD5 1189 | public static let papayaWhip = Color(hex: 0xFFEFD5) 1190 | 1191 | /// SwifterSwift: hex #FFDAB9 1192 | public static let peachPuff = Color(hex: 0xFFDAB9) 1193 | 1194 | /// SwifterSwift: hex #CD853F 1195 | public static let peru = Color(hex: 0xCD853F) 1196 | 1197 | /// SwifterSwift: hex #FFC0CB 1198 | public static let pink = Color(hex: 0xFFC0CB) 1199 | 1200 | /// SwifterSwift: hex #DDA0DD 1201 | public static let plum = Color(hex: 0xDDA0DD) 1202 | 1203 | /// SwifterSwift: hex #B0E0E6 1204 | public static let powderBlue = Color(hex: 0xB0E0E6) 1205 | 1206 | /// SwifterSwift: hex #800080 1207 | public static let purple = Color(hex: 0x800080) 1208 | 1209 | /// SwifterSwift: hex #663399 1210 | public static let rebeccaPurple = Color(hex: 0x663399) 1211 | 1212 | /// SwifterSwift: hex #FF0000 1213 | public static let red = Color(hex: 0xFF0000) 1214 | 1215 | /// SwifterSwift: hex #BC8F8F 1216 | public static let rosyBrown = Color(hex: 0xBC8F8F) 1217 | 1218 | /// SwifterSwift: hex #4169E1 1219 | public static let royalBlue = Color(hex: 0x4169E1) 1220 | 1221 | /// SwifterSwift: hex #8B4513 1222 | public static let saddleBrown = Color(hex: 0x8B4513) 1223 | 1224 | /// SwifterSwift: hex #FA8072 1225 | public static let salmon = Color(hex: 0xFA8072) 1226 | 1227 | /// SwifterSwift: hex #F4A460 1228 | public static let sandyBrown = Color(hex: 0xF4A460) 1229 | 1230 | /// SwifterSwift: hex #2E8B57 1231 | public static let seaGreen = Color(hex: 0x2E8B57) 1232 | 1233 | /// SwifterSwift: hex #FFF5EE 1234 | public static let seaShell = Color(hex: 0xFFF5EE) 1235 | 1236 | /// SwifterSwift: hex #A0522D 1237 | public static let sienna = Color(hex: 0xA0522D) 1238 | 1239 | /// SwifterSwift: hex #C0C0C0 1240 | public static let silver = Color(hex: 0xC0C0C0) 1241 | 1242 | /// SwifterSwift: hex #87CEEB 1243 | public static let skyBlue = Color(hex: 0x87CEEB) 1244 | 1245 | /// SwifterSwift: hex #6A5ACD 1246 | public static let slateBlue = Color(hex: 0x6A5ACD) 1247 | 1248 | /// SwifterSwift: hex #708090 1249 | public static let slateGray = Color(hex: 0x708090) 1250 | 1251 | /// SwifterSwift: hex #708090 1252 | public static let slateGrey = Color(hex: 0x708090) 1253 | 1254 | /// SwifterSwift: hex #FFFAFA 1255 | public static let snow = Color(hex: 0xFFFAFA) 1256 | 1257 | /// SwifterSwift: hex #00FF7F 1258 | public static let springGreen = Color(hex: 0x00FF7F) 1259 | 1260 | /// SwifterSwift: hex #4682B4 1261 | public static let steelBlue = Color(hex: 0x4682B4) 1262 | 1263 | /// SwifterSwift: hex #D2B48C 1264 | public static let tan = Color(hex: 0xD2B48C) 1265 | 1266 | /// SwifterSwift: hex #008080 1267 | public static let teal = Color(hex: 0x008080) 1268 | 1269 | /// SwifterSwift: hex #D8BFD8 1270 | public static let thistle = Color(hex: 0xD8BFD8) 1271 | 1272 | /// SwifterSwift: hex #FF6347 1273 | public static let tomato = Color(hex: 0xFF6347) 1274 | 1275 | /// SwifterSwift: hex #40E0D0 1276 | public static let turquoise = Color(hex: 0x40E0D0) 1277 | 1278 | /// SwifterSwift: hex #EE82EE 1279 | public static let violet = Color(hex: 0xEE82EE) 1280 | 1281 | /// SwifterSwift: hex #F5DEB3 1282 | public static let wheat = Color(hex: 0xF5DEB3) 1283 | 1284 | /// SwifterSwift: hex #FFFFFF 1285 | public static let white = Color(hex: 0xFFFFFF) 1286 | 1287 | /// SwifterSwift: hex #F5F5F5 1288 | public static let whiteSmoke = Color(hex: 0xF5F5F5) 1289 | 1290 | /// SwifterSwift: hex #FFFF00 1291 | public static let yellow = Color(hex: 0xFFFF00) 1292 | 1293 | /// SwifterSwift: hex #9ACD32 1294 | public static let yellowGreen = Color(hex: 0x9ACD32) 1295 | } 1296 | 1297 | } 1298 | -------------------------------------------------------------------------------- /Sources/Rainbow/Utils/Double.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | extension Double { 4 | 5 | func clamp(min: Double, max: Double) -> Double { 6 | if self < min { return min } 7 | if self > max { return max } 8 | return self 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Sources/Rainbow/Utils/NSRegularExpression.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | extension NSRegularExpression { 4 | 5 | static func match(_ s: String, pattern: String) -> [String] { 6 | guard let regexp = try? NSRegularExpression(pattern: pattern, options: []) else { return [] } 7 | guard let matches = regexp.matches(in: s, options: [], range: NSRange(location: 0, length: s.count)).first else { return [] } 8 | 9 | var captures: [String] = [] 10 | for i in 0.. Bool { 6 | return Swift.max(lhs, rhs) - Swift.min(lhs, rhs) < 0.0001 7 | } 8 | 9 | private func ~~(lhs: Int, rhs: Int) -> Bool { 10 | return Swift.max(lhs, rhs) - Swift.min(lhs, rhs) <= 1 11 | } 12 | 13 | final class RainbowTests: XCTestCase { 14 | 15 | func testRGBAColor() { 16 | let color = Color(red: 0, green: 1, blue: 255, alpha: 0.3) 17 | let rgba = color.rgba 18 | XCTAssert(rgba.red ~~ 0) 19 | XCTAssert(rgba.green ~~ 1) 20 | XCTAssert(rgba.blue ~~ 255) 21 | XCTAssert(rgba.alpha ~~ 0.3) 22 | 23 | XCTAssertEqual(Color(rgba: "rgba(0, 1, 255, 0.3)"), color) 24 | XCTAssertEqual(Color.rgb(0, 1, 255, 0.3), color) 25 | } 26 | 27 | func testHSLAColor() { 28 | let color = Color(hue: 96, saturation: 48, lightness: 59, alpha: 0.3) 29 | let hsla = color.hsla 30 | XCTAssert(hsla.hue ~~ 96) 31 | XCTAssert(hsla.saturation ~~ 48) 32 | XCTAssert(hsla.lightness ~~ 59) 33 | XCTAssert(hsla.alpha ~~ 0.3) 34 | 35 | let rgba = color.rgba 36 | XCTAssert(rgba.red ~~ 140) 37 | XCTAssert(rgba.green ~~ 201) 38 | XCTAssert(rgba.blue ~~ 100) 39 | XCTAssert(rgba.alpha ~~ 0.3) 40 | 41 | XCTAssertEqual(Color(hsla: "hsl(96, 48%, 59%, 0.3)"), color) 42 | XCTAssertEqual(Color.hsl(96, 48, 59, 0.3), color) 43 | } 44 | 45 | func testHSVAColor() { 46 | let color = Color(hue: 96, saturation: 50, value: 78, alpha: 0.3) 47 | let hsva = color.hsva 48 | XCTAssert(hsva.hue ~~ 96) 49 | XCTAssert(hsva.saturation ~~ 50) 50 | XCTAssert(hsva.value ~~ 78) 51 | XCTAssert(hsva.alpha ~~ 0.3) 52 | 53 | let rgba = color.rgba 54 | XCTAssert(rgba.red ~~ 139) 55 | XCTAssert(rgba.green ~~ 199) 56 | XCTAssert(rgba.blue ~~ 99) 57 | XCTAssert(rgba.alpha ~~ 0.3) 58 | 59 | XCTAssertEqual(color, Color(hsva: "hsv(96, 50%, 78%, 0.3)")) 60 | XCTAssertEqual(color, Color.hsv(96, 50, 78, 0.3)) 61 | } 62 | 63 | func testConversionEdgeCases() { 64 | let rgbInit = Color(red: 255, green: 255, blue: 255) // edge case: what hue does white/grey/black have? --> define as 0 65 | let hsla = rgbInit.hsla // this shouldn't fail 66 | XCTAssertEqual(hsla.hue, 0) 67 | XCTAssertEqual(hsla.saturation, 0) 68 | XCTAssertEqual(hsla.lightness, 100) 69 | } 70 | 71 | func testHexColor() { 72 | let color1 = Color(hex: "abcf") 73 | let color2 = Color(hex: "#AABBCC") 74 | XCTAssertEqual(color1, color2) 75 | 76 | let color3 = Color(red: 170, green: 187, blue: 204) 77 | let color4 = Color(hex: "abc") 78 | XCTAssertEqual(color3, color4!) 79 | 80 | let color5 = Color(hex: "hello") 81 | XCTAssertNil(color5) 82 | 83 | XCTAssertEqual(Color.hex(0x0366D6), Color.hex("#0366D6")) 84 | } 85 | 86 | func testANSI() { 87 | let color1 = Color(red: 170, green: 0, blue: 0) 88 | XCTAssertEqual(color1.ansi16, 31) 89 | 90 | let color2 = Color(red: 85, green: 85, blue: 255) 91 | XCTAssertEqual(color2.ansi16, 94) 92 | 93 | let color3 = Color(hex: "d700ff") 94 | XCTAssertEqual(color3?.ansi256, 165) 95 | 96 | let color4 = Color(hex: "999") 97 | XCTAssertEqual(color4?.ansi256, 246) 98 | } 99 | 100 | func testDarkLight() { 101 | let color1 = Color.hex("eee") 102 | XCTAssertTrue(color1!.isLight) 103 | XCTAssertFalse(color1!.isDark) 104 | 105 | let color2 = Color.hex("fff") 106 | XCTAssertTrue(color2!.isWhite) 107 | XCTAssertFalse(color2!.isBlack) 108 | } 109 | 110 | func testNegate() { 111 | let color = Color.random 112 | XCTAssertEqual(color.negate.negate, color) 113 | } 114 | 115 | func testCocoa() { 116 | let c = Color.random 117 | #if canImport(AppKit) 118 | XCTAssertEqual(Color.fromNSColor(c.toNSColor()), c) 119 | #endif 120 | 121 | #if canImport(UIKit) 122 | XCTAssertEqual(Color.fromUIColor(c.toUIColor()), c) 123 | #endif 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /Tests/RainbowTests/XCTestManifests.swift: -------------------------------------------------------------------------------- 1 | #if !canImport(ObjectiveC) 2 | import XCTest 3 | 4 | extension RainbowTests { 5 | // DO NOT MODIFY: This is autogenerated, use: 6 | // `swift test --generate-linuxmain` 7 | // to regenerate. 8 | static let __allTests__RainbowTests = [ 9 | ("testANSI", testANSI), 10 | ("testCocoa", testCocoa), 11 | ("testConversionEdgeCases", testConversionEdgeCases), 12 | ("testDarkLight", testDarkLight), 13 | ("testHexColor", testHexColor), 14 | ("testHSLAColor", testHSLAColor), 15 | ("testHSVAColor", testHSVAColor), 16 | ("testNegate", testNegate), 17 | ("testRGBAColor", testRGBAColor), 18 | ] 19 | } 20 | 21 | public func __allTests() -> [XCTestCaseEntry] { 22 | return [ 23 | testCase(RainbowTests.__allTests__RainbowTests), 24 | ] 25 | } 26 | #endif 27 | --------------------------------------------------------------------------------