`
啸笑天
  • 浏览: 3470433 次
  • 性别: Icon_minigender_1
  • 来自: China
社区版块
存档分类
最新评论

CGColorGetNumberOfComponents

 
阅读更多

[UIColor whiteColor] and [UIColor blackColor] use [UIColor colorWithWhite:alpha:] to create the UIColor. Which means this CGColorRef has only 2 color components, not 4 like colors created with [UIColor colorWithRed:green:blue:alpha:].

Of course you can NSLog those too.

if (CGColorGetNumberOfComponents(myColorRef) == 2) {
    const CGFloat *colorComponents = CGColorGetComponents(myColorRef);
    NSLog(@"r=%f, g=%f, b=%f, a=%f", colorComponents[0], colorComponents[0], colorComponents[0], colorComponents[1]);
}
else if (CGColorGetNumberOfComponents(myColorRef) == 4) {
    const CGFloat * colorComponents = CGColorGetComponents(myColorRef);
    NSLog(@"r=%f, g=%f, b=%f, a=%f", colorComponents[0], colorComponents[1], colorComponents[2], colorComponents[3]);
}
else {
    NSLog(@"What is this?");
}

 Be aware that there are different colorSpaces too. So if you need this code for more than logging (e.g. saving RGBA strings to json) you have to check (and probably convert) the colorSpace too.

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics