This page looks best with JavaScript enabled

Pseudo-Random Colors with Hashed Strings

 ·  ☕ 2 min read

In this video we’re going to explore a way I approach reproducible pseudo-random number generation that uses calculated hashes to select a random item or property for an object. In this case we’ll be assigning hue shifted colors to text strings. This helps easily identify elements in a list by searching for both a memorable name but also a color that will always be the same for a similar string.

To convert a string into an integer using an MD5 hash you could write something like this:

1
2
3
4
var stringToHash = "World of Zero";
var hasher = MD5.Create();
var hash = hasher.ComputeHash(Encoding.UTF8.GetBytes(stringToHash));
var hashedInteger = BitConverter.ToInt32(hash, 0);

Then to convert an integer into a random color use this. In this example 256 hues will be selected from the color wheel:

1
var color = Color.HSVToRGB((Math.Abs(hashedInteger) % 256) / 256.0f, 1.0f, 0.75f);

This same technique can be applied to other things as well. For example we can use the hashed value to index into a lookup table of names. Other approaches could apply a specific texture to a material or instantiate a specific prefab:

1
2
var potentialNames = new string[] { "World of Zero", "YouTube", "Unity 3D" };
var name = potentialNames[Math.Abs(hashedInteger) % potentialNames.Count];

The System.Security.Cryptography.MD5 class is documented here: https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5
More information on how hashes work on Wikipedia: https://en.wikipedia.org/wiki/Hash_function
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u


Sam Wronski
WRITTEN BY
Sam Wronski
Maker of things, currently helping build cloud things @ Google. World of Zero is a personal project disconnected from my professional work. Lets make something awesome together!