Created
May 3, 2019 08:08
-
-
Save noa4021J/acb0257d0f2f85313540f61b2690842c to your computer and use it in GitHub Desktop.
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
//HEX値でUIColorを指定できるようにする | |
extension UIColor { | |
convenience init(hexcode: String, alpha: CGFloat) { | |
let string = hexcode.replacingOccurrences(of: "#", with: "") | |
let char = string.map{ String($0) } | |
let r = CGFloat(Int(char[0]+char[1], radix:16) ?? 0) / 255.0 | |
let g = CGFloat(Int(char[2]+char[3], radix:16) ?? 0) / 255.0 | |
let b = CGFloat(Int(char[4]+char[5], radix:16) ?? 0) / 255.0 | |
self.init(red: r, green: g, blue: b, alpha: min(max(alpha,0),1)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment