Random javascript color with 7digits should

Hey guys, I found the following awesome script to create a random color with javascript.

var randColor = '#'+(Math.random()*0xFFFFFF<<0).toString(16);

the only problem with this script is that it does not guarantee that it returns a normal hexadecimal string with 7digit.

sometimes it's just 6 digits, like # e1d19.

is there any way to force a 7 digit hexadecimal value?

Thank you for your help.

edit: this is my actual problem:

function randColor() {
    var randColor = '#'+(Math.random()*0xFFFFFF<<0).toString(16);
    return randColor;
}

for (var i=0; i<100; i++) {
    $("#colorpicker").append("<div class='color' title="+randColor()+" style='background:"+randColor()+"'></div>");
}

I create small divs with random colors, when I click on them, I grab their title attribute, and I color the background of my body.

however, currently my code ends in

<div style="background:rgb(176, 249, 252);" title="#8bc47d" class="color"></div>

so when I get the title attribute, the color that I give to my body is different from a small div show.

+3
6

:

function randomColor() {
  var rc = (~~(Math.random() * 0xFFFFFF)).toString(16);
  return '#' + new Array(7 - rc.length).join('0') + rc;
}

:

new Array(n).join(char)

- n - 1 "char" . 7 6, , 5 , , ..

edit — ( ), :

  return '#' + "000000".slice(rc.length) + rc;

jsperf, , : -)

+3
var randColor = '#'+(0xFFFFFFFF-Math.random()*0xFFFFFFFF).toString(16).substr(0, 6);
+2

() :

var randColor = (Math.random()*0xFFFFFF<<0).toString(16);
while( randColor.length < 6 ) {
    randColor = '0' + randColor;
}
randColor = '#' + randColor;
+1

, 3 ( R, G B). 0, , . , - , :)

//I am GUESSING that this is how you get a 2-digit hex value ranging 0-255
var r = PadDigits(Math.random()*0xFF<<0).toString(16),2);
var g = PadDigits(Math.random()*0xFF<<0).toString(16),2);
var b = PadDigits(Math.random()*0xFF<<0).toString(16),2);

var randColor = '#'+r+g+b;

function PadDigits(n, totalDigits) 
{ 
    n = n.toString(); 
    var pd = ''; 
    if (totalDigits > n.length) 
    { 
        for (i=0; i < (totalDigits-n.length); i++) 
        { 
            pd += '0'; 
        } 
    } 
    return pd + n.toString(); 
} 
+1

, , , , . , , . :

var color = (Math.random() * 0xFFFFFF << 0).toString(16);
"#" + String(color + "000000").slice(0, 6);

, :

var color = (Math.random() * 0xFFFFFF << 0).toString(16);
"#" + String("000000" + color).slice(-6);

, , . , , , , (, , .. ).

, randColor, . .

for (var i=0; i<100; i++) {
    var color = randColor();
    $("#colorpicker").append("<div class='color' title=" + color + " style='background:" + color + "'></div>");
}
+1

No one posted it, and this is the shortest code I received to get a random color.

'#'+Math.random().toString(16).slice(-6)
  • It will get a random floating point number of 0.437920 ...
  • Convert it to hex, you will get something like this 0.7c13b41830e33
  • Take the last 6 characters that will give you a random color #

Easy to understand, short and effective.

0
source

Source: https://habr.com/ru/post/1781542/


All Articles