Skip to content

Instantly share code, notes, and snippets.

@katopz
Created December 20, 2014 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katopz/ac8567e79b37b877f9f8 to your computer and use it in GitHub Desktop.
Save katopz/ac8567e79b37b877f9f8 to your computer and use it in GitHub Desktop.
RGB Hex to UIColor
//
// DBKUIColor.swift
// DBKUIColor
//
// Created by Todsaporn Banjerdkit (katopz) on 12/19/14.
// Copyright (c) 2014 Debokeh. All rights reserved.
//
// Credit : http://stackoverflow.com/questions/24263007/how-to-use-hex-colour-values-in-swift-ios/24263296#24263296
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(rgbHex: Int) {
self.init(red: (rgbHex >> 16) & 0xff, green: (rgbHex >> 8) & 0xff, blue: rgbHex & 0xff)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment