我要做一个h5的微信推广游戏,就是根据显示的化妆品颜色,去他生成的相近的颜色中去找正确的颜色,但是首先我要随机生成几个跟产品颜色相近的颜色,请问这个功能要怎么实现
根据颜色值得到rgb分量,然后随机加上一个值(比如-5~+5),合成一个新的颜色。
<script>
var color = '999999', m = color.match(/[\da-z]{2}/g);
for (var i = 0; i < m.length; i++) m[i] = parseInt(m[i], 16);//rgb
var colors = [];
for (var i = 0; i < 10; i++) {//生成10组颜色,色差20*Math.randon
colors[i] =
Math.floor(m[0] + (Math.random() < 0.5 ? -1 : 1) * Math.random() * 20).toString(16) +
Math.floor(m[1] + (Math.random() < 0.5 ? -1 : 1) * Math.random() * 20).toString(16) +
Math.floor(m[2] + (Math.random() < 0.5 ? -1 : 1) * Math.random() * 20).toString(16);
}
document.write('<ul><li style="height:20px;background-color:#'+color+'">#'+color+'</li>')
for (var i = 0; i < colors.length; i++) document.write('<li style="height:20px;background-color:#' + colors[i] + '">#' + colors[i] + '</li>');
document.write('</ul>')
</script>