【发布时间】:2011-01-31 08:29:27
【问题描述】:
我需要确定选定的 UIColor(由用户选择)是暗色还是亮色,因此我可以更改位于该颜色之上的一行文本的颜色,以提高可读性。
以下是 Flash/Actionscript 中的示例(带演示): http://web.archive.org/web/20100102024448/http://theflashblog.com/?p=173
有什么想法吗?
干杯, 安德烈
更新
感谢大家的建议,下面是工作代码:
- (void) updateColor:(UIColor *) newColor
{
const CGFloat *componentColors = CGColorGetComponents(newColor.CGColor);
CGFloat colorBrightness = ((componentColors[0] * 299) + (componentColors[1] * 587) + (componentColors[2] * 114)) / 1000;
if (colorBrightness < 0.5)
{
NSLog(@"my color is dark");
}
else
{
NSLog(@"my color is light");
}
}
再次感谢:)
【问题讨论】:
-
好像有些颜色没有3个组件,比如UIColor.black。
标签: ios iphone xcode colors uicolor