|
Posted by Dennis de Wit on 07/24/06 11:42
Dennis de Wit wrote:
> Hi all,
>
> I am trying to generate sort-of random colors. I want to be able to
> generate n colors with the biggest variety of bright colors.
>
> If for example I want 3 colours i might have
> - red
> - green
> - blue
>
> If i wanted 6 colors i might have
> - red
> - green
> - blue
> - yellow
> - cyan
> - pink
>
> or something similar. Does anyone know a routine to do so? It should
> also work for 15 or even 100 colors. I'm not getting any further than
> making an array of predefined colors, which doesn't help me out far enough.
>
> Any smart ideas?
>
> Thanks in advance,
>
> Dennis
This is all I can come up to for now. It generates a spectrum from red
trough green trough blue back to red. It then selects N colours from the
list and displays them.
<?
function mkHex(){
$arg = func_get_args();
$res = '';
for ($i=0; $i<count($arg); $i++){
$res .= str_pad(base_convert($arg[$i], 10, 16), 2, '0', STR_PAD_LEFT);
}
return $res;
}
$colours = array();
for ($i=0; $i<255; $i++){
$colours[] = mkhex(255-$i, $i, 0);
}
for ($i=0; $i<255; $i++){
$colours[] = mkhex(0, 255-$i, $i);
}
for ($i=0; $i<255; $i++){
$colours[] = mkhex($i, 0, 255-$i);
}
$selected = array();
$amount = 10;
$step = floor(count($colours) / $amount);
for ($i=0; $i<$amount; $i++){
$selected[] = $colours[$i*$step];
}
for ($i=0;$i<count($selected);$i++){
echo '<div style="display: inline; height: 20px; background: #';
echo $selected[$i];
echo ';"> </div>';
}
?>
I think I should include more colours somehow... Any tips?
Dennis
Navigation:
[Reply to this message]
|