0

How Can I pick a random color within a color hex range. I need to pick the gray shades only as seen in this link Link for gray

Here's my code

$color = sprintf('#%06X', mt_rand(444,EEE));

Here's the error Use of undefined constant EEE - assumed 'EEE'

Ligthers
  • 217
  • 1
  • 4
  • 10
  • It looks like you are trying to use `mt_rand()` on an alphanumeric range, which you have not defined, which I don't think will work like that http://stackoverflow.com/questions/48124/generating-pseudorandom-alpha-numeric-strings – Sean Dec 20 '16 at 02:44
  • Useful? Since when? Did't you know that if you wrap it in quotes it will produce another error since it was actually picking random? `mt_rand() expects parameter 2 to be long, string given in` – Ligthers Dec 20 '16 at 02:44

2 Answers2

2

As you can see on your link, Grays (or greys) have the same Red Green and Blue values, so you need to generate one 2 digit number and use that in in 3 positions.

Just picking a random number between 222 and EEE could end up with say 3F7 which is in the range but not a gray.

John3136
  • 28,809
  • 4
  • 51
  • 69
1

Why not this:

$n = mt_rand(0, 255);
background-color: rgb(<?= $n; ?>, <?= $n; ?>, <?= $n; ?>)
Abhishek
  • 3,900
  • 21
  • 42