Hash string into RGB color

HashColorsLanguage Agnostic

Hash Problem Overview


Is there a best practice on how to hash an arbitrary string into a RGB color value? Or to be more general: to 3 bytes.

You're asking: When will I ever need this? It doesn't matter to me, but imagine those tube graphs on any GitHub [network page][1]. There you can see something like this:

![git branches][2]

Where every colored line means a distinct git branch. The low tech approach to color these branches would be a CLUT (color lookup table). The more sophisticated version would be:

$branchColor = hashStringToColor(concat($username,$branchname));

Because you want a static color every time you see the branches representation. And for bonus points: How do you ensure an even color distribution of that hash function?

So the answer to my question boils down to the implementation of hashStringToColor().

[1]: https://github.com/CocoaPods/Specs/network "Network page for CocoaPods/Specs" [2]: http://i.stack.imgur.com/w0pFx.png

Hash Solutions


Solution 1 - Hash

A good hash function will provide a near uniform distribution over the key space. This reduces the question to how do I convert a random 32 bit number to a 3 byte RGB space. I see nothing wrong with just taking the low 3 bytes.

int hash = string.getHashCode();
int r = (hash & 0xFF0000) >> 16;
int g = (hash & 0x00FF00) >> 8;
int b = hash & 0x0000FF;

Solution 2 - Hash

For any Javascript users out there, I combined the accepted answer from @jeff-foster with the djb2 hash function from erlycoder.

The result per the question:

function djb2(str){
  var hash = 5381;
  for (var i = 0; i < str.length; i++) {
    hash = ((hash << 5) + hash) + str.charCodeAt(i); /* hash * 33 + c */
  }
  return hash;
}

function hashStringToColor(str) {
  var hash = djb2(str);
  var r = (hash & 0xFF0000) >> 16;
  var g = (hash & 0x00FF00) >> 8;
  var b = hash & 0x0000FF;
  return "#" + ("0" + r.toString(16)).substr(-2) + ("0" + g.toString(16)).substr(-2) + ("0" + b.toString(16)).substr(-2);
}

UPDATE: Fixed the return string to always return a #000000 format hex string based on an edit by @alexc (thanks!).

Solution 3 - Hash

I just build a JavaScript library named color-hash, which can generate color based on the given string (using HSL color space and BKDRHash).

Repo: https://github.com/zenozeng/color-hash<br> Demo: https://zenozeng.github.io/color-hash/demo/

Solution 4 - Hash

I tried all the solutions others provided but found that similar strings (string1 vs string2) produce colors that are too similar for my liking. Therefore, I built my own influenced by the input and ideas of others.

This one will compute the MD5 checksum of the string, and take the first 6 hex digits to define the RGB 24-bit code.

The MD5 functionality is an open-source JQuery plug in. The JS function goes as follows:

function getRGB(str) {
    return '#' + $.md5(str).substring(0, 6);
}

A link to this working example is on jsFiddle. Just input a string into the input field and press enter, and do so over and over again to compare your findings.

Solution 5 - Hash

As an example, this is how Java calculates the hashcode of a string (line 1494 and following). It returns an int. You can then calculate the modulo of that int with 16,777,216 (2^24 = 3 bytes) to get an "RGB-compatible" number.

It is a deterministic calculation so the same word(s) will always have the same colour. The likelihood of hash collision (2 strings having the same colour) is small. Not sure about the colour distribution, but probably fairly random.

Solution 6 - Hash

Interstingly enough a common representation of hashing functions like sha256 or md5 is as a hex string. Here is a fiddle that uses the 10 6 byte segemnts to create a coloful representaion of and sha256 hash. It uses the last 4 byte segment to determine the angle of the colors.

https://jsfiddle.net/ypx4mtnr/4/

async function sha256(message) ... see fiddle ...

   async function sha256(message) {
    // encode as UTF-8
    const msgBuffer = new TextEncoder().encode(message);                    

    // hash the message
    const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);

    // convert ArrayBuffer to Array
    const hashArray = Array.from(new Uint8Array(hashBuffer));

    // convert bytes to hex string                  
    const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
    return hashHex;
   }  
   function sha256tohtml(x,b){
var y=chunkSubstr(x,6);
return '<div style="'+
	'display: inline-block;'+
	'margin:2em;'+
	'width:128px;'+
	'height:128px;'+
	'border-radius: 512px;'+
	'background: conic-gradient('+
	'	from '+((360/65536)*parseInt(y[10],16))+'deg at 50% 50%,'+
	'	#'+y[0]+' '+(0+b)+'%,'+
	'	#'+y[0]+' 10%,'+
	'	#'+y[1]+' '+(10+b)+'%,'+
	'	#'+y[1]+' 20%,'+
	'	#'+y[2]+' '+(20+b)+'%,'+
	'	#'+y[2]+' 30%,'+
	'	#'+y[3]+' '+(30+b)+'%,'+
	'	#'+y[3]+' 40%,'+
	'	#'+y[4]+' '+(40+b)+'%,'+
	'	#'+y[4]+' 50%,'+
	'	#'+y[5]+' '+(50+b)+'%,'+
	'	#'+y[5]+' 60%,'+
	'	#'+y[6]+' '+(60+b)+'%,'+
	'	#'+y[6]+' 70%,'+
	'	#'+y[7]+' '+(70+b)+'%,'+
	'	#'+y[7]+' 80%,'+
	'	#'+y[8]+' '+(80+b)+'%,'+
	'	#'+y[8]+' 90%,'+
	'	#'+y[9]+' '+(90+b)+'%,'+
	'	#'+y[9]+' 100%);"></div>';
  }
   function chunkSubstr(str, size) {
  const numChunks = Math.ceil(str.length / size)
  const chunks = new Array(numChunks)

  for (let i = 0, o = 0; i < numChunks; ++i, o += size) {
    chunks[i] = str.substr(o, size)
  }

  return chunks
}
   function draw(x){
 	sha256(x).then((x)=>{
    	var html=sha256tohtml(x,0);
			document.getElementById('addhere').innerHTML=html;
		});
   }
   window.onload=function(){
 	document.getElementById('sha256string').oninput=function(){draw(this.value);}
        draw(document.getElementById('sha256string').value)
}

<html>
<input id="sha256string" value="Hello World">
 <div id="addhere"></div>
</html>

Solution 7 - Hash

If you are using C# and want HSL colors

Here is a class I wrote based on the library written by Zeno Zeng and mentioned in this answer .

The HSLColor class was written by Rich Newman, it is defined here


    public class ColorHash
    {        
        
        // you can pass in a string hashing function of you choice
        public ColorHash(Func<string, int> hashFunction)
        {
            this.hashFunction = hashFunction;
        }

        // or use the default string.GetHashCode()
        public ColorHash()
        {
            this.hashFunction = (string s) => { return s.GetHashCode(); };
        }
        
        Func<string, int> hashFunction = null;

        static float[] defaultValues = new float[] { 0.35F, 0.5F, 0.65F };

        // HSLColor class is defined at https://richnewman.wordpress.com/about/code-listings-and-diagrams/hslcolor-class/
        public HSLColor HSL(string s) {
            return HSL(s, defaultValues, defaultValues);
        }

        // HSL function ported from https://github.com/zenozeng/color-hash/       
        public HSLColor HSL(string s, float[] saturationValues, float[] lightnessValues)
        {
            
            double hue;
            double saturation;
            double luminosity;

            int hash = Math.Abs(this.hashFunction(s));

            hue = hash % 359;

            hash = (int)Math.Ceiling((double)hash / 360);
            saturation = saturationValues[hash % saturationValues.Length];

            hash = (int)Math.Ceiling((double)hash / saturationValues.Length);
            luminosity = lightnessValues[hash % lightnessValues.Length];

            return new HSLColor(hue, saturation, luminosity);
        }
        
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJens KohlView Question on Stackoverflow
Solution 1 - HashJeff FosterView Answer on Stackoverflow
Solution 2 - Hashclayzermk1View Answer on Stackoverflow
Solution 3 - HashZeno ZengView Answer on Stackoverflow
Solution 4 - HashR.J.View Answer on Stackoverflow
Solution 5 - HashassyliasView Answer on Stackoverflow
Solution 6 - HashTerry RiegelView Answer on Stackoverflow
Solution 7 - HashWalter StaboszView Answer on Stackoverflow