Created
March 22, 2019 11:26
-
-
Save fitomad/a436916584d93de8a382a918602749db 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 UIColor | |
import Foundation | |
extension UIColor | |
{ | |
/** | |
Crea un nuevo `UIColor` basado en una cadena hexadecimal | |
*/ | |
public convenience init(hexadecimal: String, alpha: CGFloat = 1.0) | |
{ | |
var mutableHex = hexadecimal | |
if(mutableHex.hasPrefix("#")) | |
{ | |
mutableHex.remove(at: mutableHex.startIndex) | |
} | |
var hexInt: CUnsignedLongLong = 0 | |
let scanner: Scanner = Scanner(string:mutableHex) | |
scanner.scanHexInt64(&hexInt) | |
let red: CGFloat = CGFloat((hexInt & 0xFF0000) >> 16) / 255.0 | |
let green: CGFloat = CGFloat((hexInt & 0x00FF00) >> 8) / 255.0 | |
let blue: CGFloat = CGFloat(hexInt & 0x0000FF) / 255.0 | |
self.init(red:red, green:green, blue:blue, alpha:alpha); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment