Hash string into RGB color

后端 未结 5 1813
无人及你
无人及你 2021-01-30 08:32

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 doe

5条回答
  •  隐瞒了意图╮
    2021-01-30 09:12

    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.

提交回复
热议问题