Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert strings to an html color code hash?

Tags:

I'd like to represent strings as arbitrary html colors.

Example:

"blah blah" = #FFCC00
"foo foo 2" = #565656

It doesn't matter what the actual color code is, so long as it's a valid hexadecimal HTML color code and the whole spectrum is fairly well represented.

I guess the first step would be to do an MD5 on the string and then somehow convert that to hexadecimal color code?

Update: Usage example is to generate a visual report of file requests on a server. The colors don't have to look pretty, it's more so a human brain can detect patterns, etc in the data more readily.

like image 453
user217562 Avatar asked Sep 16 '10 06:09

user217562


People also ask

How do you color a hex code in HTML?

HEX Color Values For example, #ff0000 is displayed as red, because red is set to its highest value (ff), and the other two (green and blue) are set to 00. Another example, #00ff00 is displayed as green, because green is set to its highest value (ff), and the other two (red and blue) are set to 00.

How do you use color tags in HTML?

Learn HTML You can specify colors on page level using <body> tag or you can set colors for individual tags using bgcolor attribute. bgcolor − sets a color for the background of the page. text − sets a color for the body text. alink − sets a color for active links or selected links.


1 Answers

Thanks for the pointers, this seems to do a competent job:

function stringToColorCode($str) {   $code = dechex(crc32($str));   $code = substr($code, 0, 6);   return $code; }  $str = 'test123'; print '<span style="background-color:#'.stringToColorCode($str).'">'.$str.'</span>'; 
like image 88
user217562 Avatar answered Oct 02 '22 15:10

user217562