Skip to content

Instantly share code, notes, and snippets.

@enefry
Forked from asarode/generateRandomColor.swift
Created December 29, 2017 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enefry/350cd317c9fc3a572cceaa3d75db123d to your computer and use it in GitHub Desktop.
Save enefry/350cd317c9fc3a572cceaa3d75db123d to your computer and use it in GitHub Desktop.
Generating random UIColor in Swift
func generateRandomColor() -> UIColor {
let hue : CGFloat = CGFloat(arc4random() % 256) / 256 // use 256 to get full range from 0.0 to 1.0
let saturation : CGFloat = CGFloat(arc4random() % 128) / 256 + 0.5 // from 0.5 to 1.0 to stay away from white
let brightness : CGFloat = CGFloat(arc4random() % 128) / 256 + 0.5 // from 0.5 to 1.0 to stay away from black
return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment