I’ve a list of arbitrary strings. I would like to convert these to a hexadecimal color code.
The code should not be random, as it is essential that the method must return the same color code each time I convert the same string.
SOLUTION:
public string GetColorCode(string value)
{
var i = value.GetHashCode() & 0x00FFFFFF;
return i.ToString("X6");
}
You can use
GetHashCode()as a starting point. SinceGetHasCode()returns a full integer and you usually need just 3 bytes to define a color inRGB, you have to skip the noin significant part by doing either:or
this guarantee having same string, same color.