gpt4 book ai didi

PHP - 从具有十六进制颜色的数组中获取最亮的十六进制颜色

转载 作者:行者123 更新时间:2023-12-04 11:01:15 25 4
gpt4 key购买 nike

现在我一直在网上搜索如何做到这一点,但似乎没有一个问题/答案。

我正在使用脚本从给定图像中获取 6 种主要颜色。

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

$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);
echo '<img src="' . $img . '" />';
echo '<table>';
foreach($palette as $color) {
echo '<tr><td style="background:#' . $color . '; width:36px;"></td><td>#' . $color . '</td></tr>';
}
echo '</table>';

这将输出一个调色板:

enter image description here

我想用这些颜色做的是找出哪个是最亮的,也就是主色,在这种情况下是第二种颜色“CC0000”。当然不包括白色#FFFFFF 和黑色#000000。

我的问题是, 如何从 PHP 中的一系列颜色中获得最亮的颜色?

最佳答案

如果我理解正确,那么也许您可以像这样解决问题?

function detectColors( $image, $num, $level = 5 ) {
$level = (int)$level;
$palette = array();
$details = array();# store the count of non black or white colours here ( see $exclusions )

list( $width, $height, $type, $attr )=getimagesize( $image );
if( !$type ) return FALSE;

switch ( image_type_to_mime_type( $type ) ) {
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;

/* Colours to not factor into dominance statistics */
$exclusions=['000000','FFFFFF'];

for( $i = 0; $i < $width; $i += $level ) {
for( $j = 0; $j < $height; $j += $level ) {
$colour = imagecolorat( $img, $i, $j );
$rgb = imagecolorsforindex( $img, $colour );
$key = sprintf('%02X%02X%02X', ( round( round( ( $rgb['red'] / 0x33 ) ) * 0x33 ) ), round(round(($rgb['green'] / 0x33)) * 0x33), round(round(($rgb['blue'] / 0x33)) * 0x33));
$palette[ $key ] = isset( $palette[ $key ] ) ? ++$palette[ $key ] : 1;

if( !in_array( $key, $exclusions ) ){
/* add count of any non excluded colours */
$details[ $key ] = isset( $details[ $key ] ) ? ++$details[ $key ] : 1;
}
}
}
arsort( $palette );

/* prepare statistics for output */
$output=new stdclass;
$output->data=array_slice( array_keys( $palette ), 0, $num );
$output->highest=max( $details );
$output->lowest=min( $details );
$output->dominant=array_search( $output->highest, $details );
$output->recessive=array_search( $output->lowest, $details );


return $output;
}



$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 );


$html=[];
foreach( $palette->data as $colour ) $html[]=sprintf( '<tr><td style="background:#%1$s; width:200px; height:1rem; " >#%1$s</td></tr>', $colour );

printf('
<p>Analysing: %1$s</p>
<img src="%1$s" />
<table>%2$s</table>
<ul>
<li>Dominant colour: %3$s - count: %4$d</li>
<li>Recessive colour: %5$s - count: %6$d</li>
</ul>',
$img,
implode( PHP_EOL, $html ),
$palette->dominant,
$palette->highest,
$palette->recessive,
$palette->lowest
);

输出:
Dominant colour: CC0000 - count: 3032
Recessive colour: 333366 - count: 1

这或多或少是你问的吗?

关于PHP - 从具有十六进制颜色的数组中获取最亮的十六进制颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58780368/

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