views:

960

answers:

5

Hi there,

i am not an Javascript and i need a little help. Given this function , i want to replace the color with an Random Color generator.

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

Can anyone help me ?

+2  A: 

this can be very easily found on google.

function random_color(format)
{
 var rint = Math.round(0xffffff * Math.random());
 switch(format)
 {
  case 'hex':
   return ('#0' + rint.toString(16)).replace(/^#0([0-9a-f]{6})$/i, '#$1');
  break;

  case 'rgb':
   return 'rgb(' + (rint >> 16) + ',' + (rint >> 8 & 255) + ',' + (rint & 255) + ')';
  break;

  default:
   return rint;
  break;
 }
}
Funky Dude
Maybe so; but to which site would you prefer the possible Google Adwords-revenue went? =)
David Thomas
whichever site gives you the answer? if they provide you the answer, they should get the hits.
Funky Dude
+7  A: 

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

function get_random_color() {
    var letters = '0123456789ABCDEF'.split('');
    var color = '#';
    for (var i = 0; i < 6; i++ ) {
        color += letters[Math.round(Math.random() * 15)];
    }
    return color;
}
Anatoliy
awesome ! thanx . this works perfect for me :)
n00ki3
A: 

Here's a twist on solution provided by @Anatoliy I needed to generate only light colours (for backgrounds) so I went with 3 letter (#AAA) format.

function get_random_color() {
var letters = 'ABCDE'.split('');
var color = '#';
for (var i=0; i<3; i++ ) {
    color += letters[Math.floor(Math.random() * letters.length)];
}
return color;

}

Andrei Railean
+1  A: 

No need for a hash of hexadecimal letters, Javascript can do this by itself:

function get_random_color() {
  function c() {
    return Math.floor(Math.random()*256).toString(16)
  }
  return "#"+c()+c()+c();
}
Alsciende
A: 

So whilst all the answers here are good I wanted a bit more control over the output. For instance I'd like to prevent any near white shades, whilst ensuring I get bright vibrant colours not washed out shades.

function generateColor(ranges) {
            if (!ranges) {
                ranges = [
                    [150,256],
                    [0, 190],
                    [0, 30]
                ];
            }
            var g = function() {
                //select random range and remove
                var range = ranges.splice(Math.floor(Math.random()*ranges.length), 1)[0];
                //pick a random number from within the range
                return Math.floor(Math.random() * (range[1] - range[0])) + range[0];
            }
            return "rgb(" + g() + "," + g() + "," + g() +")";
        };

So now I can specify 3 arbitrary ranges to pick rgb values from. You can call it with no arguments and get my default set which will usually generate a quite vibrant colour with once obvious dominant shade, or you can supply your own array of ranges.

Ollie Edwards
Google Map API supports only hexadecimal HTML color in the "#FFFFFF" format.
Valery Victorovsky
Sure, pretty straightforward to convert a number to hex n.toString(16)only snag is you'll need to zero pad to make sure you get a two character return value from the inner g function.
Ollie Edwards