Last active
October 20, 2016 19:50
-
-
Save dedeexe/e46c68acf38dfe06d0695e7950b0908a to your computer and use it in GitHub Desktop.
UIColor extension for literal numbers
This file contains 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
// | |
// UIColor+Literals.swift | |
// Created by dede.exe | |
// | |
import UIKit | |
public extension UIColor { | |
public convenience init(red:Int, green:Int, blue:Int, percentAlpha:Int = 100) | |
{ | |
let alpha = Float(min(100, max(0,percentAlpha))) / 100 | |
let factor = Float(255.0) | |
let r = Float(max(min(red, 255), 0)) / factor | |
let g = Float(max(min(green, 255), 0)) / factor | |
let b = Float(max(min(blue, 255), 0)) / factor | |
self.init(colorLiteralRed: r, green: g, blue: b, alpha: alpha) | |
} | |
public convenience init(literalColor:Int, alpha:Int = 100) | |
{ | |
let r = (literalColor & 0xFF0000) >> 16 | |
let g = (literalColor & 0x00FF00) >> 8 | |
let b = (literalColor & 0x0000FF) | |
self.init(red:r, green:g, blue:b, percentAlpha : alpha) | |
} | |
} | |
//USAGE | |
let red = UIColor(red: 255, green: 0, blue: 0) | |
let green = UIColor(red: 0, green: 255, blue: 0) | |
let blue = UIColor(red: 0, green: 0, blue: 255) | |
let redHex = UIColor(literalColor: 0xFF0000, alpha: 100) | |
let greenHex = UIColor(literalColor: 0x00FF00, alpha: 100) | |
let blueHex = UIColor(literalColor: 0x0000FF, alpha: 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment