Skip to content

Instantly share code, notes, and snippets.

@kiwiznesic
Last active October 6, 2015 11:45
Show Gist options
  • Select an option

  • Save kiwiznesic/fc90c319d8d6b029752f to your computer and use it in GitHub Desktop.

Select an option

Save kiwiznesic/fc90c319d8d6b029752f to your computer and use it in GitHub Desktop.
Get random UIColor with Swift
/*
UIColor class method that returns a random color. All parameters are optional.
*/
import UIKit
extension UIColor{
class func randomColor(hue:CGFloat? = ( CGFloat( CGFloat(arc4random()) % 256 / 256.0 ) ),
saturation:CGFloat? = ( CGFloat( CGFloat(arc4random()) % 128 / 256.0 ) + 0.5 ),
brightness:CGFloat? = ( CGFloat( CGFloat(arc4random()) % 128 / 256.0 ) + 0.5 ),
alpha:CGFloat? = CGFloat(1.0)) -> UIColor{
return UIColor(hue: hue!, saturation: saturation!, brightness: brightness!, alpha: alpha!);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment