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);相关文章推荐
- zend引擎中php扩展返回值的宏
- 02_spring通过xml获取ApplicationContext
- Java设计模式(Design Patterns In Java)读书摘要——第1章 绪论
- 声明php内核zend中的参数小结
- zend中常见的宏定义操作
- php内核中与线程相关的操作
- 138_二分搜索 (lower_bound)
- 01_spring基本命名空间及各种类型注入方式
- 每秒千万网络包的线速 SYN/DNS flooding
- 如何实践设计原则
- vim 基础学习之可视模式
- mybatis缓存机制
- mybatis延迟加载
- Codeforces 615D Multipliers 【组合数学】
- Codeforces 615B Longtail Hedgehog 【dp】
- 基于Python SimpleHTTPServer.py的修改脚本:HTTP文件服务器,修正中文目录列表,支持视频文件在线播放
- hdoj 4578 Transformation 【线段树 区间加、乘、修改、幂次求和】
- Codeforces 615A Bulbs 【水题】
- hdoj 4614 Vases and Flowers 【线段树 + 二分】
- hdoj 4553 约会安排 【线段树区间合并】