PHP-使用十六进制颜色从数组中获取Brighttest十六进制颜色
现在我一直在网上搜索如何做到这一点,但似乎没有一个问题/答案 我正在使用脚本从给定的图像中获取6种主要颜色PHP-使用十六进制颜色从数组中获取Brighttest十六进制颜色,php,colors,Php,Colors,现在我一直在网上搜索如何做到这一点,但似乎没有一个问题/答案 我正在使用脚本从给定的图像中获取6种主要颜色 function detectColors($image, $num, $level = 5) { $level = (int) $level; $palette = array(); $size = getimagesize($image); if (!$size) { return FALSE; } switch
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>';
函数检测颜色($image,$num,$level=5)
{
$level=(int)$level;
$palete=array();
$size=getimagesize($image);
如果(!$size){
返回FALSE;
}
开关($size['mime'])){
案例“图像/jpeg”:
$img=imagecreatefromjpeg($image);
打破
案例“image/png”:
$img=imagecreatefrompng($image);
打破
案例“image/gif”:
$img=imagecreatefromformgif($image);
打破
违约:
返回FALSE;
}
如果(!$img){
返回FALSE;
}
对于($i=0;$i<$size[0];$i+=$level){
对于($j=0;$j<$size[1];$j+=$level){
$thisColor=imagecolorat($img,$i,$j);
$rgb=imagecolorsforindex($img,$thisColor);
$color=sprintf(“%02X%02X%02X”),(圆形(圆形($rgb['red']/0x33))*0x33)),圆形(圆形($rgb['green']/0x33))*0x33),圆形(圆形($rgb['blue']/0x33))*0x33));
$palete[$color]=isset($palete[$color])?+$palete[$color]:1;
}
}
arsort($);
返回数组切分(数组切分键($palette),0,$num);
}
$img='1https://gs2-sec.ww.prod.dl.playstation.net/gs2-sec/appkgo/prod/CUSA03041_00/15/i_2efe1b71a037233f60cec3b41a18d69c02bcff5fd0c895c212d44f37883dbaf8/i/icon0.png';
$palette=DetectColor($img,6,5);
回声';
回声';
foreach($调色板为$颜色){
回声“#”.$color.”;
}
回声';
这将输出一个调色板:
我想用这些颜色做的是找出哪一种是最好的,也就是主色,在这个例子中是第二种颜色“CC0000”。当然不包括白人和黑人
我的问题是,如何从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
);
这或多或少是你所问的吗?好的,十六进制颜色从最暗到最亮,分别为012345679ABCDEF,它们分为2个字符,代表红绿蓝,这就是为什么RGB颜色值由6个字符组成 因此,您需要一个函数来比较这些值并返回最高值。我不使用and array,但您可以使用此函数一次比较2个值,并将其调整为使用数组
function GetBrightest($first_color,$Second_color) {
//echo $first_color;
//remove the hash #
$first = $a = str_replace('#', '', $first_color);
$second = $a = str_replace('#', '', $Second_color);
$split_first = str_split($first, 2);
$split_second = str_split($second, 2);
print_r( $split_first);
print_r( $split_second);
//is red brighter
$r = ( $split_first[0] > $split_second[0] ? 1: 0);
$g = ( $split_first[1] > $split_second[1] ? 1: 0);
$b = ( $split_first[2] > $split_second[2] ? 1: 0);
//add the booleans
$t = $r+$g+$b;
if($t > 2 )
{
return $first_color;
}
else
{
return $Second_color;
}
} $Color = GetBrightest('#aa4444','#ff3333'); echo $Color;
我在这里所做的比较是一个粗略的比较,因此您可以改进该方法,因为3个数组值中的2个可能更大,但是如果一种颜色的差异很大,则会使其更亮
此函数中的FF0000和991111将返回第二种颜色,因为绿色和蓝色更亮,而红色在第一个值中非常亮
所以你可以从那里提炼它 这有用吗?是的,这正是我要找的!是否可以修改此代码,使其只扫描主色,而不循环多次?我认为这是不可能的-毕竟你不知道(或脚本不知道)没有分析的主色,所以我可以在不检查每个像素的情况下看到任何推断未知的捷径谢谢你的回答和解释,JonoJames!RamRaider的答案更多的是为了解决我一直试图用它来实现的问题,但是你的回答肯定会增加这个问题的价值。再次感谢!
function GetBrightest($first_color,$Second_color) {
//echo $first_color;
//remove the hash #
$first = $a = str_replace('#', '', $first_color);
$second = $a = str_replace('#', '', $Second_color);
$split_first = str_split($first, 2);
$split_second = str_split($second, 2);
print_r( $split_first);
print_r( $split_second);
//is red brighter
$r = ( $split_first[0] > $split_second[0] ? 1: 0);
$g = ( $split_first[1] > $split_second[1] ? 1: 0);
$b = ( $split_first[2] > $split_second[2] ? 1: 0);
//add the booleans
$t = $r+$g+$b;
if($t > 2 )
{
return $first_color;
}
else
{
return $Second_color;
}
} $Color = GetBrightest('#aa4444','#ff3333'); echo $Color;