note 42619 added to function.dechex
| From: | (@web.de) | Date: | Mon, 24 May 2004 03:46:54 +0000 |
| Subject: | note 42619 added to function.dechex | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-70011@lists.php.net to get a copy of this message | ||
this function generates an hex-colorcode out of a string.
usefull if you want to show a list of words all in different colors, that should remain its color
even if the order changes.
(if the string is empty, it gives back a random color)
function word2color($w){
if (strlen($w)==0) return substr('00000' . dechex(mt_rand(0, 0xffffff)), -6);
while (strlen($w)<6) $w.=$w;
$minbrightness=1; // range from 0 to 15, if this is 0 then for ex. black is allowed
$max_brightness=14; // range from 0 to 15, if this is 15 then for ex. white is allowed
$plus_red=0; // set one of these to set the probability of one of these colors higher
$plus_green=0;
$plus_blue=0;
for ($i=0; $i<6; $i++) {
#$r.= '">';// this is a depug mode, to see the color written
$plus=0;
if ($plus_red<>0 and $i==0) $plus=$plus_red;
if ($plus_green<>0 and $i==2) $plus=$plus_green;
if ($plus_blue<>0 and $i==4) $plus=$plus_blue;
$c=$w[round(strlen($w)/6*$i)];
$dec=ord($c)%($max_brightness+$plus-$minbrightness) +$minbrightness+$plus;
if ($dec>$max_brightness-$minbrightness) $dec=$max_brightness-$minbrightness;
$r.= strtoupper( dechex($dec) );
}
return $r;
}
----
Manual Page -- http://www.php.net/manual/en/function.dechex.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42619
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42619&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42619&report=yes
Search -- http://master.php.net/manage/user-notes.php