Mark B
Mark B

Reputation: 147

How to extract the name of the type of color that was just randomly generated

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

Answers (4)

Alessign
Alessign

Reputation: 788

It is totally possible! Here's very basic solution for Swift 3:

  1. To pre-define colours go to http://colorizer.org and use the HSV/HSB sliders for each colour wanted. Start from left to define the ranges for each slider. It's a bit of work, but we need those values. Notice the ranges:

Hue = 0-360
Sat = 0-100
Br = 0-100

  1. After we have the values of each colour range, we need to convert the HUE values for Swift use by dividing it by 360. Saturation & Brightness values needs to be divided by 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:

enter image description here

  1. 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

Choppin Broccoli
Choppin Broccoli

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

Cole
Cole

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

Obj-Swift
Obj-Swift

Reputation: 2942

Once you have generated random color, get the rgb values. Use this link to define color names for appropriate rgb values and generate names.It certainly doesn't cover all the colors but its one of those possible solutions.

Upvotes: 0

Related Questions