I am working on a code that needs to do the following:
imagine I have a string of characters, for example: $\text{dk48vns203lvm923dpvgj39dkv}$
The characters can be $0,\dots,9$ or not capital letters $a,\dots ,z$, so $36$ options per character. I would like to create a function that maps a part of this string into an RGB color. What it means is that I need $3$ $0-255$ numbers for example $\text{fj3k0} = (233,9,48)$. So thinking about it, the total options for an RGB color is $256^3 = 16\ 777\ 216$. Each character has $36$ options so if we look at $4$ chars it's $36^4 = 1\ 679\ 616$ and $5$ chars is $36^5 = 60\ 466\ 176$. I want a function to be as unique as possible and uniformly distributed. I have a working solution right now but I believe it can be improved.
My solution:
Take $6$ chars, every $2$ chars will map to a different $256$ number ($2$ chars for R, $2$ chars for G, $2$ chars for B).
These $2$ chars are mapped just like a base $36$ number which means $00$ is $0$ and $\text{zz}$ is $1296$ $(36 \times 36^1 + 36 \times 36^0)$. I then take the calculation and normalize it for $255$.
Main problems:
Not really unique.
Because of the normalization, a lot of numbers become real numbers, for example $700$ can be something like $146.4345$. RGB is integers so this is rounding down which ruins the uniqueness even more.
I am using $6$ chars. $6$ chars can potentially map to $36^6 = 2\ 176\ 782\ 336$ unique RGB tuples, but this mapping struggles to find $16\ 777\ 216$ inside them so this is kind of a waste in information.
Thanks
You could take the remainder of the number represented by the two characters divided by $256$, i.e. if the number represented is $n$, then calculate $n \bmod 256$
... a small problem is that two characters have $36^2=1296$ combinations which is $5\cdot256+16$, and so it 'favors' the numbers $1$ through $16$ ... although only $16$ out of $1296$ times, which is a little more than $1$ out of $100$ times ... I don't think that's bad, and if that is acceptable to you as well, then go for it.
Otherwise, you can take $6$ characters, take the number $m$ represented by that, calculate $m'=m \bmod 256^3$, and then compute the RGB values as follows:
$$R = m' \bmod 256$$
$$G = \frac{m'-R}{256} \bmod 256$$
$$B = \frac{m'-R-256\cdot G}{256^2}$$
You will still have numbers that are still more likely to be the outcome than other numbers, but this time that happens only about $1$ in $200$ times.