Skip to content

Instantly share code, notes, and snippets.

@negibouze
Created January 20, 2015 07:49
Show Gist options
  • Save negibouze/ea2a0d9da63b4c1f4790 to your computer and use it in GitHub Desktop.
Save negibouze/ea2a0d9da63b4c1f4790 to your computer and use it in GitHub Desktop.
UIColor with RGB
// Pattern A func
func rgb(red r: CGFloat, green g: CGFloat, blue b: CGFloat) -> UIColor {
return rgba(red: r, green: g, blue: b, alpha: 1.0)
}
func rgba(red r: CGFloat, green g: CGFloat, blue b: CGFloat, alpha a: CGFloat) -> UIColor {
return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
}
// Pattern B Extension
extension UIColor {
convenience init(r: CGFloat, g: CGFloat, b: CGFloat) {
self.init(r: r, g: g, b: b, a: 1.0)
}
convenience init(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) {
self.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment