PHP中的颜色之间的“距离”

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP中的颜色之间的“距离”脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找一个功能,可以准确地表示两种颜色之间的距离作为数字或东西.

例如,我正在寻找一个数组的十六进制值或RGB数组,我想找到一个给定的颜色在数组中最相似的颜色

例如.我传递一个RGB值的函数,并返回数组中最接近的颜色

每个颜色表示为十六进制代码中的元组.要确定近距离匹配,您需要分别减去每个RGB组件.

例:

@H_404_11@Color 1: #112233 Color 2: #122334 Color 3: #000000 Difference between color1 and color2: R=1,G=1 B=1 = 0x3 Difference between color3 and color1: R=11,G=22,B=33 = 0x66 So color 1 and color 2 are closer than 1 and 3.

编辑

那么你想要最接近的命名颜色?使用每种颜色的十六进制值创建一个数组,然后循环并返回名称.这样的东西

@H_404_11@function getColor($rgb) { // these are not the actual rgb values $colors = array(BLUE =>0xFFEEBB,red => 0X103ABD,GREEN => 0x123456); $largestDiff = 0; $closestColor = ""; foreach ($colors as $name => $rgbColor) { if (colorDiff($rgbColor,$rgb) > $largestDiff) { $largestDiff = colorDiff($rgbColor,$rgb); $closestColor = $name; } } return $closestColor; } function colorDiff($rgb1,$rgb2) { // do the math on each tuple // Could use bITwise operates more efficiently but just do strings for Now. $red1 = hexdec(substr($rgb1,2)); $green1 = hexdec(substr($rgb1,2,2)); $blue1 = hexdec(substr($rgb1,4,2)); $red2 = hexdec(substr($rgb2,2)); $green2 = hexdec(substr($rgb2,2)); $blue2 = hexdec(substr($rgb2,2)); return abs($red1 - $red2) + abs($green1 - $green2) + abs($blue1 - $blue2) ; }

脚本宝典总结

以上是脚本宝典为你收集整理的PHP中的颜色之间的“距离”全部内容,希望文章能够帮你解决PHP中的颜色之间的“距离”所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。