大家好,
我想对颜色进行排序,以便它们井井有条(以相似的颜色组合在一起的方式)
我在论坛的某个地方找到了这个函数“rgb2hsl”(对不起,如果我不记得链接了)将 RGB 值转换为 HSL,然后按 H、S、L 排序。
结果还可以,但远不能令人满意,因为一些浅色与深色混合在一起:
以下是 php 中的整个脚本:您能否帮助调整函数以更好地对颜色进行分组?
提前致谢。
<?php
function rgb2hsl ($R, $G, $B)
{
$var_R = ($R / 255);
$var_G = ($G / 255);
$var_B = ($B / 255);
$var_Min = min($var_R, $var_G, $var_B);
$var_Max = max($var_R, $var_G, $var_B);
$del_Max = $var_Max - $var_Min;
$V = $var_Max;
if ($del_Max == 0)
{
$H = 0;
$S = 0;
}
else
{
$S = $del_Max / $var_Max;
$del_R = ( ( ( $var_Max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
$del_G = ( ( ( $var_Max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
$del_B = ( ( ( $var_Max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
if ($var_R == $var_Max) $H = $del_B - $del_G;
else if ($var_G == $var_Max) $H = ( 1 / 3 ) + $del_R - $del_B;
else if ($var_B == $var_Max) $H = ( 2 / 3 ) + $del_G - $del_R;
if ($H<0) $H++;
if ($H>1) $H--;
}
$HSL = array();
$HSL['H'] = $H;
$HSL['S'] = $S;
$HSL['L'] = $V;
return $HSL;
}
function hex2rgb( $colour )
{
if ( $colour[0] == '#' )
{
$colour = substr( $colour, 1 );
}
if ( strlen( $colour ) == 6 ) {
list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5] );
} elseif ( strlen( $colour ) == 3 ) {
list( $r, $g, $b ) = array( $colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2] );
} else {
return false;
}
$r = hexdec( $r );
$g = hexdec( $g );
$b = hexdec( $b );
return array( 'R' => $r, 'G' => $g, 'B' => $b );
}
function cmp($a, $b)
{
$rgb1 = hex2rgb($a);
$rgb2 = hex2rgb($b);
$hsl1 = rgb2hsl($rgb1['R'], $rgb1['G'], $rgb1['B']);
$hsl2 = rgb2hsl($rgb2['R'], $rgb2['G'], $rgb2['B']);
$h1 = $hsl1['H'];
$s1 = $hsl1['S'];
$l1 = $hsl1['L'];
$h2 = $hsl2['H'];
$s2 = $hsl2['S'];
$l2 = $hsl2['L'];
if($h1 == $h2 && $s1 == $s2 && $l1 == $l2)
{
return 0;
}
else
{
if (($h1 > $h2) ||
(($h1 == $h2) && ($s1 > $s1) ) ||
(($h1 == $h2) && ($s1 == $s1) && ($l1 > $l2)))
{
return 1;
}
else
{
return -1;
}
}
}
$a = array(
"#000000","#FFFFFF","#642424","#CC0605","#CB2821","#A2231D",
"#AF2B1E","#BF4435","#F54021","#C93C20","#FFDDD6","#F44611","#F75E25","#C1876B",
"#FABFA1","#734222","#FF7514","#FFCC99","#8A6642","#FDF4E3","#F3A505","#C6A664",
"#F4A900","#C2B078","#F5CB21","#E6D690","#FAD201","#E1CC4F","#F3DA0B","#7E7B52",
"#FFFCC1","#B8B799","#F8F32B","#FFFF99","#EDFF21","#B8CE3B","#424632","#343B29",
"#85BD3E","#31372B","#79BB51","#89AC76","#4FAE2E","#C0DEBC","#2D572C","#8D948D",
"#1CA744","#1E5945","#39B49F","#3F888F","#439AA4","#256D7B","#434B4D","#293133",
"#008FB7","#0D3A4D","#D0E6F1","#474B4E","#2271B3","#0E294B","#0B2C59","#0E3987",
"#4170CC","#1D1E33","#A49FC7","#6C3EC3","#261448","#986DE9","#37225D","#260B3E",
"#D8C6DE","#6C4675","#FFA3FF","#E54EE5","#6D3F5B","#D569A7","#A03472","#9E1A65",
"#421C31","#CF3476","#DE4C8A","#4A192C","#641C34","#F8C5CE","#EA899A","#412227",
"#5E2129","#9B111E","#75151E","#D60B11","#D36E70","#D53032" );
usort($a, "cmp");
foreach ($a as $key => $value)
{
echo "<div style=\"width:390px; height:40px; background:$value;\"> $value</div>";
}
?>