Created
October 7, 2017 08:04
-
-
Save nrlnishan/c482dc8ec47d6c1534ef3df39291951f to your computer and use it in GitHub Desktop.
UIColor Extensions
This file contains hidden or 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
| class func from(r:CGFloat,g:CGFloat,b:CGFloat,a:CGFloat) -> UIColor { | |
| return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a) | |
| } | |
| // Hex should be in format #bcbcbc | |
| class func from (hex:String) -> UIColor { | |
| var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased() | |
| if (cString.hasPrefix("#")) { | |
| cString.remove(at: cString.startIndex) | |
| } | |
| if ((cString.characters.count) != 6) { | |
| return UIColor.gray | |
| } | |
| var rgbValue:UInt32 = 0 | |
| Scanner(string: cString).scanHexInt32(&rgbValue) | |
| return UIColor( | |
| red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, | |
| green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0, | |
| blue: CGFloat(rgbValue & 0x0000FF) / 255.0, | |
| alpha: CGFloat(1.0) | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment