Created
June 7, 2014 10:55
-
-
Save bastianh/6af74ef5edc06952aa11 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// 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