Skip to content

Instantly share code, notes, and snippets.

@chrishulbert
Created February 23, 2015 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrishulbert/5e4dd1f5dcdf185362b2 to your computer and use it in GitHub Desktop.
Save chrishulbert/5e4dd1f5dcdf185362b2 to your computer and use it in GitHub Desktop.
import UIKit
extension UIColor {
/// Creates a UIColor from an 'rrggbb' hex string.
class func fromHex(hexColour: String) -> UIColor {
let str = hexColour.cStringUsingEncoding(NSUTF8StringEncoding)
let x = strtol(str!, nil, 16)
let r = x >> 16
let g = (x >> 8) & 0xff
let b = x & 0xff
let red = CGFloat(r) / 255.0
let green = CGFloat(g) / 255.0
let blue = CGFloat(b) / 255.0
return UIColor(red: red, green: green, blue: blue, alpha: 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment