我从这个方法返回一个UIColor:
- (UIColor *)getUserSelectedColor {
return [UIColor colorWithRed:redSlider.value green:greenSlider.value blue:blueSlider.value alpha:1.0];
}
并获得这样的颜色:
UIColor *selectedColor = [(ColorPickerView *)alertView getUserSelectedColor];
现在我想从selectedColor获取红色,绿色和蓝色,以便使用这些值。我想要0到1之间的值。
答案 0 :(得分:112)
访问SelectedColor.CGColor
时崩溃的原因可能是您没有保留getColor
的结果,也许您需要的是:
SelectedColor = [[(ColorPickerView *)alertView getColor] retain];
您只能从使用RGB色彩空间的UIColor
获取RGB颜色分量,因为您使用的colorWithRed:green:blue:alpha:
不是问题,但如果您的代码发生变化,则可能会有所不同
有了这个想法,让颜色组件变得非常简单:
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));
答案 1 :(得分:23)
此解决方案也适用于非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];
}
答案 2 :(得分:8)
在大多数情况下,这将起作用,除非转换为RGB不起作用。
float red, green, blue, alpha;
BOOL conversionToRGBWentOk = [color getRed:&red green:&green blue:&blue alpha:&alpha];
事实上,这就是这些方法的用途。如果conversionToRGBWentOk
为NO
,则会遇到问题。
答案 3 :(得分:4)
我认为你应该有一个a look here,其中Ars'指南展示如何扩展UIColor
类,并支持访问颜色组件。
答案 4 :(得分:2)
你只是简单地做这个
CGFloat red,green,blue,alpha;
[UIColorobject getRed:&red green:&green blue:&blue alpha:&alpha];
红色,绿色,蓝色和alpha你得到rgb值 如果您有任何疑问,请询问......
谢谢
答案 5 :(得分:1)
这段代码应该适用于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];
}
答案 6 :(得分:0)
只需添加属性ColorLiteral
,如示例所示。 Xcode将提示您可以选择的整个颜色列表。
self.view.backgroundColor = ColorLiteral
答案 7 :(得分:0)
我认为这是一种方法。如果您还需要使用alpha参数,则可以像输入R G和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];
}
答案 8 :(得分:0)
以下是我为此和其他颜色控件制作的一些有用的宏:
在您的情况下,您只会使用
getRGBA(myColor, red, green, blue, alpha);
NSLog(@"Red Value: %f", red);
NSLog(@"Blue Value: %f", green);
NSLog(@"Green Value: %f", blue);
#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;\
})()\
)
*/
答案 9 :(得分:0)
有一个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(&green, green: nil, 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
}
}
它与Swift 4.2兼容,并且还可以处理黑色,灰色等2种颜色。 您可以像这样进入特定的运河:
myColor.rgba.blue
或者,等价的:
myColor.blueComponent