gpt4 book ai didi

iphone - 如何正确获取 CGColor 的颜色分量

转载 作者:行者123 更新时间:2023-12-03 18:37:27 24 4
gpt4 key购买 nike

我有一个黑色的 UILabel;

我正在编写以下代码来获取黑色的分量。

UIColor *aColor = [aLabel.textColor retain];
const CGFloat* components = CGColorGetComponents(aColor.CGColor);
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();

但总是给出绿色分量而不是黑色分量;

有人对此有想法吗?

我是否传递了不同的色彩空间?

:-(请帮助我。

最佳答案

很可能是错误的色彩空间。我猜想灰度色彩空间的 alpha 分量达到了您认为绿色分量应该在的位置。
我使用这个函数从 UIColor 创建一个字符串,我只遇到 RGB 和灰度色彩空间,所以我只是将少于 4 个 (R+G+B+A) 分量的每种颜色解释为灰度。

if (CGColorGetNumberOfComponents(color.CGColor) < 4) {
const CGFloat *components = CGColorGetComponents(color.CGColor);
color = [UIColor colorWithRed:components[0] green:components[0] blue:components[0] alpha:components[1]];
}
if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {
NSLog(@"no rgb colorspace");
// do seomthing
}
const CGFloat *components = CGColorGetComponents(color.CGColor);
NSString *colorAsString = [NSString stringWithFormat:@"%f,%f,%f,%f", components[0], components[1], components[2], components[3]];

当然这个方法并不适用于所有情况,您应该根据您的需求采用它。

关于iphone - 如何正确获取 CGColor 的颜色分量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4155642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com