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

…衆ロ難τιáo~ 提交于 2020-01-22 07:29:53

问题


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.


回答1:


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>';



回答2:


Almost always, just using random colours will

  1. look bad
  2. conflict with the background

I would recommend creating a (longish) list of colours that work well together and with your background - then just hash the string and modulus (%) with your number of colours to get an index into the table.

public function colorFromString($string)
{
  $colors = [
    '#0074D9',
    '#7FDBFF',
    '#39CCCC',
    // this list should be as long as practical to avoid duplicates
  ];

  // generate a partial hash of the string (a full hash is too long for the % operator)
  $hash = substr(sha1($string), 0, 10);

  // determine the color index
  $colorIndex = hexdec($hash) % count($colors);

  return $colors[$colorIndex];
}



回答3:


I agree with sje397 above that completely random colours could end up looking nasty. Rather than make a long list of nice-looking colours, I would suggest choosing a constant saturation+luminescence value, and varying the hue based on the content. To get an RGB colour from an HSL colour, you may use something similar to what's described in http://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB .

Here's an example (try it in http://codepad.viper-7.com something that works, such as https://codepad.remoteinterview.io/ZXBMZWYJFO):

<?php

function hsl2rgb($H, $S, $V) {
    $H *= 6;
    $h = intval($H);
    $H -= $h;
    $V *= 255;
    $m = $V*(1 - $S);
    $x = $V*(1 - $S*(1-$H));
    $y = $V*(1 - $S*$H);
    $a = [[$V, $x, $m], [$y, $V, $m],
          [$m, $V, $x], [$m, $y, $V],
          [$x, $m, $V], [$V, $m, $y]][$h];
    return sprintf("#%02X%02X%02X", $a[0], $a[1], $a[2]);
}

function hue($tstr) {
    return unpack('L', hash('adler32', $tstr, true))[1];
}

$phrase = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
$words = [];
foreach (explode(' ', $phrase) as $word)
    $words[hue($word)] = $word;
ksort($words);
foreach ($words as $h => $word) {
    $col = hsl2rgb($h/0xFFFFFFFF, 0.4, 1);
    printf('<span style="color:%s">%s</span> ', $col, $word);
}
?>



回答4:


One liner:

substr(md5($string), 0, 6);



回答5:


I think I understand the desire very well. I wanted the same thing. Trying to get a unique background colour that represent a title. I didn't want to predefine anything myself, I wanted to sort of get a visual hash for each title, but with pretty-ness. Found this to do the job pretty well. You can modify the brightness of the colours and variations of what it returns. You just test it to colours you are comfortable with. I think any colour should be good for this job, only that the brightness matters to distinguish it from the foreground.

https://mrkmg.com/blog/2012/01/13/php-function-to-generate-a-color-from-a-text-string/

Give it a string, minimum brightness (0-255) and a variance (2-10) and it will generate a colour for your string. This one can give you pretty colours if tweaked.



来源:https://stackoverflow.com/questions/3724111/how-can-i-convert-strings-to-an-html-color-code-hash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!