Reputation: 11
I have this little bit of code in which it selects some colors, I would like to know what if I wanted to select a random color between these ones under the code, how could I do that ?
extension UIColor {
static var random: UIColor {
return UIColor(red: .random(in: 0...2), green: .random(in: 0...2), blue: .random(in: 0...2), alpha: 1.0)
}
}
Colors I would like to pick in my extension
Upvotes: 0
Views: 3915
Reputation: 24341
To get a random color from a list of UIColors
, just call randomElement()
on the collection
of UIColor
elements, i.e.
extension UIColor {
static func random(from colors: [UIColor]) -> UIColor? {
return colors.randomElement()
}
}
You can use it like:
let randomColor = UIColor.random(from: [.red, .yellow, .green, .blue, .purple])
I've used the sample colors here. Use your own specific colors that you want to use.
In case you have colors in the form of hex values, you need to add another method in the extension to convert that hex
value into UIColor
instance.
Let me know in case of any issues.
Upvotes: 1
Reputation: 4552
If you want to use only the colours that you added in your question then add your required colour hex string in Array
.
let arrColors = ["ff0000", "228B22", "FFFF00", "800080", "ffa500"] // Use your color hex
Now you need to pick random object from above array like this.
let randomIndex = Int(arc4random_uniform(UInt32(arrColors.count)))
self.vwNavigationBar.backgroundColor = self.hexStringToUIColor(hex: arrColors[randomIndex])
Function to convert Hex string to Color
func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if ((cString.count) != 6) {
return UIColor.gray
}
var rgbValue:UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
Upvotes: 2
Reputation: 353
Try this. It should work.
extension UIColor {
static func random() -> UIColor {
return UIColor.rgb(CGFloat.random(in: 0..<256), green: CGFloat.random(in: 0..<256), blue: CGFloat.random(in: 0..<256))
}
static func rgb(_ red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1)
}
convenience init(red: Int, green: Int, blue: Int) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")
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
)
}
static func getColor() -> UIColor {
let chooseFrom = [0x064545,0x708545,0x45580] //Add your colors here
return UIColor.init(rgb: chooseFrom[Int.random(in: 0..<chooseFrom.count)])
}
}
Upvotes: 1