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.

Path csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
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 Sat, 13 Feb 2021 12:18:38 +0000
Organization A noiseless patient Spider
Lines 22
Message-ID <87blcob1pd.fsf@bsb.me.uk> (permalink)
References <f432693c-5415-4992-8297-43244b3440d8n@googlegroups.com> <e9df338f-c38e-49c9-866e-ddbefe0adee0n@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=iso-8859-1
Content-Transfer-Encoding 8bit
Injection-Info reader02.eternal-september.org; posting-host="713a0bf9957c780b6018b18642f1da14"; logging-data="8148"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0tvD54++RjZ/P8/FZtuQCImftJZiQeDY="
Cancel-Lock sha1:G9V2k/QP+RfOBC1R6KVu0JcYffw= sha1:pj1frsPMn9LTMt0mHFvP7+yxq0o=
X-BSB-Auth 1.efae3c63e6364525487f.20210213121838GMT.87blcob1pd.fsf@bsb.me.uk
Xref csiph.com comp.lang.javascript:39462

Show key headers only | 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