检查 UIColor 是暗还是亮? [英] Check if UIColor is dark or bright?

查看:25
本文介绍了检查 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://web.archive.org/web/20100102024448/http://theflashblog.com/?p=173

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

有什么想法吗?

干杯,安德烈

更新

感谢大家的建议,以下是工作代码:

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

如果您只处理黑色或白色文本,请使用上面的颜色亮度计算.如果低于 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.

偏向黑色文本.:)

使用的公式是((红色值* 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天全站免登陆