Skip to content

Instantly share code, notes, and snippets.

@rymcol
Last active April 15, 2016 22:56
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 rymcol/45c2a4f5574fa6d4ef311df1b37a68fa to your computer and use it in GitHub Desktop.
Save rymcol/45c2a4f5574fa6d4ef311df1b37a68fa to your computer and use it in GitHub Desktop.
UIColor Extension to Initialize from HEX Color Values
//
// UIColor Extension.swift
// Uses Swift 2.0
//
// Created by Ryan Collins on 9/22/15.
//
//
import Foundation
import UIKit
extension UIColor {
convenience init(fromHex: String) {
var hexDigits = Array(fromHex.characters)
if (hexDigits[0] == "#") {
hexDigits.removeFirst()
}
let red = "\(hexDigits[0])\(hexDigits[1])"
let green = "\(hexDigits[2])\(hexDigits[3])"
let blue = "\(hexDigits[4])\(hexDigits[5])"
let rVal: CGFloat = CGFloat(Int(UInt(red, radix: 16)!))
let gVal: CGFloat = CGFloat(Int(UInt(green, radix: 16)!))
let bVal: CGFloat = CGFloat(Int(UInt(blue, radix: 16)!))
self.init(red: rVal/255, green: gVal/255, blue: bVal/255, alpha: 1.0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment