从 UIColor 中提取 RGB

发布于 2024-07-18 08:56:54 字数 166 浏览 10 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

时光无声 2024-07-25 08:56:54

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

尝试这个:

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

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];
自此以后,行同陌路 2024-07-25 08:56:54

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

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];
}
杀手六號 2024-07-25 08:56:54

这是一个全面的解决方案,还考虑了非 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];

}

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

}
潦草背影 2024-07-25 08:56:54

只需使用 memcpy:

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

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
写下不归期 2024-07-25 08:56:54

感谢威尔斯特的指导。 对于任何使用灰度颜色(使用 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];
    }
}

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];
    }
}
心凉怎暖 2024-07-25 08:56:54

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

CGFloat red = [myColor red];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文