objective-c 如何从 UIColor 对象获取红绿蓝 (RGB) 和 alpha 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1496716/
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
How do I get red green blue (RGB) and alpha back from a UIColor object?
提问by Rahul Vyas
I am getting a UIColor returned from this method:
我从这个方法返回一个 UIColor :
- (UIColor *)getUserSelectedColor {
return [UIColor colorWithRed:redSlider.value green:greenSlider.value blue:blueSlider.value alpha:1.0];
}
and getting color like this:
并获得这样的颜色:
UIColor *selectedColor = [(ColorPickerView *)alertView getUserSelectedColor];
Now I want to get red, green, blue from selectedColor, in order to use those values. I want values between 0 and 1.
现在我想从 selectedColor 中获取红色、绿色、蓝色,以便使用这些值。我想要 0 到 1 之间的值。
回答by PeyloW
The reason for the crash when accessing SelectedColor.CGColorcould be that you do not retain the result from getColor, perhaps what you need is:
访问时崩溃的原因SelectedColor.CGColor可能是您没有保留 的结果getColor,也许您需要的是:
SelectedColor = [[(ColorPickerView *)alertView getColor] retain];
You can only get the RGB color component from a UIColorthat 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.
您只能从UIColor使用 RGB 颜色空间的a中获取 RGB 颜色分量,因为您使用colorWithRed:green:blue:alpha:的不是问题,但是如果您的代码发生变化,则可能会有所不同。
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));
回答by Willster
This solution works for non-RGB colours as well e.g. black or white color.
此解决方案也适用于非 RGB 颜色,例如黑色或白色。
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];
}
回答by Dan Rosenstark
In most cases this will work, unless the conversion to RGB doesn't work.
在大多数情况下,这将起作用,除非转换为 RGB 不起作用。
float red, green, blue, alpha;
BOOL conversionToRGBWentOk = [color getRed:&red green:&green blue:&blue alpha:&alpha];
That's what these methods are for, in fact. If the conversionToRGBWentOkis NOyou'll have a problem, though.
事实上,这就是这些方法的用途。但是,如果conversionToRGBWentOk是,NO您将遇到问题。
回答by unwind
I think you should have a a look here, where Ars' guide shows how to extend the UIColorclass with support for accessing the color components.
我想你应该看看这里,其中 Ars 的指南展示了如何UIColor通过支持访问颜色组件来扩展类。
回答by Axel Guilmin
There is a Swift extension for that :)
有一个 Swift 扩展:)
extension UIColor {
var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
var red: CGFloat = 0.0
var green: CGFloat = 0.0
var blue: CGFloat = 0.0
var alpha: CGFloat = 0.0
getRed(&red, green: &green, blue: &blue, alpha: &alpha)
return (red: red, green: green, blue: blue, alpha: alpha)
}
var redComponent: CGFloat {
var red: CGFloat = 0.0
getRed(&red, green: nil, blue: nil, alpha: nil)
return red
}
var greenComponent: CGFloat {
var green: CGFloat = 0.0
getRed(nil, green: &green, blue: nil, alpha: nil)
return green
}
var blueComponent: CGFloat {
var blue: CGFloat = 0.0
getRed(nil, green: nil, blue: &blue, alpha: nil)
return blue
}
var alphaComponent: CGFloat {
var alpha: CGFloat = 0.0
getRed(nil, green: nil, blue: nil, alpha: &alpha)
return alpha
}
}
It is compatible with Swift 4.2 and also works with 2 components colors like black, gray, etc. You can access a specific canal like so :
它与 Swift 4.2 兼容,也适用于 2 个组件颜色,如黑色、灰色等。您可以像这样访问特定的运河:
myColor.rgba.blue
Or, its equivalent :
或者,它的等价物:
myColor.blueComponent
回答by Waseem Shah
you just simple doing this
你只是简单地做这个
CGFloat red,green,blue,alpha;
[UIColorobject getRed:&red green:&green blue:&blue alpha:&alpha];
in red,green,blue and alpha you get rgb value if you have any question please ask...
在红色、绿色、蓝色和 alpha 中,如果您有任何问题,请询问 rgb 值...
Thanks
谢谢
回答by manicaesar
This snippet of code should work with both RGB and grayscale:
这段代码应该适用于 RGB 和灰度:
CGFloat *components = (CGFloat *) CGColorGetComponents(<UIColor instance>.CGColor);
if(CGColorGetNumberOfComponents(<UIColor instance>.CGColor) == 2)
{
//assuming it is grayscale - copy the first value
components[2] = components[1] = components[0];
}
回答by Aleksandar
I think this would be a way to go. If you need to use alpha parameter as well, you can interpolate alpha from input like you do for R G and B.
我认为这将是一条路。如果您还需要使用 alpha 参数,您可以像对 RG 和 B 一样从输入中插入 alpha。
- (UIColor *)getColorBetweenColor:(UIColor *)color1 andColor:(UIColor *)color2 percentage:(CGFloat)percent {
CGFloat red1, green1, blue1, alpha1;
CGFloat red2, green2, blue2, alpha2;
[color1 getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1];
[color2 getRed:&red2 green:&green2 blue:&blue2 alpha:&alpha2];
double resultRed = red1 + percent * (red2 - red1);
double resultGreen = green1 + percent * (green2 - green1);
double resultBlue = blue1 + percent * (blue2 - blue1);
return [UIColor colorWithRed:resultRed green:resultGreen blue:resultBlue alpha:1];
}
回答by Albert Renshaw
Here are some useful macros I've made for this and other color controls:
以下是我为此和其他颜色控件制作的一些有用的宏:
In your case you would just use
在您的情况下,您只需使用
getRGBA(myColor, red, green, blue, alpha);
NSLog(@"Red Value: %f", red);
NSLog(@"Blue Value: %f", green);
NSLog(@"Green Value: %f", blue);
Macros:
宏:
#define rgba(r,g,b,a) [UIColor colorWithRed:((float)(r))/255.0f green:((float)(g))/255.0f blue:((float)(b))/255.0f alpha:a]
#define rgb(r,g,b) rgba(r, g, b, 1.0f)
#define rgbaf(r,g,b,a) [UIColor colorWithRed:(r) green:(g) blue:(b) alpha:a]
#define rgbf(r,g,b) rgbaf(r, g, b, 1.0f)
#define rgba_fromColor(__color, __r, __g, __b, __a) \
CGFloat __r, __g, __b, __a;\
UIColor *__unpackedColor = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:__color]];/*Bring system colors into compatible color-space (e.g. DarkGrayColor)*/\
[__unpackedColor getRed:&__r green:&__g blue:&__b alpha:&__a];
#define getRGBA(__color, __r, __g, __b, __a) rgba_fromColor(__color, __r, __g, __b, __a)
#define getRed(__color) (\
(^float (void){\
rgba_fromColor(__color, r, g, b, a);\
return r;\
})()\
)
#define getGreen(__color) (\
(^float (void){\
rgba_fromColor(__color, r, g, b, a);\
return g;\
})()\
)
#define getBlue(__color) (\
(^float (void){\
rgba_fromColor(__color, r, g, b, a);\
return b;\
})()\
)
#define getAlpha(__color) (\
(^float (void){\
rgba_fromColor(__color, r, g, b, a);\
return a;\
})()\
)
#define hsba(h,s,b,a) [UIColor colorWithHue:((float)(h))/360.0f saturation:((float)(s))/100.0f brightness:((float)(b))/100.0f alpha:a]
#define hsb(h,s,b) hsba(h, s, b, 1.0f)
#define hsbaf(h,s,b,a) [UIColor colorWithHue:(h) saturation:(s) brightness:(b) alpha:a]
#define hsbf(h,s,b) rgbaf(h, s, b, 1.0f)
#define hsba_fromColor(__color, __h, __s, __b, __a) \
CGFloat __h, __s, __b, __a;\
UIColor *__unpackedColor = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:__color]];/*Bring system colors into compatible color-space (e.g. DarkGrayColor)*/\
[__unpackedColor getHue:&__h saturation:&__s brightness:&__b alpha:&__a];
#define getHSBA(__color, __h, __s, __b, __a) hsba_fromColor(__color, __h, __s, __b, __a)
#define getHue(__color) (\
(^float (void){\
hsba_fromColor(__color, h, s, b, a);\
return h;\
})()\
)
#define getSaturation(__color) (\
(^float (void){\
hsba_fromColor(__color, h, s, b, a);\
return s;\
})()\
)
#define getBrightness(__color) (\
(^float (void){\
hsba_fromColor(__color, h, s, b, a);\
return b;\
})()\
)
/*
///already defined in RGBA macros
#define getAlpha(__color) (\
(^float (void){\
hsba_fromColor(__color, h, s, b, a);\
return a;\
})()\
)
*/
回答by puneeth
Just add the property ColorLiteralas shown in the example. Xcode will prompt you with a whole list of colors which you can choose.
只需添加ColorLiteral示例中所示的属性。Xcode 会提示你一个完整的颜色列表,你可以选择。
self.view.backgroundColor = ColorLiteral

