您的位置:首页 > 其它

CGColor

2016-01-09 04:01 211 查看

前言

CGColor主要用于CoreGraphics框架之中,CGColor其实是个结构体,而我们通常再使用CGColor的时候使用的是他的引用类CGColorRef。CGColor主要由CGColorSpace和Color Components两个部分组成,同样的颜色组成,如果颜色空间不同的话,解析出来的结构可能会有所不同。这就像我们在处理图片数据的时候,如果把RGBA格式当成BGRA格式处理的结果可想而知。在Quartz 2D中CGColor常用来设置context的填充颜色,设置透明度等。

一、创建CGColor

1、CG_EXTERN CGColorRef __nullable CGColorCreate(CGColorSpaceRef __nullable space,

const CGFloat * __nullable components) CG_AVAILABLE_STARTING(__MAC_10_3, __IPHONE_2_0);

参数:

(1)、CGColorSpaceRef __nullable space:指定CGColor对应的颜色空间,Quartz就会retain该对象,因此调用完之后你就可以安全的释放该对象;

(2)、const CGFloat * __nullable components:一个CGFloat的数组,该数组的元素个数是指定色彩空间包含的颜色分量数n,加上对应的alpha值;

2、 CG_EXTERN CGColorRef __nullable CGColorCreateCopy(CGColorRef __nullable color)

CG_AVAILABLE_STARTING(__MAC_10_3, __IPHONE_2_0);

该方法需要一个实例CGColorRef对象;

参数:

CGColorRef __nullable color:一个实例CGColorRef对象;

3、 CG_EXTERN CGColorRef __nullable CGColorCreateCopyByMatchingToColorSpace(__nullable CGColorSpaceRef,

CGColorRenderingIntent intent, CGColorRef __nullable color, __nullable CFDictionaryRef options)

CG_AVAILABLE_STARTING(__MAC_10_11, __IPHONE_9_0);

iOS9.0新增,需研究;

4、 CG_EXTERN CGColorRef __nullable CGColorCreateCopyWithAlpha(CGColorRef __nullable color,

CGFloat alpha) CG_AVAILABLE_STARTING(__MAC_10_3, __IPHONE_2_0);

参数:

(1)、CGColorRef __nullable color:一个实例CGColorRef对象;

(2)、CGFloat alpha:指定复制所需的不透明度;取值0~1;

5、 CG_EXTERN CGColorRef __nullable CGColorCreateWithPattern(CGColorSpaceRef __nullable space,

CGPatternRef __nullable pattern, const CGFloat * __nullable components)

CG_AVAILABLE_STARTING(__MAC_10_3, __IPHONE_2_0);

参数:

(1)、CGColorSpaceRef __nullable space:指定CGColor对应的颜色空间;

(2)、CGPatternRef __nullable pattern:颜色模式;

(3)、const CGFloat * __nullable components:一个CGFloat的数组,该数组的元素个数是指定色彩空间包含的颜色分量数n,加上对应的alpha值;

ps:上述函数返回一个新创建的CGColorRef,当我们不再使用该对象的时候使用CGColorRelease函数释放该对象;

二、获取CGColor数据

1、获取ColorSpace

CG_EXTERN CGColorSpaceRef __nullable CGColorGetColorSpace(CGColorRef __nullable color)

CG_AVAILABLE_STARTING(__MAC_10_3, __IPHONE_2_0);

参数:

CGColorRef __nullable color:要获取ColorSpace的CGColorRef;


例子:

CGColorRef cgColor = [UIColorredColor].CGColor;
CGColorSpaceRef colorSpace = CGColorGetColorSpace(cgColor);
NSLog(@"color space: %@", colorSpace);


打印:

color space: <CGColorSpace 0x7fd3f2607f40> (kCGColorSpaceDeviceRGB)


2、获取ColorComponents

(1)、 获取CGColorRef的中包含的颜色组成部分的个数

CG_EXTERN size_t CGColorGetNumberOfComponents(CGColorRef __nullable color) CG_AVAILABLE_STARTING(__MAC_10_3, __IPHONE_2_0);

参数:

CGColorRef __nullable color:要获取颜色组成部分的个数的CGColorRef;


(2)、获取实际的颜色组成部分的数组

CG_EXTERN const CGFloat * __nullable CGColorGetComponents(CGColorRef __nullable color) CG_AVAILABLE_STARTING(__MAC_10_3, __IPHONE_2_0);

参数:

CGColorRef __nullable color:要获取实际的颜色组成部分的数组的CGColorRef;


例如:

CGColorRef cgColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5].CGColor;
NSUInteger num = CGColorGetNumberOfComponents(cgColor);
NSLog(@"color components num:%ld",num);

const CGFloat *colorComponents = CGColorGetComponents(cgColor);
for (int i = 0; i < num; ++i) {
NSLog(@"color components %d: %f", i, colorComponents[i]);
}


打印:

color components num:4
color components 0: 1.000000
color components 1: 1.000000
color components 2: 1.000000
color components 3: 0.500000


3、获取alpha

CG_EXTERN CGFloat CGColorGetAlpha(CGColorRef __nullable color) CG_AVAILABLE_STARTING(__MAC_10_3, __IPHONE_2_0);

例如:

CGColorRef cgColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5].CGColor;
CGFloat alpha = CGColorGetAlpha(cgColor);
NSLog(@"color components alpha:%f",alpha);


打印:

color components alpha:0.500000


4、获取Pattern

CG_EXTERN CGPatternRef __nullable CGColorGetPattern(CGColorRef __nullable color)

CG_AVAILABLE_STARTING(__MAC_10_3, __IPHONE_2_0);

三、其他

1、retain

CG_EXTERN CGColorRef __nullable CGColorRetain(CGColorRef __nullable color) CG_AVAILABLE_STARTING(__MAC_10_3, __IPHONE_2_0);

2、release

CG_EXTERN void CGColorRelease(CGColorRef __nullable color) CG_AVAILABLE_STARTING(__MAC_10_3, __IPHONE_2_0);

3、判断颜色是否相等

CG_EXTERN bool CGColorEqualToColor(CGColorRef __nullable color1, CGColorRef __nullable color2) CG_AVAILABLE_STARTING(__MAC_10_3, __IPHONE_2_0);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: