给定这个函数,我想用随机颜色生成器替换颜色。

document.overlay = GPolyline.fromEncoded({
    color: "#0000FF",
    weight: 10,
    points: encoded_points,
    zoomFactor: 32,
    levels: encoded_levels,
    numLevels: 4
});

我该怎么做呢?


当前回答

另一个随机颜色生成器:

var randomColor;
randomColor = Math.random() * 0x1000000; // 0 < randomColor < 0x1000000 (randomColor is a float)
randomColor = Math.floor(randomColor); // 0 < randomColor <= 0xFFFFFF (randomColor is an integer)
randomColor = randomColor.toString(16); // hex representation randomColor
randomColor = ("000000" + randomColor).slice(-6); // leading zeros added
randomColor = "#" + randomColor; // # added

其他回答

许多答案调用Math.random()的次数比必需的要多。或者他们希望这个数字的十六进制表示,有六个字符。

首先将随机浮点数乘以范围[0,0xffffff + 1)。现在我们的数字有了0xRRRRRR的形式和一些变化,这是一个有24位有效位的数字。一次读取4位,并使用该随机数[0,15]并在查找时将其转换为匹配的十六进制字符。

function randomColor() {
    var lookup = "0123456789abcdef";
    var seed = Math.random() * 0x1000000;
    return (
        "#" +
        lookup[(seed & 0xf00000) >> 20] +
        lookup[(seed & 0x0f0000) >> 16] +
        lookup[(seed & 0x00f000) >> 12] +
        lookup[(seed & 0x000f00) >> 8] +
        lookup[(seed & 0x0000f0) >> 4] +
        lookup[seed & 0x00000f]
    );
};

这段代码(Mohsen的)不能生成像#fcfc80这样的颜色。

'#' + Math.random().toString(16).substr(-6);

Nicolas Buduroi的方法有效,除非Math.random()返回0,合理。

'#' + (Math.random().toString(16) + "000000").substring(2,8)

这段代码生成了很多非法的颜色(比如#abcde)。

'#' + Math.floor(Math.random()*16777215).toString(16);

我一直在用(+1或其他也可以,但我习惯了毫无理由的+2)

"#" + ((Math.random()+2)*16777216|0).toString(16).slice(1)

我认为第一个回答是最简洁/有用的,但我只是写了一个初学者可能更容易理解的回答。

function randomHexColor(){
    var hexColor=[]; //new Array()
    hexColor[0] = "#"; //first value of array needs to be hash tag for hex color val, could also prepend this later

    for (i = 1; i < 7; i++)
    {
        var x = Math.floor((Math.random()*16)); //Tricky: Hex has 16 numbers, but 0 is one of them

        if (x >=10 && x <= 15) //hex:0123456789ABCDEF, this takes care of last 6 
        {
            switch(x)
            {
                case 10: x="a" 
                break;
                case 11: x="b" 
                break;
                case 12: x="c" 
                break;
                case 13: x="d" 
                break;
                case 14: x="e" 
                break;
                case 15: x="f" 
                break;  
            }
        }
        hexColor[i] = x;
    }
    var cString = hexColor.join(""); //this argument for join method ensures there will be no separation with a comma
    return cString;
}

函数 getHashColor() { var hash = “0123456789ABCDEF”; var hashColor = “#”; for (var i = 0; i < 6; i++) hashColor += hash[Math.floor(Math.random() * hash.length)]; document.getElementById('demo').style.background = hashColor } <div id=“demo” style=“background:red;height:300px;width:300px”></div> <button type=“button” onclick=“getHashColor()”>Clik Me</button>

随机颜色生成与亮度控制:

function getRandColor(brightness){

    // Six levels of brightness from 0 to 5, 0 being the darkest
    var rgb = [Math.random() * 256, Math.random() * 256, Math.random() * 256];
    var mix = [brightness*51, brightness*51, brightness*51]; //51 => 255/5
    var mixedrgb = [rgb[0] + mix[0], rgb[1] + mix[1], rgb[2] + mix[2]].map(function(x){ return Math.round(x/2.0)})
    return "rgb(" + mixedrgb.join(",") + ")";
}