Random color generation using PHP

前端 未结 13 1799
终归单人心
终归单人心 2020-12-14 09:48

I\'m trying to generate random HTML colors in PHP, but I\'m having trouble getting them to look similar, or in the same family. Is there some function I can use to generate

13条回答
  •  遥遥无期
    2020-12-14 10:45

    Another short and simple version: change the mt_rand(X, Y) values in order to generate desired colour range: (0, 255) - full range; (180, 255) - pastel palette; (0, 100) - dark palette; etc...

    function gen_color(){
        mt_srand((double)microtime()*1000000); 
        $color_code = '';
        while(strlen($color_code)<6){
            $color_code .= sprintf("%02X", mt_rand(0, 255));
        }
        return $color_code;
    }
    

提交回复
热议问题