代码之家  ›  专栏  ›  技术社区  ›  J S

从UIColor中提取rgb[重复]

  •  80
  • J S  · 技术社区  · 17 年前

    看到这个问题之前,但我的例子似乎不起作用。

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

    数组是空的,无法用GDB查看它。有什么线索吗?

    8 回复  |  直到 16 年前
        1
  •  109
  •   Pablo Santa Cruz    17 年前

    您提供的代码示例应该可以工作。

    试试这个:

    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
  •  38
  •   Teetotum    14 年前

    在iOS 5中,您可以使用:

    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
  •  14
  •   Willster justkt    14 年前

    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
  •  7
  •   Realexer    15 年前

    只需使用memcpy:

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

    谢谢你给威尔斯特指路。对于任何使用灰度颜色的人 colorWithWhite:alpha:

    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
  •  1
  •   OhioDude    17 年前

    本文帮助我解决了这个问题,或者至少构建了解决这个问题的框架。

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

        8
  •  1
  •   benvolioT    15 年前

    结帐 uicolor-utilities . 似乎是一个非常好的库,可以用UIColor来做这些和其他许多有用的事情。例如,使用此库可以编写:

    CGFloat red = [myColor red];