Please note that I have already read Generating a random hex color code with PHP and I got help from that question too. But my question is different to that question.
I am going to create 1000+ images with some text like below image.
Text color is always white. I need to generate background color randomly. I use following code to generate random color.
<?php
function random_color_part() {
return str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT);
}
function random_color() {
return random_color_part() . random_color_part() . random_color_part();
}
echo random_color();
?>
This script generate light colors like white, light yellow, light blue too. I need to remove them. Because if the background color is also light colour it is hard to read the text.
So is there way to modify this code generate only dark colors?
May this is help you, random dark hex color in PHP
function random_color_part() {
$dt = '';
for($o=1;$o<=3;$o++)
{
$dt .= str_pad( dechex( mt_rand( 0, 127 ) ), 2, '0', STR_PAD_LEFT);
}
return $dt;
}
You only call random_color_part().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With