在 Objective-C 中比较颜色

发布于 2024-08-08 05:15:19 字数 748 浏览 3 评论 0原文

我试图使用 Objective-C 编写的代码来确定两种颜色是否相等。

我正在使用这段代码来确定两种颜色是否等效(当前用于调试目的)

    NSLog(@"currentColor is %@", currentColor);
NSLog(@"Adjacent Color is %@",[[buttonArray objectAtIndex:1] backgroundColor]);
NSLog(@"%i",[[buttonArray objectAtIndex:1] backgroundColor]==currentColor);

我的控制台显示

2009-10-20 00:27:10.814 colorGame[13588:207] currentColor is kCGColorSpaceModelRGB 0 0 1 1 
2009-10-20 00:27:10.815 colorGame[13588:207] Adjacent Color is kCGColorSpaceModelRGB 0 0 1 1 
2009-10-20 00:27:10.815 colorGame[13588:207] 0

我可以根据要求发布更多代码(我不知道是否真的有必要)。当前颜色最初被定义为

UIColor *currentColor;

好像有帮助。

我相当确定我只是进行了错误的比较,并且可能有一些内置方法可以比较颜色,但我只是不知道。

I'm trying to determine if two colors are equivalent, using code written in Objective-C.

I'm using this snippet of code to determine if the two colors are equivalent (currently for debugging purposes)

    NSLog(@"currentColor is %@", currentColor);
NSLog(@"Adjacent Color is %@",[[buttonArray objectAtIndex:1] backgroundColor]);
NSLog(@"%i",[[buttonArray objectAtIndex:1] backgroundColor]==currentColor);

My console is showing

2009-10-20 00:27:10.814 colorGame[13588:207] currentColor is kCGColorSpaceModelRGB 0 0 1 1 
2009-10-20 00:27:10.815 colorGame[13588:207] Adjacent Color is kCGColorSpaceModelRGB 0 0 1 1 
2009-10-20 00:27:10.815 colorGame[13588:207] 0

I can post more code if asked (I don't know if any more is really necessary). Current color was initially defined as

UIColor *currentColor;

if that is any help.

I'm fairly sure I'm just doing the compare wrong, and that there is probably some built-in method that can compare colors, that I'm just not aware of.

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

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

发布评论

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

评论(3

柳絮泡泡 2024-08-15 05:15:19
@implementation UIColor (compare)

- (BOOL) isEqualToColor:(UIColor *) otherColor
{
return CGColorEqualToColor(self.CGColor, otherColor.CGColor);
}

@end

请记住,看起来相同的两种颜色可能返回 TRUE,也可能不返回 TRUE,因为组件保留为浮点数,并且它们的差异可能小于显示硬件可以解析的值。

另请记住,如果它们是在不同的颜色空间中定义的,则此方法将永远不会返回 TRUE。

@implementation UIColor (compare)

- (BOOL) isEqualToColor:(UIColor *) otherColor
{
return CGColorEqualToColor(self.CGColor, otherColor.CGColor);
}

@end

Keep in mind that two colors that look the same may or may not return TRUE, since the components are kept as floats and they may differ by a value that's less than the display hardware can resolve.

Also keep in mind that if they're defined in different color spaces, this method will never return TRUE.

过去的过去 2024-08-15 05:15:19

对象必须使用 isEqual: 方法进行比较,而不是 ==,它只是比较指针地址

objects must be compared using the isEqual: method, not ==, which simply compares the pointer address

影子的影子 2024-08-15 05:15:19

您正在测试对象指针的等效性,这可能永远不会返回 true。如果您想使用实际的颜色值,那么您需要获取底层的 CGColor 引用。

You're testing object pointers for equivalency, which is probably never going to return true. If you want to work with the actual color values then you'll need to get the underlying CGColor reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文