Skip to content

Instantly share code, notes, and snippets.

@naturaln0va
Created July 13, 2015 21:42
Show Gist options
  • Save naturaln0va/ba59f3925855cb4f7629 to your computer and use it in GitHub Desktop.
Save naturaln0va/ba59f3925855cb4f7629 to your computer and use it in GitHub Desktop.
UIColor hex value extension in Swift.
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