在PHP中由Hue排序颜色

H ieveryone,

I would like to sort colors so that they are beautifully organized (in a way that similar colors are grouped together)

I have found this function 'rgb2hsl' somewhere in a forum (sorryif I don't recall the link) to convert an RGB value to HSL then sort by H, S, L.

the result is OK but far from satisfying because some light colors are mixed with dark ones:

Below is the entire script in php: could you please help tune-in the function to group the colors in a nicer way?

thanks in advance.

<?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>";
}

?>

You cannot have random colors aesthetically listed together unless you first define the color scheme you want to use, that is if you want to automate it.

This web site shows you how to use different color schemes : http://colorschemedesigner.com/

what you may do is calculate the Hues of all the colors using HSV r HSL and make groups of colors (maximum colors in a group should not be more that 5).

TBH even though standard color schemes are only to facilitate you to a certain extent, there are many color schemes which look really great but they completely violate the standard schemes due to the fact that standard color schemes do not take account of saturation and perceptive brightness. e.g. http://www.colourlovers.com/palettes

Now I do not want to pull you off the road so my suggestion would be to instead of selecting the pallets yourself select some random Hues and calculate the rest of the hues/tints/saturation by selecting a particular color scheme.