Created
September 5, 2017 16:31
-
-
Save darrarski/43584cc4e9d049506fc6587b28175868 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIColor { | |
convenience init?(hexRed red: Int, green: Int, blue: Int, alpha: Int = 255) { | |
guard red >= 0 && red <= 255 else { return nil } | |
guard green >= 0 && green <= 255 else { return nil } | |
guard blue >= 0 && blue <= 255 else { return nil } | |
self.init(red: CGFloat(red) / 255.0, | |
green: CGFloat(green) / 255.0, | |
blue: CGFloat(blue) / 255.0, | |
alpha: CGFloat(alpha) / 255.0) | |
} | |
convenience init?(hexString: String) { | |
let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted) | |
var int = UInt32() | |
Scanner(string: hex).scanHexInt32(&int) | |
let red, green, blue, alpha: UInt32 | |
switch hex.characters.count { | |
case 3: // RGB (12-bit) | |
(alpha, red, green, blue) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17) | |
case 6: // RRGGBB (24-bit) | |
(alpha, red, green, blue) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF) | |
case 8: // AARRGGBB (32-bit) | |
(alpha, red, green, blue) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF) | |
default: | |
return nil | |
} | |
self.init(hexRed: Int(red), green: Int(green), blue: Int(blue), alpha: Int(alpha)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment