Last active
April 15, 2016 22:56
-
-
Save rymcol/45c2a4f5574fa6d4ef311df1b37a68fa to your computer and use it in GitHub Desktop.
UIColor Extension to Initialize from HEX Color Values
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
// | |
// 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