是否有一个 php 函数可以通过给出 rgb 或十六进制颜色作为参数来返回最接近的颜色名称?我已经搜索了很多,但找不到可以完成这项工作的函数。
请帮忙
请参阅下面我的代码。我用它来复制徽标颜色以在运行时自动更改网站主题。希望它有效!
只需将图像 URL 作为函数中的参数传递即可。
function CopyLogoColor($logo_path){
$i = imagecreatefromjpeg($logo_path);
$rTotal = 0;
$gTotal =0;
$bTotal = 0;
$total = 0;
for ( $x=0 ; $x<imagesx($i) ; $x++){
for ( $y=0 ; $y<imagesy($i) ; $y++ ) {
$rgb = imagecolorat($i,$x,$y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8)& 0xFF;
$b = $rgb & 0xFF;
$rTotal += $r;
$gTotal += $g;
$bTotal += $b;
$total++;
}
}
$rAverage = round($rTotal/$total);
$gAverage = round($gTotal/$total);
$bAverage = round($bTotal/$total);
$r = intval($rAverage);
$g = intval($gAverage);
$b = intval($bAverage);
$r = dechex($r<0?0:($r>255?255:$r));
$g = dechex($g<0?0:($g>255?255:$g));
$b = dechex($b<0?0:($b>255?255:$b));
$color = (strlen($r) < 2?'0':'').$r;
$color .= (strlen($g) < 2?'0':'').$g;
$color .= (strlen($b) < 2?'0':'').$b;
return '#'.$color;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)