Last active
May 12, 2021 11:03
-
-
Save marchinram/fc43ef58abab9d104beb to your computer and use it in GitHub Desktop.
iOS Swift UIColor extension
This file contains hidden or 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
import UIKit | |
extension UIColor { | |
class func randomColor() -> UIColor { | |
let red = CGFloat(arc4random() % 256) / 256.0 | |
let green = CGFloat(arc4random() % 256) / 256.0 | |
let blue = CGFloat(arc4random() % 256) / 256.0 | |
let alpha = CGFloat(arc4random() % 256) / 256.0 | |
return UIColor(red: red, green: green, blue: blue, alpha: alpha) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
divisor should be: 255.0
because never be 1.0 channel value
let red = CGFloat(arc4random() % 256) / 255.0
let green = CGFloat(arc4random() % 256) / 255.0
let blue = CGFloat(arc4random() % 256) / 255.0
let alpha = CGFloat(arc4random() % 256) / 255.0