macOS 开发 - CGColor

CGColor


简述

CGColor 属于 CoreGraphics 框架

CGColor其实是个结构体,而我们通常在使用的CGColor的时候使用的是它的引用类型CGColorRef。

CGColor 主要由CGColorSapce和Color Components两个部分组成,同样的颜色组成,如果颜色空间不同的话,解析出来的结果可能会有所不同。


官方介绍翻译

A set of components that define a color, with a color space specifying how to interpret them.

CGColor is the fundamental data type used internally by Core Graphics to represent colors. CGColor objects, and the functions that operate on them, provide a fast and convenient way of managing and setting colors directly, especially colors that are reused (such as black for text).

A color object contains a set of components (such as red, green, and blue) that uniquely define a color, and a color space that specifies how those components should be interpreted.

Color objects provide a fast and convenient way to manage and set colors, especially colors that are used repeatedly. Drawing operations use color objects for setting fill and stroke colors, managing alpha, and setting color with a pattern.

CGColor is derived from CFTypeRef and inherits the properties that all Core Foundation types have in common.

一系列组件来定义一个颜色,包括颜色空间来定义如何解释他们。

CGColor 是Core Graphics 框架用来描述色彩的基本数据类型。CGColor 对象和方法提供一个快速和遍历的方法去直接管理和设置颜色,特别是被重用的颜色,比如文字的黑色。

一个颜色对象包含一系列组件(比如红绿蓝)来定义一个颜色,一个颜色空间指定这些组件如何演绎。

色彩对象提供了快速和遍历的方法来设置颜色,特别是常备冲用的颜色。画图方法使用颜色对象来填充颜色、管理alpha、使用模式设置颜色。

CGColor 来自 CFTypeRef,拥有 Core Foundation types 的公共属性。


常见对象

  • CGColorConversionInfoRef

An object that describes how to convert between color spaces for use by other system services.

使用其他系统服务时,用来描述颜色空间之间转换的对象。

  • CGColorSpaceRef

A profile that specifies how to interpret a color value for display.

用来定义如何演绎颜色值来展示。

  • CGFontRef

A set of character glyphs and layout information for drawing text.

绘制文字时的一组字形和布局信息


方法

1、创建颜色 CGColorRef

/* 使用颜色空间和组件来创建一个颜色,颜色空间可以是任何非pattern 空间的空间*/
CGColorRef CGColorCreate(CGColorSpaceRef cg_nullable space,
                                              const CGFloat * cg_nullable components);

//在颜色空间中创建一个 颜色,空间必须是pattern空间。
CGColorRef CGColorCreateWithPattern(CGColorSpaceRef cg_nullable space,
                                                         CGPatternRef cg_nullable pattern, const CGFloat * cg_nullable components);

//复制一份color
CGColorRef CGColorCreateCopy(CGColorRef cg_nullable color);

//复制一份color,并设置alpha
CGColorRef CGColorCreateCopyWithAlpha(CGColorRef cg_nullable color,CGFloat alpha);

/* Create a copy of `color' by matching existing color to destination color space. */
//按照已存在的颜色样式,创建颜色到指定的颜色空间。
CGColorRef CGColorCreateCopyByMatchingToColorSpace(cg_nullable CGColorSpaceRef,


//创建一份 gray 颜色空间的颜色
CGColorRef  CGColorCreateGenericGray(CGFloat gray, CGFloat alpha);

//创建一份 RGB 颜色空间的颜色
CGColorRef  CGColorCreateGenericRGB(CGFloat red, CGFloat green,
                                              CGFloat blue, CGFloat alpha);

//创建一份 CMYK 颜色空间的颜色
CGColorRef  CGColorCreateGenericCMYK(CGFloat cyan, CGFloat magenta,
                                               CGFloat yellow, CGFloat black, CGFloat alpha);

2、获取颜色数据


//返回颜色常量,不是拷贝或者create。
//不会每次都返回新的值,所以,不应该每次都释放返回值。但是,CGColorGetConstantColor 返回的颜色可以在内嵌的属性中被retained 和 released,和其他CF 类型值一样。
CGColorRef CGColorGetConstantColor(CFStringRef cg_nullable colorName);

                                                                        CGColorRenderingIntent intent, CGColorRef cg_nullable color, __nullable CFDictionaryRef options)
CG_AVAILABLE_STARTING(__MAC_10_11, __IPHONE_9_0);

//等同于 `CFRetain(color)',CFRetain 中color为NULL时会崩溃,CGColorRetain 不会崩溃。
CGColorRef CGColorRetain(CGColorRef cg_nullable color);


//等同于 `CFRelease(color)',CFRelease 中color为NULL时会崩溃,CGColorRelease 不会崩溃。
void CGColorRelease(CGColorRef cg_nullable color);

//true-如果两个颜色相等
bool CGColorEqualToColor(CGColorRef cg_nullable color1, CGColorRef cg_nullable color2);

//返回颜色成分个数(包含透明度)
size_t CGColorGetNumberOfComponents(CGColorRef cg_nullable color);

//返回颜色成分(包含透明度)
const CGFloat * __nullable CGColorGetComponents(CGColorRef cg_nullable color);

//返回颜色的透明度
CGFloat CGColorGetAlpha(CGColorRef cg_nullable color);

//返回指定颜色的颜色空间
CGColorSpaceRef __nullable CGColorGetColorSpace(CGColorRef cg_nullable color);

//如果颜色在模式的颜色空间,返回颜色样式
CGPatternRef __nullable CGColorGetPattern(CGColorRef cg_nullable color);

//返回颜色的 CFTypeID
CFTypeID CGColorGetTypeID(void);

3、获取系统颜色


//CGColorGetConstantColor 中的颜色名字
//灰色颜色空间的颜色
const CFStringRef  kCGColorWhite;

const CFStringRef  kCGColorBlack;

const CFStringRef  kCGColorClear;

参考

https://blog.csdn.net/xiaoxiaobukuang/article/details/50486288

猜你喜欢

转载自blog.csdn.net/lovechris00/article/details/81104215