如何获取一个UIColor中 各个分量的值

时间:2022-12-17 23:30:49

首先 UIColor 实际上是 UIKit对CGColor的一个包装类  当我们对较低级的 CGColor 层次进行编程时,我们便有了对颜色对象更多的控制 便可以检测 一个UIColor中 各个分量的值


代码如下:

/* Load the color */

UIColor *steelBlueColor =[UIColor colorWithRed:0.3f green:0.4f blue:0.6f alpha:1.0f];

CGColorRef colorRef = [steelBlueColor CGColor];

const CGFloat *components = CGColorGetComponents(colorRef);

NSUInteger componentsCount = CGColorGetNumberOfComponents(colorRef);

NSUInteger counter = 0;
for (counter = 0; counter < componentsCount; counter++) {

NSLog(@"Component %lu = %.02f",(unsigned long)counter + 1,components[counter]);

}



                    =====In the console run=====

Component 1 = 0.30

Component 2 = 0.40

Component 3 = 0.60

Component 4 = 1.00