Guest User

Untitled

a guest
Apr 26th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. extension UIColor {
  2. convenience init(hexString: String) {
  3. let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
  4. var int = UInt32()
  5. Scanner(string: hex).scanHexInt32(&int)
  6. let a, r, g, b: UInt32
  7. switch hex.count {
  8. case 3: // RGB (12-bit)
  9. (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
  10. case 6: // RGB (24-bit)
  11. (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
  12. case 8: // ARGB (32-bit)
  13. (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
  14. default:
  15. (a, r, g, b) = (255, 0, 0, 0)
  16. }
  17. self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
  18. }
  19. }
Add Comment
Please, Sign In to add comment