Reputation: 147
I would like to generate a random color and then extract the name of the color. I know that I can generate the color by creating three randomized floats and then returning a UIColor, but I am lost when it comes time to define the name of the color that has just been created.
Upvotes: 1
Views: 1546
Reputation: 788
It is totally possible! Here's very basic solution for Swift 3:
Hue = 0-360
Sat = 0-100
Br = 0-100
For instance, if I want to define blue colour, the measures from the website I took are
H = 179...240
S = 50...100
B = 60...100
H = H/360
S = S/100
B = B/100
H range = 0.497...0.667
S range = 0.5...1.0
B range = 0.6...1.0
I have pre-defined few basic colours in spreadsheet:
Now we need to implement in the code. Create function as such:
func whichColor(color: UIColor) -> String{
var (h,s,b,a) : (CGFloat, CGFloat, CGFloat, CGFloat) = (0,0,0,0)
_ = color.getHue(&h, saturation: &s, brightness: &b, alpha: &a)
print("HSB range- h: \(h), s: \(s), v: \(b)")
var colorTitle = ""
switch (h, s, b) {
// red
case (0...0.138, 0.88...1.00, 0.75...1.00):
colorTitle = "red"
// yellow
case (0.139...0.175, 0.30...1.00, 0.80...1.00):
colorTitle = "yellow"
// green
case (0.176...0.422, 0.30...1.00, 0.60...1.00):
colorTitle = "green"
// teal
case (0.423...0.494, 0.30...1.00, 0.54...1.00):
colorTitle = "teal"
// blue
case (0.495...0.667, 0.30...1.00, 0.60...1.00):
colorTitle = "blue"
// purple
case (0.668...0.792, 0.30...1.00, 0.40...1.00):
colorTitle = "purple"
// pink
case (0.793...0.977, 0.30...1.00, 0.80...1.00):
colorTitle = "pink"
// brown
case (0...0.097, 0.50...1.00, 0.25...0.58):
colorTitle = "brown"
// white
case (0...1.00, 0...0.05, 0.95...1.00):
colorTitle = "white"
// grey
case (0...1.00, 0, 0.25...0.94):
colorTitle = "grey"
// black
case (0...1.00, 0...1.00, 0...0.07):
colorTitle = "black"
default:
print("empty def")
colorTitle = "Color didn't fit defined ranges..."
}
return colorTitle
}
And now just pass the colour you want to analyse and it should print generic name of the colour:
print("Color: \(self.whichColor(color: YourColorSource!))")
It is not perfect, whoever wants to use it, needs to play with the ranges properly to define the real borders between colours or implementing more colours.
Note: Some values from the spreadsheet are not matching the Swift code as I have adjusted it for continuous range of colours without gaps...
Upvotes: 3
Reputation: 3076
I don't think that's really possible. You can, however, create your own NSDictionary of UIColors (where the key would be the name you give the color) and then randomly select one from the dictionary.
Upvotes: 1
Reputation: 2646
Another option would be to define a set of ranges for Red, Blue, and Green. Based on where the color RGB values fall into their respective set of ranges, it will return some approximate color name.
For example, if Red, Green, and Blue are all close to 255, you append "Light" to the front of the color name (as in "Light Green")
I also found this library which accepts enum values for its random color initializers. You could probably adapt those enums to also return corresponding string values, forming a color name dynamically.
Upvotes: 1