PHP从十六进制颜色数组中筛选最亮颜色(排除黑白)
解决方法:从颜色数组中筛选最亮颜色(排除黑白)
要实现这个需求,我们可以通过计算颜色的相对亮度来找出最亮的颜色,同时先排除白色(#FFFFFF)和黑色(#000000)。推荐使用WCAG(网页内容无障碍指南)的相对亮度公式,因为它考虑了人眼对不同颜色的敏感度(对绿色最敏感,蓝色最不敏感),比简单的RGB平均值更准确。
步骤分解
- 过滤无效颜色:先从颜色数组中移除
FFFFFF和000000。 - 计算每个颜色的亮度:使用WCAG公式转换并计算亮度值。
- 找出亮度最高的颜色:遍历过滤后的数组,记录亮度最大的颜色。
完整代码实现
首先添加计算亮度和筛选最亮颜色的函数,再结合你现有的颜色提取脚本:
function detectColors($image, $num, $level = 5) { $level = (int) $level; $palette = array(); $size = getimagesize($image); if (!$size) { return FALSE; } switch ($size['mime']) { case 'image/jpeg': $img = imagecreatefromjpeg($image); break; case 'image/png': $img = imagecreatefrompng($image); break; case 'image/gif': $img = imagecreatefromgif($image); break; default: return FALSE; } if (!$img) { return FALSE; } for ($i = 0; $i < $size[0]; $i += $level) { for ($j = 0; $j < $size[1]; $j += $level) { $thisColor = imagecolorat($img, $i, $j); $rgb = imagecolorsforindex($img, $thisColor); $color = sprintf('%02X%02X%02X', round(round(($rgb['red'] / 0x33)) * 0x33), round(round(($rgb['green'] / 0x33)) * 0x33), round(round(($rgb['blue'] / 0x33)) * 0x33) ); $palette[$color] = isset($palette[$color]) ? ++$palette[$color] : 1; } } arsort($palette); return array_slice(array_keys($palette), 0, $num); } // 新增:计算颜色相对亮度并筛选最亮颜色的函数 function getBrightestColor($colors) { // 排除白色和黑色 $filteredColors = array_filter($colors, function($hexColor) { return $hexColor !== 'FFFFFF' && $hexColor !== '000000'; }); // 如果过滤后没有颜色,返回null if (empty($filteredColors)) { return null; } $brightestHex = null; $maxLuminance = -1; foreach ($filteredColors as $hex) { // 将十六进制颜色转换为0-1范围的RGB值 $r = hexdec(substr($hex, 0, 2)) / 255; $g = hexdec(substr($hex, 2, 2)) / 255; $b = hexdec(substr($hex, 4, 2)) / 255; // 应用WCAG亮度转换公式 $r = $r > 0.03928 ? pow(($r + 0.055) / 1.055, 2.4) : $r / 12.92; $g = $g > 0.03928 ? pow(($g + 0.055) / 1.055, 2.4) : $g / 12.92; $b = $b > 0.03928 ? pow(($b + 0.055) / 1.055, 2.4) : $b / 12.92; // 计算最终相对亮度(范围0-1,值越大越亮) $luminance = 0.2126 * $r + 0.7152 * $g + 0.0722 * $b; // 更新最亮颜色记录 if ($luminance > $maxLuminance) { $maxLuminance = $luminance; $brightestHex = $hex; } } return $brightestHex; } // 调用示例 $img = 'https://gs2-sec.ww.prod.dl.playstation.net/gs2-sec/appkgo/prod/CUSA03041_00/15/i_2efe1b71a037233f60cec3b41a18d69c02bcff5fd0c895c212d44f37883dbaf8/i/icon0.png'; $palette = detectColors($img, 6, 5); $brightestColor = getBrightestColor($palette); // 输出结果 echo '<img src="' . $img . '" />'; echo '<table>'; foreach($palette as $color) { $isBrightest = $color === $brightestColor ? ' style="font-weight:bold;"' : ''; echo '<tr><td style="background:#' . $color . '; width:36px;"></td><td' . $isBrightest . '>#' . $color . '</td></tr>'; } echo '</table>'; if ($brightestColor) { echo '<p>最亮颜色:<span style="color:#' . $brightestColor . '; font-weight:bold;">#' . $brightestColor . '</span></p>'; } else { echo '<p>没有符合条件的颜色(已排除黑白)</p>'; }
关键说明
- WCAG亮度公式:这个公式是行业标准,能更精准地反映人眼感知的亮度,比如相同RGB值的绿色会比红色看起来更亮,公式会体现这一点。
- 过滤逻辑:通过
array_filter直接移除黑白两种颜色,确保结果符合你的需求。 - 边界处理:如果过滤后没有剩下任何颜色,函数会返回
null,避免报错。
内容的提问来源于stack exchange,提问作者Appel Flap
相关产品推荐
相关产品推荐

