Ligthers
Ligthers

Reputation: 217

Picking random color within the shades of gray color range

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'

Upvotes: 0

Views: 427

Answers (2)

Abhishek
Abhishek

Reputation: 3967

Why not this:

$n = mt_rand(0, 255);
background-color: rgb(<?= $n; ?>, <?= $n; ?>, <?= $n; ?>)

Upvotes: 1

John3136
John3136

Reputation: 29266

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.

Upvotes: 2

Related Questions