给定这个函数,我想用随机颜色生成器替换颜色。
document.overlay = GPolyline.fromEncoded({
color: "#0000FF",
weight: 10,
points: encoded_points,
zoomFactor: 32,
levels: encoded_levels,
numLevels: 4
});
我该怎么做呢?
给定这个函数,我想用随机颜色生成器替换颜色。
document.overlay = GPolyline.fromEncoded({
color: "#0000FF",
weight: 10,
points: encoded_points,
zoomFactor: 32,
levels: encoded_levels,
numLevels: 4
});
我该怎么做呢?
当前回答
Array.prototype.reduce使它非常简洁。
["r", "g", "b"].reduce(function(res) {
return res + ("0" + ~~(Math.random()*256).toString(16)).slice(-2)
}, "#")
它需要一个旧浏览器的垫片。
其他回答
正则表达式
总是返回有效的十六进制6位颜色
"#xxxxxx".replace(/x/g, y=>(Math.random()*16|0).toString(16))
令c= "#xxxxxx"。替换(/ x / g, y = > (math . random () * 16 | 0) .toString (16)); console.log (c); document.body.style.background = c
使用色彩分明的颜色。
它生成了一个视觉上不同颜色的调色板。
Distinct-colors是高度可配置的:
选择调色板中有多少种颜色 将色调限制在一个特定的范围内 限制色度(饱和度)到一个特定的范围 将亮度限制在一个特定的范围内 配置调色板的一般质量
如果你像我一样是个新手,对十六进制之类的东西一无所知,这可能更直观。
function r() {
return Math.floor(Math.random() * 256);
}
const color = "rgb(" + r() + "," + r() + "," + r() + ")";
你只需要以一个字符串结束,比如“rgb(255, 123, 220)”。
试试这个包- https://www.npmjs.com/package/gen-random-colors。
它还提供了从0到5(0是最暗的)配置颜色集的能力。
这条线会为你随机改变颜色:
setInterval(function(){y.style.color=''+"rgb(1"+Math.floor(Math.random() * 100)+",1"+Math.floor(Math.random() * 100)+",1"+Math.floor(Math.random() * 100)+")"+'';},1000);