How to get RGB values from UIColor?

后端 未结 15 1636
孤城傲影
孤城傲影 2020-11-28 22:27

I\'m creating a color object using the following code.

curView.backgroundColor = [[UIColor alloc] initWithHue:229 saturation:40 brightness:75 alpha:1];
         


        
15条回答
  •  眼角桃花
    2020-11-28 23:04

    Using HandyUIKit makes this really easy:

    import HandyUIKit    
    
    let color = UIColor(red: 0.1, green: 0.2, blue: 0.3, alpha: 0.4)
    
    // get any of the rgba values
    color.rgba.red    // => 0.1
    color.rgba.green  // => 0.2
    color.rgba.blue   // => 0.3
    color.rgba.alpha  // => 0.4
    

    There is also a similar option to get hsba values:

    let color = UIColor(hue: 0.1, saturation: 0.2, brightness: 0.3, alpha: 0.4)
    
    // you can get any of the hsba values, too
    color.hsba.hue         // => 0.1
    color.hsba.saturation  // => 0.2
    color.hsba.brightness  // => 0.3
    color.hsba.alpha       // => 0.4
    

    Simply install it using Carthage and you're good to go.

    I hope it helps!

提交回复
热议问题