php - 十六进制代码亮度 PHP?

标签 php colors hex brightness

我希望我网站上的用户能够选择十六进制颜色,我只想为深色显示白色文本,为浅色显示黑色文本。你能用十六进制代码(最好是 PHP)计算出亮度吗?

最佳答案

$hex = "78ff2f"; //Bg color in hex, without any prefixing #!

//break up the color in its RGB components
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));

//do simple weighted avarage
//
//(This might be overly simplistic as different colors are perceived
// differently. That is a green of 128 might be brighter than a red of 128.
// But as long as it's just about picking a white or black text color...)
if($r + $g + $b > 382){
    //bright color, use dark font
}else{
    //dark color, use bright font
}

关于php - 十六进制代码亮度 PHP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3015116/

相关文章:

python - 如何在 IDLE 的终端中打印彩色文本?

delphi - 在delphi中更改字段值的DBGRID行颜色

windows - 最小化字符数的简单数字到数字(或数字到十六进制)加密算法

objective-c - 将 NSString 转换为字符数组

PHP file_put_contents : Create new file after 250, 000行代码

php - 在 PHP while 循环中创建电子邮件正文变量

php - PHP header_remove 的可能替代方案

php - MySQL 显示图像

bash - 如何用十六进制数字对 CSV 文件进行排序?

java - 我有一个变量,但 Java 将它们视为两个