Created
July 13, 2015 21:42
-
-
Save naturaln0va/ba59f3925855cb4f7629 to your computer and use it in GitHub Desktop.
UIColor hex value extension in Swift.
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 | |
{ | |
func hexString() -> String? | |
{ | |
var redValue: CGFloat = 0 | |
var greenValue: CGFloat = 0 | |
var blueValue: CGFloat = 0 | |
var alphaValue: CGFloat = 0 | |
if self.getRed(&redValue, green: &greenValue, blue: &blueValue, alpha: &alphaValue) { | |
let r = Int(redValue * 255.0) | |
let g = Int(greenValue * 255.0) | |
let b = Int(blueValue * 255.0) | |
return "#"+String(format: "%02X", Int(r))+String(format: "%02X", Int(g))+String(format: "%02X", Int(b)) | |
} else { | |
return nil | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment