How to get RGB values from UIColor?

后端 未结 15 1604
孤城傲影
孤城傲影 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!

    0 讨论(0)
  • 2020-11-28 23:05
    const float* colors = CGColorGetComponents( curView.backgroundColor.CGColor );
    

    Thanks. I had to add the const at the start of the line as it was generating a warning.

    0 讨论(0)
  • 2020-11-28 23:06

    I wanted to get the background color of the UITableViewStyle "UITableViewStyleGrouped" so in the viewDidAppear: method I added the code:

    NSLog(@"%@", self.tableView.backgroundView.backgroundColor);

    It did what I anticipated and returned the log:

    UIDeviceRGBColorSpace 0.937255 0.937255 0.956863 1

    So in short just type in NSLog(@"%@", [UIColor whateverColor]);

    0 讨论(0)
  • 2020-11-28 23:07

    Swift 3 version of David Rees answer:

    extension UIColor {
    
        var redValue: CGFloat{
            return cgColor.components! [0]
        }
    
        var greenValue: CGFloat{
            return cgColor.components! [1]
        }
    
        var blueValue: CGFloat{
            return cgColor.components! [2]
        }
    
        var alphaValue: CGFloat{
            return cgColor.components! [3]
        }
    }
    
    0 讨论(0)
  • 2020-11-28 23:10

    SWIFT 3 & 4

    I found that cgColor.components would not always return 4 color values, so I changed this so it gets them from a CIColor wrapper

    extension UIColor {
        var redValue: CGFloat{ return CIColor(color: self).red }
        var greenValue: CGFloat{ return CIColor(color: self).green }
        var blueValue: CGFloat{ return CIColor(color: self).blue }
        var alphaValue: CGFloat{ return CIColor(color: self).alpha }
    }
    

    SWIFT 2

    extension UIColor {
        var red: CGFloat{ return CGColorGetComponents(self.CGColor)[0] }
        var green: CGFloat{ return CGColorGetComponents(self.CGColor)[1] }
        var blue: CGFloat{ return CGColorGetComponents(self.CGColor)[2] }
        var alpha: CGFloat{ return CGColorGetComponents(self.CGColor)[3] }
    }
    

    It's not the most efficient way so I wouldn't go using this where a view will be constantly re-drawn.

    0 讨论(0)
  • 2020-11-28 23:12

    In iOS 5 you could use:

    CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha =0.0;
    [multipliedColor getRed:&red green:&green blue:&blue alpha:&alpha];
    
    0 讨论(0)
提交回复
热议问题