samedi 27 mai 2017

How to generate a random DARK hex color code with PHP?

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.

enter image description here

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?




Aucun commentaire:

Enregistrer un commentaire