Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// ColorExtension.swift
//
// Created by Bastian Hoyer on 07.06.14.
// Copyright (c) 2014 Bastian Hoyer. All rights reserved.
//
import UIKit
func int2rgb(rgbValue: CUnsignedInt) -> (CGFloat, CGFloat, CGFloat) {
return (
CGFloat((rgbValue & 0xff0000) >> 16) / 255.0,
CGFloat((rgbValue & 0xFF00) >> 8) / 255.0,
CGFloat(rgbValue & 0xFF) / 255.0
)
}
extension UIColor {
convenience init(hexValue: CUnsignedInt) {
let (r,g,b) = int2rgb(hexValue)
self.init(red: r, green: g, blue: b, alpha: 1.0)
}
convenience init(hexString: NSString) {
var rgbValue : CUnsignedInt = 0
NSScanner(string: hexString).scanHexInt(&rgbValue)
let (r,g,b) = int2rgb(rgbValue)
self.init(red: r, green: g, blue: b, alpha: 1.0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment