Skip to content

Instantly share code, notes, and snippets.

@qdequele
Created June 10, 2015 06:45
Show Gist options
  • Save qdequele/08be34a0d6a20f1c9975 to your computer and use it in GitHub Desktop.
Save qdequele/08be34a0d6a20f1c9975 to your computer and use it in GitHub Desktop.
How to user hexadecimal UIColor in swift
//
// UIColor.swift
//
// Created by Quentin De Quelen on 09/06/15.
//
import UIKit
extension UIColor {
convenience init(red: Int, green: Int, blue: Int) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}
convenience init(netHex:Int) {
self.init(red:(netHex >> 16) & 0xff, green:(netHex >> 8) & 0xff, blue:netHex & 0xff)
}
}
//How to use it, just call it like that :
UIColor(netHex:0x33C9DE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment