Tag color generator

I made a thing to generate bright, happy colors for our tags, using the tag name itself as a seed for a generated RGB value. The colors it picks are quite nice (you can seem them on the homepage) but it crudely brightens a random channel in a way that creates samey results. It could obviously do with some refinement. Any ideas on how the following function could make nicer, more varied colors?

The basic requirements are that it should generally avoid pale results (white backdrop), too dark (indistinguishable), or too gray (dull)

function tocolor($n)
{
	//Shoehorn given string $n into base-36 for easier mangling to an RGB value
	
    $vals = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $vals = array_flip(str_split($vals));
    $out = 0;
    $n = strtoupper($n); //make it all uppercase
    $n = preg_replace('/\s+/', '', $n);  //remove holes
    $len = strlen($n); 
    for ($i = 0; $i < $len; $i++) { 
        $c = $n[$len - ($i + 1)];
        $out += $vals[$c] * pow(36, $i); // 
    }
 	
 	//generate legit red, green ane blue channels from what remains
 	
 	$r = substr($out,0,2);
 	$g = substr($out,2,2);
 	$b = substr($out,4,2);
 	
 	//this crudely brightens up the resulting value by putting a high value into one channel
 	
 	$brightcolor = ($out  % 3)+1; 
	if ($brightcolor == 1) {$r = "dd";}
	if ($brightcolor == 2) {$g = "dd";}
	if ($brightcolor == 3) {$b = "dd";}

	$color = $r . $g . $b; // actually an RGB value
	
	if ($n == "OFFWORLD") {echo "color:#69F";} // OVERRIDE for offworld official color! 
	else { echo "color:#" . substr($color ,0,6); }
}
2 Likes

How about picking a random color out of colorbrewer http://bl.ocks.org/mbostock/5577023? Or perhaps this library is more to your taste http://llllll.li/randomColor/

3 Likes

randomcolor looks promising

1 Like

@gwwar is definitely your huckleberry on this sort of thing.

HSL colorspace provides a way to keep the brightness constant, with hue in the range 0 … 359

$hue = hashOfTag(...);
echo "color:hsl(".($hue % 360).",100%,75%);";
2 Likes

This topic was automatically closed after 520 days. New replies are no longer allowed.