Skip to content

Instantly share code, notes, and snippets.

@chrishulbert
Created February 23, 2015 22:42

Revisions

  1. chrishulbert created this gist Feb 23, 2015.
    18 changes: 18 additions & 0 deletions gistfile1.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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)
    }

    }