CGColorGetComponents() 返回什么?

发布于 2024-07-18 11:58:28 字数 200 浏览 11 评论 0原文

CGFloat* colors = CGColorGetComponents(hsbaColor.CGColor);

它返回一个浮点数,还是一个浮点数数组? 看起来星号是创建数组的简写。 那是对的吗?

当我在 HSB UIColor 对象的 CGColor 属性上调用此函数时,它会将值转换为 RGB 吗?

CGFloat* colors = CGColorGetComponents(hsbaColor.CGColor);

Does this return a float, or an array of floats? It looks like the asterisk is shorthand for creating an array. Is that correct?

When I call this function on the CGColor property of an HSB UIColor object does it convert the values to RGB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花开雨落又逢春i 2024-07-25 11:58:28
CGFloat* 颜色 = CGColorGetComponents(hsbaColor.CGColor); 
  

它返回一个浮点数还是浮点数数组? 看起来星号是创建数组的简写。 这是正确的吗?

有点。

CGFloat *colors 声明一个变量,该变量保存指向至少一个 CGFloat 的指针。 CGColorGetComponents 返回一个指向多个 CGFloat 的指针,一个接一个地 — 一个 C 数组。 您获取该指针并将其分配给(将指针放入)colors 变量。

声明变量不会创建数组。 事实上,CGColorGetComponents 也没有。 无论创建 CGColor 对象,都会创建数组并将其存储在对象内; CGColorGetComponents 让您拥有指向该存储的指针。

声明 CGFloat *colors 变量仅创建一个位置(变量)来存储指向一个或多个 CGFloats 的指针。 变量中的东西是指针,而指针处的东西是数组。

如果仍然不清楚,请参阅有关 C 中指针的所有信息

CGFloat* colors = CGColorGetComponents(hsbaColor.CGColor);

Does this return a float, or an array of floats? It looks like the asterisk is shorthand for creating an array. Is that correct?

Sort of.

CGFloat *colors declares a variable holding a pointer to at least one CGFloat. CGColorGetComponents returns a pointer to several CGFloats, one after the other—a C array. You take that pointer and assign it to (put the pointer in) the colors variable.

Declaring the variable does not create the array. In fact, neither does CGColorGetComponents. Whatever created the CGColor object created the array and stored it inside the object; CGColorGetComponents lets you have the pointer to that storage.

Declaring the CGFloat *colors variable creates only a place—the variable—to store a pointer to one or more CGFloats. The thing in the variable is the pointer, and the thing at that pointer is the array.

If this is still unclear, see Everything you need to know about pointers in C.

甜扑 2024-07-25 11:58:28

是的,它返回一个 CGFloats 数组。 具体来说,它返回“与指定颜色关联的颜色分量(包括 alpha)的强度值数组”。

返回的颜色分量取决于传递的 CGColorRef 使用的颜色空间。

更多信息可以在 CGColor 文档

Yes, it returns an array of CGFloats. Specifically, it returns "an array of intensity values for the color components (including alpha) associated with the specified color."

The color components returned depend on what color space the passed CGColorRef uses.

More information can be found in the CGColor documentation.

来自 Apple

它返回颜色的值与 Quartz 颜色相关的组件(包括 Alpha)。 与关联的颜色分量(包括 alpha)的强度值数组
指定的颜色。 数组的大小比颜色的颜色空间分量数多 1。

From Apple:

It returns the values of the color components (including alpha) associated with a Quartz color. 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.

硪扪都還晓 2024-07-25 11:58:28

为了获取 RGB 分量,我使用:

// Get UIColor's RGB normalized components (0..1)
CGFloat red, green, blue, alpha;
[color getRed:&red green:&green blue:&blue alpha:&alpha];

// Convert RGB components to 8-bit values (0..255)
int r = (int)(red * 255.0);
int g = (int)(green * 255.0);
int b = (int)(blue * 255.0);

To get RGB components I use:

// Get UIColor's RGB normalized components (0..1)
CGFloat red, green, blue, alpha;
[color getRed:&red green:&green blue:&blue alpha:&alpha];

// Convert RGB components to 8-bit values (0..255)
int r = (int)(red * 255.0);
int g = (int)(green * 255.0);
int b = (int)(blue * 255.0);
瞎闹 2024-07-25 11:58:28

下面是如何正确将 CGColorRef myColorRef 转换为 NSColor myNSColor 的示例:

NSColorSpace *cp = [[NSColorSpace alloc] initWithCGColorSpace:CGColorGetColorSpace(myColorRef)];
const CGFloat *components = CGColorGetComponents(myColorRef);
size_t componentCount = CGColorGetNumberOfComponents(myColorRef);
NSColor* myNSColor = [NSColor colorWithColorSpace:cp components:components count:componentCount];

Here's an example of how you can correctly convert a CGColorRef myColorRef to an NSColor myNSColor:

NSColorSpace *cp = [[NSColorSpace alloc] initWithCGColorSpace:CGColorGetColorSpace(myColorRef)];
const CGFloat *components = CGColorGetComponents(myColorRef);
size_t componentCount = CGColorGetNumberOfComponents(myColorRef);
NSColor* myNSColor = [NSColor colorWithColorSpace:cp components:components count:componentCount];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文