Create a hexadecimal colour based on a string with JavaScript

后端 未结 13 957
旧时难觅i
旧时难觅i 2020-11-30 17:15

I want to create a function that will accept any old string (will usually be a single word) and from that somehow generate a hexadecimal value between #000000

13条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 18:10

    All you really need is a good hash function. On node, I just use

    const crypto = require('crypto');
    function strToColor(str) {
        return '#' + crypto.createHash('md5').update(str).digest('hex').substr(0, 6);
    }
    

提交回复
热议问题