How to get RGB values from UIColor?

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

    Just made a category for this.

    NSLog(@"%f", [UIColor blueColor].blue); // 1.000000
    

    Goes something like:

    typedef enum { R, G, B, A } UIColorComponentIndices;
    
    @implementation UIColor (EPPZKit)
    
    -(CGFloat)red
    { return CGColorGetComponents(self.CGColor)[R]; }
    
    -(CGFloat)green
    { return CGColorGetComponents(self.CGColor)[G]; }
    
    -(CGFloat)blue
    { return CGColorGetComponents(self.CGColor)[B]; }
    
    -(CGFloat)alpha
    { return CGColorGetComponents(self.CGColor)[A]; }
    
    @end
    

    Part of eppz!kit with more UIColor goodies.

    0 讨论(0)
  • 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];
    
    0 讨论(0)
  • 2020-11-28 23:27

    set your UIColor like this

    UIColor.FromRGB(128, 179, 255)
    

    this is for Xamarin ios... but for sure there is a method like this in swift.

    0 讨论(0)
提交回复
热议问题