Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random color generator

Given this function, I want to replace the color with a random color generator.

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

How can I do it?

like image 237
n00ki3 Avatar asked Sep 27 '09 21:09

n00ki3


People also ask

What are 16 colors?

Web Standard Color Namesaqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow.


2 Answers

Use getRandomColor() in place of "#0000FF":

function getRandomColor() {    var letters = '0123456789ABCDEF';    var color = '#';    for (var i = 0; i < 6; i++) {      color += letters[Math.floor(Math.random() * 16)];    }    return color;  }        function setRandomColor() {    $("#colorpad").css("background-color", getRandomColor());  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="colorpad" style="width:300px;height:300px;background-color:#000">    </div>  <button onclick="setRandomColor()">Random Color</button>
like image 168
Anatoliy Avatar answered Sep 28 '22 10:09

Anatoliy


I doubt anything will be faster or shorter than this one:

"#" + ((1<<24)*Math.random() | 0).toString(16) 

Challenge!

like image 30
ZPiDER Avatar answered Sep 28 '22 10:09

ZPiDER