我从这个方法返回一个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之间的值。
访问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));
这是我为此和其他颜色控件制作的一些有用的宏:
在你的情况下,你会使用
ColorLiteral
self.view.backgroundColor = ColorLiteral
该解决方案也适用于非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];
}
在大多数情况下,这将起作用,除非转换为RGB不起作用。
float red, green, blue, alpha;
BOOL conversionToRGBWentOk = [color getRed:&red green:&green blue:&blue alpha:&alpha];
事实上,这就是这些方法的用途。如果conversionToRGBWentOk
是NO
你会遇到问题。
我认为你应该有一个qazxsw poi,其中'指南显示如何扩展qazxsw poi类,支持访问颜色组件。
这段代码应该适用于RGB和灰度:
UIColor
我认为这是一种方法。如果您还需要使用alpha参数,则可以像输入R G和B一样从输入中插入alpha。
CGFloat red,green,blue,alpha;
[UIColorobject getRed:&red green:&green blue:&blue alpha:&alpha];
有一个Swift扩展:)
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];
}
它与Swift 4.2兼容,也适用于黑色,灰色等2种颜色的组件。您可以访问特定的运河,如下所示:
- (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];
}
或者,它的等价物:
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
}
}
只需添加属性myColor.rgba.blue
,如示例中所示。 Xcode将提示您可以选择的整个颜色列表。
myColor.blueComponent