将随机字符串转换为十六进制颜色 [英] Converting a random string into a hex colour
本文介绍了将随机字符串转换为十六进制颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序中有一个操作日志表。我想根据该条目的sessionID为行分配一个随机颜色,以帮助查看模式/分组操作。
I have a table of action logs in my application. I want to assign rows a random colour based on the sessionID of that entry to help see patterns/grouped actions.
到目前为止我有这个:
console.log(stringToColorCode('mj3bPTCbIAVoNr93me1I'));
function stringToColorCode(str) {
return '#'+ ('000000' + (Math.random()*0xFFFFFF<<0).toString(16)).slice(-6);
}
但是我需要用我的字符串整数替换Math.random(),是否有任何技术可以将字符串转换为与随机字符串保持一致的随机数?
However I need to replace Math.random() with my string-integer, are there any techniques for converting a string to a random number that remains consistent with the random string?
推荐答案
var color_codes = {};
function stringToColorCode(str) {
return (str in color_codes) ? color_codes[str] : (color_codes[str] = '#'+ ('000000' + (Math.random()*0xFFFFFF<<0).toString(16)).slice(-6));
}
这篇关于将随机字符串转换为十六进制颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文