patternswiftMinor
Choosing a random flat color
Viewed 0 times
randomflatchoosingcolor
Problem
I have a big array of custom flat colors with perhaps 100 lines:
```
extension UIColor {
class func randomFlatColor() -> UIColor {
let colors = [
UIColor(red: 210/255, green: 77/255, blue: 87/255, alpha: 1.0),
UIColor(red: 217/255, green: 30/255, blue: 24/255, alpha: 1.0),
UIColor(red: 150/255, green: 40/255, blue: 27/255, alpha: 1.0),
UIColor(red: 220/255, green: 198/255, blue: 224/255, alpha: 1.0),
UIColor(red: 103/255, green: 65/255, blue: 114/255, alpha: 1.0),
UIColor(red: 68/255, green: 108/255, blue: 179/255, alpha: 1.0),
UIColor(red: 210/255, green: 77/255, blue: 87/255, alpha: 1.0),
UIColor(red: 228/255, green: 241/255, blue: 254/255, alpha: 1.0),
UIColor(red: 65/255, green: 131/255, blue: 215/255, alpha: 1.0),
UIColor(red: 89/255, green: 171/255, blue: 227/255, alpha: 1.0),
UIColor(red: 129/255, green: 207/255, blue: 224/255, alpha: 1.0),
UIColor(red: 82/255, green: 179/255, blue: 217/255, alpha: 1.0),
UIColor(red: 197/255, green: 239/255, blue: 247/255, alpha: 1.0),
UIColor(red: 34/255, green: 167/255, blue: 240/255, alpha: 1.0),
UIColor(red: 52/255, green: 152/255, blue: 219/255, alpha: 1.0),
UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1.0),
UIColor(red: 25/255, green: 181/255, blue: 254/255, alpha: 1.0),
UIColor(red: 51/255, green: 110/255, blue: 123/255, alpha: 1.0),
UIColor(red: 34/255, green: 49/255, blue: 63/255, alpha: 1.0),
UIColor(red: 30/255, green: 139/255, blue: 195/255, alpha: 1.0),
UIColor(red: 58/255, green: 83/255, blue: 155/255, alpha: 1.0),
UIColor(red: 52/255,
```
extension UIColor {
class func randomFlatColor() -> UIColor {
let colors = [
UIColor(red: 210/255, green: 77/255, blue: 87/255, alpha: 1.0),
UIColor(red: 217/255, green: 30/255, blue: 24/255, alpha: 1.0),
UIColor(red: 150/255, green: 40/255, blue: 27/255, alpha: 1.0),
UIColor(red: 220/255, green: 198/255, blue: 224/255, alpha: 1.0),
UIColor(red: 103/255, green: 65/255, blue: 114/255, alpha: 1.0),
UIColor(red: 68/255, green: 108/255, blue: 179/255, alpha: 1.0),
UIColor(red: 210/255, green: 77/255, blue: 87/255, alpha: 1.0),
UIColor(red: 228/255, green: 241/255, blue: 254/255, alpha: 1.0),
UIColor(red: 65/255, green: 131/255, blue: 215/255, alpha: 1.0),
UIColor(red: 89/255, green: 171/255, blue: 227/255, alpha: 1.0),
UIColor(red: 129/255, green: 207/255, blue: 224/255, alpha: 1.0),
UIColor(red: 82/255, green: 179/255, blue: 217/255, alpha: 1.0),
UIColor(red: 197/255, green: 239/255, blue: 247/255, alpha: 1.0),
UIColor(red: 34/255, green: 167/255, blue: 240/255, alpha: 1.0),
UIColor(red: 52/255, green: 152/255, blue: 219/255, alpha: 1.0),
UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1.0),
UIColor(red: 25/255, green: 181/255, blue: 254/255, alpha: 1.0),
UIColor(red: 51/255, green: 110/255, blue: 123/255, alpha: 1.0),
UIColor(red: 34/255, green: 49/255, blue: 63/255, alpha: 1.0),
UIColor(red: 30/255, green: 139/255, blue: 195/255, alpha: 1.0),
UIColor(red: 58/255, green: 83/255, blue: 155/255, alpha: 1.0),
UIColor(red: 52/255,
Solution
This StackOverflow question can solve part of your problem. Setting up the array as a static variable within the function will mean it is only initialized once and any future call to the function will only have to calculate the random index to pull from and return whatever color object is at that index.
And while this solution should be an improvement from the perspective of time, it would mean you're app is constantly consuming a larger amount of memory (versus just temporarily consuming this memory while this function is calculating).
Your tuple idea in the comments is a better way to go.
Initializing the array of tuples should be slightly faster than initializing the array of
And while this solution should be an improvement from the perspective of time, it would mean you're app is constantly consuming a larger amount of memory (versus just temporarily consuming this memory while this function is calculating).
Your tuple idea in the comments is a better way to go.
typealias ColorTuple = (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
extension UIColor {
class func randomFlatColor() -> UIColor {
struct RandomColors {
static let colors: Array = [
(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0), // red
(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0), // green
(red: 0.0, green: 0.0, blue: 1.0, alpha: 1.0) // blue
// etc...
]
}
let colorCount = UInt32(RandomColors.colors.count)
let randomIndex = arc4random_uniform(colorCount)
let color = RandomColors.colors[Int(randomIndex)]
return UIColor(red: color.red, green: color.green, blue: color.blue, alpha: color.alpha)
}
}Initializing the array of tuples should be slightly faster than initializing the array of
UIColor objects. But importantly, it should take significantly less memory to keep this array around, and using this pattern, the array will only have to be initialized once.Code Snippets
typealias ColorTuple = (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
extension UIColor {
class func randomFlatColor() -> UIColor {
struct RandomColors {
static let colors: Array<ColorTuple> = [
(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0), // red
(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0), // green
(red: 0.0, green: 0.0, blue: 1.0, alpha: 1.0) // blue
// etc...
]
}
let colorCount = UInt32(RandomColors.colors.count)
let randomIndex = arc4random_uniform(colorCount)
let color = RandomColors.colors[Int(randomIndex)]
return UIColor(red: color.red, green: color.green, blue: color.blue, alpha: color.alpha)
}
}Context
StackExchange Code Review Q#81873, answer score: 4
Revisions (0)
No revisions yet.