php函数十六进制或RGB颜色到颜色的名称

编程入门 行业动态 更新时间:2024-10-09 18:25:43
本文介绍了php函数十六进制或RGB颜色到颜色的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有一个php函数通过将rgb或hex颜色作为参数来返回最接近的colorname?我已经苦了很多,但是找不到能完成这项工作的功能.

Is there a php function that return the closest colorname by give the rgb or hex color as parameter? I have seared a lot but can't find a function that does that job.

请帮助

推荐答案

请参阅下面的我的代码".我使用它来复制徽标颜色,以在运行时自动更改网站主题.希望它能起作用!

See my Code below. I use it to copy Logo Color to change the site theme automatically at run-time. Hope it works!

只需将图像URL作为参数传递给函数.

Simply pass the image URL as parameter in the function.

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; }

更多推荐

php函数十六进制或RGB颜色到颜色的名称

本文发布于:2023-06-10 18:42:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/616560.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:颜色   函数   名称   php   RGB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!