Swift UIColor extensions

extension UIColor {
    convenience init?(rgb: Int) {
        self.init(
            red:    CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
            green:  CGFloat((rgb & 0xFF00) >> 8) / 255.0,
            blue:   CGFloat(rgb & 0xFF) / 255.0,
            alpha:  1.0)
    }

    convenience init?(rgba: Int) {
        self.init(
            red:    CGFloat((rgba & 0xFF000000) >> 32) / 255.0,
            green:  CGFloat((rgba & 0xFF0000) >> 16) / 255.0,
            blue:   CGFloat((rgba & 0xFF00) >> 8) / 255.0,
            alpha:  CGFloat(rgba & 0xFF) / 255.0)
    }
}

Usage

var color1 = UIColor(rgba: 0x435363FF)
var color2 = UIColor(rgb: 0x435363)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s