Skip to content

Instantly share code, notes, and snippets.

@bastianh
Created June 7, 2014 10:55
Show Gist options
  • Save bastianh/6af74ef5edc06952aa11 to your computer and use it in GitHub Desktop.
Save bastianh/6af74ef5edc06952aa11 to your computer and use it in GitHub Desktop.
//
// 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