-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIColor+Hex.swift
More file actions
26 lines (23 loc) · 808 Bytes
/
UIColor+Hex.swift
File metadata and controls
26 lines (23 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import Foundation
import UIKit
public extension UIColor {
convenience init(hex string: String) {
var hex = string.hasPrefix("#")
? String(string.dropFirst())
: string
guard hex.count == 3 || hex.count == 6
else {
self.init(white: 1.0, alpha: 0.0)
return
}
if hex.count == 3 {
for (index, char) in hex.enumerated() {
hex.insert(char, at: hex.index(hex.startIndex, offsetBy: index * 2))
}
}
self.init(
red: CGFloat((Int(hex, radix: 16)! >> 16) & 0xFF) / 255.0,
green: CGFloat((Int(hex, radix: 16)! >> 8) & 0xFF) / 255.0,
blue: CGFloat((Int(hex, radix: 16)!) & 0xFF) / 255.0, alpha: 1.0)
}
}