ios 怎么获取redcomponent greencomponent
1个回答
展开全部
方法一:CGFloat* colors = CGColorGetComponents(hsbaColor.CGColor)
如:
UIColor *color = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
const CGFloat *components = CGColorGetComponents(color.CGColor);
NSLog(@"Red: %f", components[0]);
NSLog(@"Green: %f", components[1]);
NSLog(@"Blue: %f", components[2]);
CGColorGetComponents方法返回的是一个数组,存储的是RGBALPHA四个值,关于 CGColorGetComponents请看APPLE文档的描述.
CGColorGetComponents
Returns the values of the color components (including alpha) associated with a Quartz color.
const CGFloat * CGColorGetComponents (
CGColorRef color
);
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 of the array is one more than the number of components of the color space for the color.
但是,此方法只适用于RGB颜色空间,UIColor *color = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
如果是非RGB颜色空间 [UIColor grayColor]就不能用这个方法了!
方法二:
Google了一下,找到一个方法,记录备忘。自己亲身试过可行!
[objc] view plain copy
CGFloat R, G, B;
UIColor *uiColor = [lblDate textColor];
CGColorRef color = [uiColor CGColor];
int numComponents = CGColorGetNumberOfComponents(color);
if (numComponents == 4)
{
const CGFloat *components = CGColorGetComponents(color);
R = components[0];
G = components[1];
B = components[2];
}
方法三:
在stackoverflow找到的,未试过,应该没什么问题
附上原文地址:http://stackoverflow.com/questions/4700168/get-rgb-value-from-uicolor-presets
5down voteaccepted
The only way I know of for doing this is to create a Core Graphics bitmap context in an RGB color space and draw into it with your color. You can then read out the RGB values from the resulting bitmap. Note that this won't work for certain colors, like pattern colors (eg. [UIColor groupTableViewBackgroundColor]), which don't have reasonable RGB values.
- (void)getRGBComponents:(CGFloat [3])components forColor:(UIColor *)color { CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); unsigned char resultingPixel[4]; CGContextRef context = CGBitmapContextCreate(&resultingPixel, 1, 1, 8, 4, rgbColorSpace, kCGImageAlphaNoneSkipLast); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, CGRectMake(0, 0, 1, 1)); CGContextRelease(context); CGColorSpaceRelease(rgbColorSpace); for (int component = 0; component < 3; component++) { components[component] = resultingPixel[component] / 255.0f; } }
You can use it something like this:
CGFloat components[3]; [self getRGBComponents:components forColor:[UIColor grayColor]]; NSLog(@"%f %f %f", components[0], components[1], components[2]);
如:
UIColor *color = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
const CGFloat *components = CGColorGetComponents(color.CGColor);
NSLog(@"Red: %f", components[0]);
NSLog(@"Green: %f", components[1]);
NSLog(@"Blue: %f", components[2]);
CGColorGetComponents方法返回的是一个数组,存储的是RGBALPHA四个值,关于 CGColorGetComponents请看APPLE文档的描述.
CGColorGetComponents
Returns the values of the color components (including alpha) associated with a Quartz color.
const CGFloat * CGColorGetComponents (
CGColorRef color
);
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 of the array is one more than the number of components of the color space for the color.
但是,此方法只适用于RGB颜色空间,UIColor *color = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
如果是非RGB颜色空间 [UIColor grayColor]就不能用这个方法了!
方法二:
Google了一下,找到一个方法,记录备忘。自己亲身试过可行!
[objc] view plain copy
CGFloat R, G, B;
UIColor *uiColor = [lblDate textColor];
CGColorRef color = [uiColor CGColor];
int numComponents = CGColorGetNumberOfComponents(color);
if (numComponents == 4)
{
const CGFloat *components = CGColorGetComponents(color);
R = components[0];
G = components[1];
B = components[2];
}
方法三:
在stackoverflow找到的,未试过,应该没什么问题
附上原文地址:http://stackoverflow.com/questions/4700168/get-rgb-value-from-uicolor-presets
5down voteaccepted
The only way I know of for doing this is to create a Core Graphics bitmap context in an RGB color space and draw into it with your color. You can then read out the RGB values from the resulting bitmap. Note that this won't work for certain colors, like pattern colors (eg. [UIColor groupTableViewBackgroundColor]), which don't have reasonable RGB values.
- (void)getRGBComponents:(CGFloat [3])components forColor:(UIColor *)color { CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); unsigned char resultingPixel[4]; CGContextRef context = CGBitmapContextCreate(&resultingPixel, 1, 1, 8, 4, rgbColorSpace, kCGImageAlphaNoneSkipLast); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, CGRectMake(0, 0, 1, 1)); CGContextRelease(context); CGColorSpaceRelease(rgbColorSpace); for (int component = 0; component < 3; component++) { components[component] = resultingPixel[component] / 255.0f; } }
You can use it something like this:
CGFloat components[3]; [self getRGBComponents:components forColor:[UIColor grayColor]]; NSLog(@"%f %f %f", components[0], components[1], components[2]);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询