检查UIColor是否黑暗或明亮? [英] Check if UIColor is dark or bright?

查看:171
本文介绍了检查UIColor是否黑暗或明亮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定一个选定的UIColor(由用户选择)是黑暗还是明亮,因此我可以更改位于该颜色顶部的一行文本的颜色,以提高可读性。

I need to determine whether a selected UIColor (picked by the user) is dark or bright, so I can change the color of a line of text that sits on top of that color, for better readability.

以下是Flash / Actionscript(带演示)的示例:
http://theflashblog.com/?p=173

Here's an example in Flash/Actionscript (with demo): http://theflashblog.com/?p=173

任何想法?

Cheers,
Andre

Cheers, Andre

UPDATE

感谢大家的建议,这里是工作代码:

Thanks to everyone's suggestions, here's the working code:

- (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");
    }
}

再次感谢:)

推荐答案

W3C具有以下功能:
http://www.w3.org/WAI/ER/WD-AERT/#color-contrast

W3C has the following: http://www.w3.org/WAI/ER/WD-AERT/#color-contrast

如果你只是做黑色或白色文字,请使用上面的颜色亮度计算。如果低于125,使用白色文本。如果为125或以上,请使用黑色文字。

If you're only doing black or white text, use the color brightness calculation above. If it is below 125, use white text. If it is 125 or above, use black text.

编辑1:偏向黑色文字。 :)

edit 1: bias towards black text. :)

编辑2:使用的公式是((红色值* 299)+(绿色值* 587)+(蓝色值* 114))/ 1000。

edit 2: The formula to use is ((Red value * 299) + (Green value * 587) + (Blue value * 114)) / 1000.

这篇关于检查UIColor是否黑暗或明亮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆