Hash string into RGB color

后端 未结 5 1822
无人及你
无人及你 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:16

    I tried all the solutions others provided but found that similar strings (string1 vs string2) produce colors that are too similar for my liking. Therefore, I built my own influenced by the input and ideas of others.

    This one will compute the MD5 checksum of the string, and take the first 6 hex digits to define the RGB 24-bit code.

    The MD5 functionality is an open-source JQuery plug in. The JS function goes as follows:

    function getRGB(str) {
        return '#' + $.md5(str).substring(0, 6);
    }
    

    A link to this working example is on jsFiddle. Just input a string into the input field and press enter, and do so over and over again to compare your findings.

提交回复
热议问题