ios 如何从 UIColor 获取 RGB 值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/437113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:31:48  来源:igfitidea点击:

How to get RGB values from UIColor?

iosrgbuicolor

提问by defmech

I'm creating a color object using the following code.

我正在使用以下代码创建一个颜色对象。

curView.backgroundColor = [[UIColor alloc] initWithHue:229 saturation:40 brightness:75 alpha:1];

How can I retrieve RGB values from the created color object?

如何从创建的颜色对象中检索 RGB 值?

采纳答案by codelogic

const CGFloat *colors = CGColorGetComponents( curView.backgroundColor.CGColor );

These links provide further details:

这些链接提供了更多详细信息:

回答by Teetotum

In iOS 5 you could use:

在 iOS 5 中,您可以使用:

CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha =0.0;
[multipliedColor getRed:&red green:&green blue:&blue alpha:&alpha];

回答by David Rees

SWIFT 3 & 4

SWIFT 3 & 4

I found that cgColor.components would not always return 4 color values, so I changed this so it gets them from a CIColor wrapper

我发现 cgColor.components 并不总是返回 4 个颜色值,所以我改变了它,以便从 CIColor 包装器中获取它们

extension UIColor {
    var redValue: CGFloat{ return CIColor(color: self).red }
    var greenValue: CGFloat{ return CIColor(color: self).green }
    var blueValue: CGFloat{ return CIColor(color: self).blue }
    var alphaValue: CGFloat{ return CIColor(color: self).alpha }
}

SWIFT 2

快速 2

extension UIColor {
    var red: CGFloat{ return CGColorGetComponents(self.CGColor)[0] }
    var green: CGFloat{ return CGColorGetComponents(self.CGColor)[1] }
    var blue: CGFloat{ return CGColorGetComponents(self.CGColor)[2] }
    var alpha: CGFloat{ return CGColorGetComponents(self.CGColor)[3] }
}

It's not the most efficient way so I wouldn't go using this where a view will be constantly re-drawn.

这不是最有效的方法,所以我不会在视图会不断重新绘制的情况下使用它。

回答by Ashok Kumar

Hopefully this will be helpful

希望这会有所帮助

CGFloat red, green, blue, alpha;

//Create a sample color

UIColor *redColor = [UIColor redColor];

//Call 

[redColor getRed: &red 
  green: &green
  blue: &blue 
  alpha: &alpha];
NSLog(@"red = %f. Green = %f. Blue = %f. Alpha = %f",
  red,
  green,
  blue,
  alpha);

回答by Geri Borbás

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!kitwith more UIColor goodies.

部分eppz!kit有更多的UIColor的好东西

回答by defmech

const float* colors = CGColorGetComponents( curView.backgroundColor.CGColor );

Thanks. I had to add the constat the start of the line as it was generating a warning.

谢谢。我必须const在行的开头添加 ,因为它会生成警告。

回答by Linh Nguyen

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

回答by danfordham

Swift 3 version of David Rees answer:

Swift 3 版本的 David Rees 回答:

extension UIColor {

    var redValue: CGFloat{
        return cgColor.components! [0]
    }

    var greenValue: CGFloat{
        return cgColor.components! [1]
    }

    var blueValue: CGFloat{
        return cgColor.components! [2]
    }

    var alphaValue: CGFloat{
        return cgColor.components! [3]
    }
}

回答by pkamb

The top voted answer is outdated:

最高投票的答案已过时:

error: :3:1: error: 'CGColorGetComponents' has been replaced by property 'CGColor.components'

错误::3:1:错误:“CGColorGetComponents”已被属性“CGColor.components”替换

Instead, use

相反,使用

myUIColor.cgColor.components

myUIColor.cgColor.components

回答by Mari

You can use CIColor components (swift 5)

您可以使用 CIColor 组件(swift 5)

let ciColor = CIColor(color: backgroundColor)
let alpha = ciColor.alpha
let red = ciColor.red
let blue = ciColor.blue
let green = ciColor.green

this works for non-RGB color space too

这也适用于非 RGB 色彩空间