这是可能检测到的颜色是浅色还是深色? [英] Is this possible to detect a colour is a light or dark colour?

查看:251
本文介绍了这是可能检测到的颜色是浅色还是深色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下两个粉红色方块:

Consider these two pink square:

并且:

你可能知道,一个更轻,一个更暗或更尖锐。
问题是,我可以用人眼看出来,但是这是否可以使用系统方式或程序方式来检测这些信息?至少,这是否可能有一个值告诉我,颜色更像是白色还是颜色不像白色? (假设我得到了那种颜色的RGB代码。)谢谢。

As you may know, one is lighter and one is darker or more sharp. The problem is, I can tell it by human eyes, but is this possible to use a system way or programme way to detect this information? At least, is this possible to have a value that tell me that colour is more like white or that colour is less like white? (Assume I got the RGB code of that colour.) Thanks.

推荐答案

因为你没有指定任何特定的语言/要检测更深/更浅的十六进制,我想为这个

Since you haven't specified any particular language/script to detect the darker/lighter hex, I would like to contribute a PHP solution to this

演示

$color_one = "FAE7E6"; //State the hex without #
$color_two = "EE7AB7";

function conversion($hex) {
    $r = hexdec(substr($hex,0,2)); //Converting to rgb
    $g = hexdec(substr($hex,2,2));
    $b = hexdec(substr($hex,4,2));

    return $r + $g + $b; //Adding up the rgb values
}

echo (conversion($color_one) > conversion($color_two)) ? 'Color 1 Is Lighter' : 'Color 1 Is Darker';
//Comparing the two converted rgb, the greater one is darker






正如有些人,我已经修改了我的功能,以产生更好/准确的结果... (增加亮度)


As pointed out by @Some Guy, I have modified my function for yielding a better/accurate result... (Added Luminance)

function conversion($hex) {
    $r = 0.2126*hexdec(substr($hex,0,2)); //Converting to rgb and multiplying luminance
    $g = 0.7152*hexdec(substr($hex,2,2));
    $b = 0.0722*hexdec(substr($hex,4,2));

    return $r + $g + $b;
}

演示2

这篇关于这是可能检测到的颜色是浅色还是深色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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