方法一:
CGFloat
*
colors
=
CGColorGetComponents
(
hsbaColor
.
CGColor
)
CGColorRef color
如:
UIColor *color = [UIColor colorWithRed:0.0 green:0.0 blue:1.0alpha:1.0];
const CGFloat *components =CGColorGetComponents(color.CGColor);
NSLog(@"Red: %f", components[0]);
NSLog(@"Green: %f", components[1]);
NSLog(@"Blue: %f", components[2]);
CGColorGetComponents方法返回的是一个数组,存储的是R\G\B\ALPHA四个值,关于
CGColorGetComponents请看APPLE文档的描述.
CGColorGetComponents
Returns the values of the color components (including alpha)associated with a Quartz color.
const CGFloat * CGColorGetComponents (
);
Parameters
color
A Quartz color.
Return Value
An array of intensity values for the color components(including alpha) associated with the specified color. The size ofthe array is one more than the number of components of the colorspace for the color.
但是,此方法只适用于RGB颜色空间,UIColor *color =[UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
如果是非RGB颜色空间
[UIColorgrayColor]就不能用这个方法了!
方法二:
Google了一下,找到一个方法,记录备忘。自己亲身试过可行!
1 2 3 4 5 6 7 8 9 10 11 12 13 | CGFloat R, G, B; |
方法三:
在stackoverflow找到的,未试过,应该没什么问题
5
downvote
|
The only way I know of for doing this is to create a Core Graphicsbitmap context in an RGB color space and draw into it with yourcolor. You can then read out the RGB values from the resultingbitmap. Note that this won't work for certain colors, like patterncolors (eg. [UIColor groupTableViewBackground
You can use it something like this:
|