Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #39462

Re: Code for changing same colors on webpage to unique random colors after each webpage display change.

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: Code for changing same colors on webpage to unique random colors after each webpage display change.
Date 2021-02-13 12:18 +0000
Organization A noiseless patient Spider
Message-ID <87blcob1pd.fsf@bsb.me.uk> (permalink)
References <f432693c-5415-4992-8297-43244b3440d8n@googlegroups.com> <e9df338f-c38e-49c9-866e-ddbefe0adee0n@googlegroups.com>

Show all headers | View raw


Jonas Thörnvall <jonas.thornvall@gmail.com> writes:

> function rndColor() {
>     var hex = ['0', '1', '2', '3', '4', '5', '6', '7',
>                '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'],
>         color = '#', i;
>     for (i = 0; i < 6 ; i++) {
>         color = color + hex[Math.floor(Math.random() * 16)];
>     }
>     return color;
> }

Or you could use

  '#' + Math.floor((Math.random()*0x1000000)).toString(16).padStart(6, '0')

and, if you want to avoid padStart, (it's a recent addition):

  '#' + ('000000' + Math.floor((Math.random()*0x1000000)).toString(16)).slice(-6)

-- 
Ben.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Code for changing same colors on webpage to unique random colors after each webpage display change. I A <he12457@hotmail.com> - 2021-02-12 14:43 -0800
  Re: Code for changing same colors on webpage to unique random colors after each webpage display change. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-02-13 03:28 -0800
    Re: Code for changing same colors on webpage to unique random colors after each webpage display change. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-02-13 12:18 +0000
  Re: Code for changing same colors on webpage to unique random colors after each webpage display change. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-02-13 03:37 -0800

csiph-web