As elementary OS is going to add letter tiles (similar to Android's) to act as default avatars for contacts, there was discussion about the color allocation algorithm. As some of the proposals were about using a boring lookup tables, I decided to try my hand at some hashing and color format conversion.
The algorithm generates a 32 bit hash and squashes it in the [0, 1]
interval by dividing it with
uint32.MAX. This hash value then gets multiplied
with 2*PI and used as the hue component of HSL. This gets converted back to RGB
for further manipulation.
The hashing function
The first hashing function used was a simple 8 bit XOR. Ignoring the fact that that only gave 256 possible colors, empirically, the hashing functions proved to be quite prone to collisions, especially with short strings and with short Hamming distances. Since the whole idea of the coloring is to help distinguish between strings that might be similar, I decided to change it to Bob Jenkins' one-at-a-time hashing function.
Picking saturation and lightness
As one of the requirements was to use colors similar to the elementary OS brand colors, I have adjusted the saturation and lightness values to match the mean of those components over the range of brand colors.
Since the foreground of the tile will contain a letter styled white, I used a contrast checker to evaluate the colors and adjust them for an acceptable contrast level. However, since the letters will also have a dark drop-shadow behind them, I did not pursue full test coverage.
HSL to RGB
Build and run
valac main.vala && ./main.vala