问题描述
在iOS 5的UIColor中有这个方法:
There's this method in UIColor in iOS 5:
- (BOOL)getHue:(CGFloat *)hue saturation:(CGFloat *)saturation brightness:(CGFloat *)brightness alpha:(CGFloat *)alpha
不知道我打算如何在代码中使用它。当然,我不需要说明每个组件,如果我想要从UIColor中获得。
But i don't understand how i'm meant to use that in code. Surely i don't need to be stating each of those components if i'm looking to get that out of the UIColor?
推荐答案
CGFloat hue;
CGFloat saturation;
CGFloat brightness;
CGFloat alpha;
[aColor getHue:&hue
saturation:&saturation
brightness:&brightness
alpha:&alpha];
//now the variables hold the values
编辑 br>
getHue:saturation:brightness:alpha:
返回一个bool,确定UIColor是否已被转换。
editgetHue:saturation:brightness:alpha:
returns a bool, determining, if the UIColor could had been converted at all.
例如:
BOOL b = [[UIColor colorWithRed:.23 green:.42 blue:.9 alpha:1.0] getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
NSLog(@"%f %f %f %f %d", hue, saturation, brightness, alpha, b);
将记录 0.619403 0.744444 0.900000 1.000000 1
因为它是有效的
而
BOOL b = [[UIColor colorWithPatternImage:[UIImage imageNamed:@"pattern.png"]] getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
NSLog(@"%f %f %f %f %d", hue, saturation, brightness, alpha, b);
logs 0.000000 0.000000 -1.998918 0.000000 0
。最后的0是Bool,所以这是无效的,实际上亮度只能从0.0到1.0
,但在这里它拥有一些随机的垃圾。
logs 0.000000 0.000000 -1.998918 0.000000 0
. The last 0 is the Bool, so this is not valid, and actually brightness can only range from 0.0 to 1.0
, but here it holds some random crap.
结论
代码应该是
conclusion
The code should be something like
CGFloat hue;
CGFloat saturation;
CGFloat brightness;
CGFloat alpha;
if([aColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]){
//do what ever you want to do if values are valid
} else {
//what needs to be done, if converting failed?
//Some default values? raising an exception? return?
}
这篇关于如何使用“getHue:saturation:brightness:alpha:”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!