How to get RGB values from UIColor?

后端 未结 15 1691
孤城傲影
孤城傲影 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:27

     UIColor *color = [[UIColor greenColor] retain]; //line 1
    
    //OR(You will have color variable either like line 1 or line 2)
    
    color = curView.backgroundColor;//line 2
    CGColorRef colorRef = [color CGColor];
    
    int _countComponents = CGColorGetNumberOfComponents(colorRef);
    
    if (_countComponents == 4) {
        const CGFloat *_components = CGColorGetComponents(colorRef);
        CGFloat red     = _components[0];
        CGFloat green = _components[1];
        CGFloat blue   = _components[2];
        CGFloat alpha = _components[3];
    
        NSLog(@"%f,%f,%f,%f",red,green,blue,alpha);
    }
    
    [color release];
    

提交回复
热议问题