Skip to content

Latest commit

 

History

History
23 lines (18 loc) · 480 Bytes

string-to-random-color.md

File metadata and controls

23 lines (18 loc) · 480 Bytes

String to hex color

function hashCode(str) { // java String#hashCode
    var hash = 0;
    for (var i = 0; i < str.length; i++) {
       hash = str.charCodeAt(i) + ((hash << 5) - hash);
    }
    return hash;
} 

function intToRGB(i){
    var c = (i & 0x00FFFFFF)
        .toString(16)
        .toUpperCase();

    return "#" + "00000".substring(0, 6 - c.length) + c;
}

Reference

https://stackoverflow.com/a/3426956