如何从UIColor对象获得红绿蓝(RGB)和alpha背面? [英] How do I get red green blue (RGB) and alpha back from a UIColor object?
问题描述
我从这个方法返回一个UIColor:
I am getting a UIColor returned from this method:
- (UIColor *)getUserSelectedColor {
return [UIColor colorWithRed:redSlider.value green:greenSlider.value blue:blueSlider.value alpha:1.0];
}
并获得如下颜色:
UIColor *selectedColor = [(ColorPickerView *)alertView getUserSelectedColor];
现在我想从selectedColor获得红色,绿色和蓝色,以便使用这些值。我想要0到1之间的值。
Now I want to get red, green, blue from selectedColor, in order to use those values. I want values between 0 and 1.
推荐答案
访问 SelectedColor.CGColor时崩溃的原因
可能是你没有保留 getColor
的结果,也许你需要的是:
The reason for the crash when accessing SelectedColor.CGColor
could be that you do not retain the result from getColor
, perhaps what you need is:
SelectedColor = [[(ColorPickerView *)alertView getColor] retain];
您只能从 UIColor获取RGB颜色分量
使用RGB颜色空间,因为你使用的是 colorWithRed:green:blue:alpha:
这不是问题,但如果你的代码有所不同更改。
You can only get the RGB color component from a UIColor
that is using the RGB color space, since you are using colorWithRed:green:blue:alpha:
that is not a problem, but be vary of this if your code changes.
这样可以让颜色组件变得非常简单:
With this is mind getting the color components is really easy:
const CGFloat* components = CGColorGetComponents(SelectedColor.CGColor);
NSLog(@"Red: %f", components[0]);
NSLog(@"Green: %f", components[1]);
NSLog(@"Blue: %f", components[2]);
NSLog(@"Alpha: %f", CGColorGetAlpha(SelectedColor.CGColor));
这篇关于如何从UIColor对象获得红绿蓝(RGB)和alpha背面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!