Extracting rgb from UIColor [duplicate]

孤街醉人 提交于 2019-11-27 09:48:46

问题


This question already has an answer here:

  • How to get RGB values from UIColor? 15 answers

Seen this asked before but my example does not seem to work.

const CGFloat *toCol = CGColorGetComponents([[UIColor greenColor] CGColor]);

The array is empty from looking at it with GDB. Any hints?


回答1:


The code sample you provided should work.

Try this:

UIColor uicolor = [[UIColor greenColor] retain];
CGColorRef color = [uicolor CGColor];

int numComponents = CGColorGetNumberOfComponents(color);

if (numComponents == 4)
{
  const CGFloat *components = CGColorGetComponents(color);
  CGFloat red = components[0];
  CGFloat green = components[1];
  CGFloat blue = components[2];
  CGFloat alpha = components[3];
}

[uicolor release];



回答2:


In iOS 5 you could use:

UIColor *color = [UIColor orangeColor];
CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;

if ([color respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
    [color getRed:&red green:&green blue:&blue alpha:&alpha];
} else {
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    red = components[0];
    green = components[1];
    blue = components[2];
    alpha = components[3];
}



回答3:


Here's an all round solution that also takes into account non-RGB colours e.g. [UIColor blackColor]

UIColor *color = [UIColor blackColor];
CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;
// iOS 5
if ([color respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
     [color getRed:&red green:&green blue:&blue alpha:&alpha];
} else {
     // < iOS 5
     const CGFloat *components = CGColorGetComponents(color.CGColor);
     red = components[0];
     green = components[1];
     blue = components[2];
     alpha = components[3];
}

// This is a non-RGB color
if(CGColorGetNumberOfComponents(color.CGColor) == 2) {
    CGFloat hue;
    CGFloat saturation;
    CGFloat brightness;
    [color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];

}



回答4:


Just use memcpy:

CGColorRef tmpColor = [[currentColorView backgroundColor] CGColor];
CGFloat newComponents[4] = {};
memcpy(newComponents, CGColorGetComponents(tmpColor), sizeof(newComponents));
// now newComponents is filled with tmpColor rgba data



回答5:


Thanks for the direction Willster. For any out there who are using grayscale color (created with colorWithWhite:alpha:), the code sample below will let you figure out the white value (the HSV method doesn't work on colors created this way).

CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0, white = 0.0;

// This is a non-RGB color
if(CGColorGetNumberOfComponents(self.color.CGColor) == 2) {
    [self.color getWhite:&white alpha:&alpha];
}
else {
    // iOS 5
    if ([self.color respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
        [self.color getRed:&red green:&green blue:&blue alpha:&alpha];
    } else {
        // < iOS 5
        const CGFloat *components = CGColorGetComponents(self.color.CGColor);
        red = components[0];
        green = components[1];
        blue = components[2];
        alpha = components[3];
    }
}



回答6:


This article helped me solve this problem or at least build the framework to solve it.

http://developer.apple.com/iphone/library/qa/qa2007/qa1509.html




回答7:


Here's some more direct sample code for getting RGB components from a arbitrary color:

Get RGB value from UIColor presets




回答8:


Checkout uicolor-utilities. Seems like a very good library for doing this and many other useful things with UIColor. For example, with this library you could write:

CGFloat red = [myColor red];


来源:https://stackoverflow.com/questions/816828/extracting-rgb-from-uicolor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!