gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:13:12 25 4
gpt4 key购买 nike

我希望我网站上的用户能够选择十六进制颜色,我只想为深色显示白色文本,为浅色显示黑色文本。你能用十六进制代码(最好是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com