Created
April 29, 2017 19:20
-
-
Save keyurgolani/0b98d2010d98ba13708c5d20eba9359d to your computer and use it in GitHub Desktop.
Extends UIColor in Swift to init a UIColor from given Hex Value or given RGB value with fixed 1.0 alpha value.
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
extension UIColor { | |
convenience init(red: Int, green: Int, blue: Int) { | |
assert(red >= 0 && red <= 255, "Invalid red value") | |
assert(green >= 0 && green <= 255, "Invalid green value") | |
assert(blue >= 0 && blue <= 255, "Invalid blue value") | |
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0) | |
} | |
convenience init(rgb: Int) { | |
self.init( | |
red: (rgb >> 16) & 0xFF, | |
green: (rgb >> 8) & 0xFF, | |
blue: rgb & 0xFF | |
) | |
} | |
} | |
var color1 = UIColor(rgb: 0x3d017a) | |
var color2 = UIColor(rgb: 0xb22e44).withAlphaComponent(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment