【问题标题】:Extracting rgb from UIColor [duplicate]从 UIColor 中提取 rgb [重复]
【发布时间】:2010-10-23 10:37:11
【问题描述】:

以前见过这个问题,但我的示例似乎不起作用。

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

使用 GDB 查看数组是空的。有什么提示吗?

【问题讨论】:

    标签: iphone core-graphics uicolor cgcolor


    【解决方案1】:

    感谢威尔斯特的指导。对于任何使用灰度颜色(使用colorWithWhite:alpha: 创建)的人,下面的代码示例将让您计算出白色值(HSV 方法不适用于以这种方式创建的颜色)。

    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];
        }
    }
    

    【讨论】:

      【解决方案2】:

      这是一个全面的解决方案,它还考虑了非 RGB 颜色,例如[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];
      
      }
      

      【讨论】:

        【解决方案3】:

        在 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];
        }
        

        【讨论】:

        • 在 MonoTouch (Xamarin) 中:color.GetRGBA(out red, out green, out blue, out alpha);
        【解决方案4】:

        只需使用 memcpy:

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

        【讨论】:

        • 会测试,如果没问题,这是最简单的解决方案,而且理解起来并不复杂
        • 我在我的 ios 应用程序中使用了它,它可以工作。看不出有什么理由不使用这种方法
        【解决方案5】:

        结帐uicolor-utilities。似乎是一个非常好的库,可以使用 UIColor 执行此操作以及许多其他有用的事情。例如,使用这个库,您可以编写:

        CGFloat red = [myColor red];
        

        【讨论】:

          【解决方案6】:

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

          试试这个:

          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 个(不是 4 个)组件。
          • 仅供参考,Susanna,灰度颜色仅显示为 2 个分量。
          • 我已经发布了一个也可以处理非 RGB 颜色/灰度颜色的解决方案。
          猜你喜欢
          • 2017-05-03
          • 1970-01-01
          • 2015-04-23
          • 1970-01-01
          • 2010-10-01
          • 2012-02-26
          • 2010-12-02
          • 2011-08-24
          相关资源
          最近更新 更多