使用PHP生成高对比度随机颜色 [英] Generate high contrast random color using PHP
问题描述
我需要为系统中的所有用户生成随机颜色。诀窍是2个用户不能有非常相似的颜色,他们需要是可区分的。我有代码生成随机颜色给定原始的混合颜色,但没有找到一种方法来生成高对比度使用PHP的随机颜色
public static function generateRandomColor($ rgb)
{
$ red = rand(1,256);
$ green = rand(1,256);
$ blue = rand(1,256);
if(!empty($ rgb))
{
$ red =($ red + $ rgb ['red'])/ 2;
$ green =($ green + $ rgb ['green'])/ 2;
$ blue =($ blue + $ rgb ['blue'])/ 2;
}
$ color =rgb({$ red},{$ green},{$ blue});
return $ color;
}
然后我有一个循环:
$ colorsArr = array();
$ mixed = array('red'=> 255,'green'=> 255,'blue'=> 255);
for($ i = 0; $ i {
$ color = generateRandomColor($ mixed);
$ colorsArr。='< div style =width:25px; height:25px; background-color:'。$ color。'>< / div>';
}
现在这会生成颜色,但是一些颜色看起来像。目标是为每个用户都有一个独特的颜色。
注意:我不想为500位用户硬编码颜色我感到无聊,这里有一些代码你可以dink周围:
<?php
define(COL_MIN_AVG,64);
define(COL_MAX_AVG,192);
define(COL_STEP,16);
//(192 - 64)/ 16 = 8
// 8 ^ 3 = 512颜色
function usercolor($ username){
$ range = COL_MAX_AVG - COL_MIN_AVG;
$ factor = $ range / 256;
$ offset = COL_MIN_AVG;
$ base_hash = substr(md5($ username),0,6);
$ b_R = hexdec(substr($ base_hash,0,2));
$ b_G = hexdec(substr($ base_hash,2,2));
$ b_B = hexdec(substr($ base_hash,4,2));
$ f_R = floor((floor($ b_R * $ factor)+ $ offset)/ COL_STEP)* COL_STEP;
$ f_G = floor((floor($ b_G * $ factor)+ $ offset)/ COL_STEP)* COL_STEP;
$ f_B = floor((floor($ b_B * $ factor)+ $ offset)/ COL_STEP)* COL_STEP;
return sprintf('#%02x%02x%02x',$ f_R,$ f_G,$ f_B);
}
for($ i = 0; $ i <30; $ i ++){
printf('< div style =height:100px; width:100px; background-color:%s>& nbsp;< / div>'。\\\
,usercolor(rand()));
}
许多颜色看起来很相似,彼此相邻是苗条的。
I need to generate random colors for all the users in the system. The trick is that 2 users cant have very similar colors, they need to be distinguishable. I have the code to generate random colors given the original mix color but cant find a way to generate random colors with high contrast using only PHP
public static function generateRandomColor($rgb)
{
$red = rand(1, 256);
$green = rand(1, 256);
$blue = rand(1, 256);
if (! empty($rgb))
{
$red = ($red + $rgb['red']) / 2;
$green = ($green + $rgb['green']) / 2;
$blue = ($blue + $rgb['blue']) / 2;
}
$color = "rgb({$red}, {$green}, {$blue})";
return $color;
}
and then i have a loop:
$colorsArr = array();
$mixed = array('red' => 255, 'green' => 255, 'blue' => 255);
for($i = 0; $i < count($users); $i++)
{
$color = generateRandomColor($mixed);
$colorsArr .= '<div style="width:25px; height: 25px; background-color: ' . $color . '"></div>';
}
now this generates colors but some colors look like each other. The goal is to have an unique color for each user. any help appreciated thanks.
NOTE: i do not want to hardcode the colors for 500 users
I got bored, here's some code you can dink around with:
<?php
define( COL_MIN_AVG, 64 );
define( COL_MAX_AVG, 192 );
define( COL_STEP, 16 );
// (192 - 64) / 16 = 8
// 8 ^ 3 = 512 colors
function usercolor( $username ) {
$range = COL_MAX_AVG - COL_MIN_AVG;
$factor = $range / 256;
$offset = COL_MIN_AVG;
$base_hash = substr(md5($username), 0, 6);
$b_R = hexdec(substr($base_hash,0,2));
$b_G = hexdec(substr($base_hash,2,2));
$b_B = hexdec(substr($base_hash,4,2));
$f_R = floor((floor($b_R * $factor) + $offset) / COL_STEP) * COL_STEP;
$f_G = floor((floor($b_G * $factor) + $offset) / COL_STEP) * COL_STEP;
$f_B = floor((floor($b_B * $factor) + $offset) / COL_STEP) * COL_STEP;
return sprintf('#%02x%02x%02x', $f_R, $f_G, $f_B);
}
for( $i=0; $i<30; $i++ ) {
printf('<div style="height: 100px; width: 100px; background-color: %s"> </div>'."\n", usercolor(rand()));
}
Many colors will look pretty similar, but the chances that they'll be right next to each other are slim.
这篇关于使用PHP生成高对比度随机颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!