Colorfade

Colorfade

first var is the start color (in hex - FFFFFF)

second is the finishing fade color (same hex format).

third var is the string. So if you want to fade from red to green “PHPFreaks Rocks!” you would put:

colorfade_string(”FF0000″, “00FF00″, “PHP Rocks!”);

function colorfade_string($hex_start, $hex_end, $string)
{
$realLen = trim(strlen($string));

//we need to skip spaces
$n_spaces = substr_count($string, ” “);

$len = $realLen - $n_spaces;

if($len == 0)
die(”String length = 0″);

if(!ereg(”([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})”, $hex_start, $firstcolor))
die(”First Color is Invalid”);

if(!ereg(”([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})”, $hex_end, $endcolor))
die(”Second Color is invalid”);

$start_1 = hexdec($firstcolor[1]);
$start_2 = hexdec($firstcolor[2]);
$start_3 = hexdec($firstcolor[3]);

$end_1 = hexdec($endcolor[1]);
$end_2 = hexdec($endcolor[2]);
$end_3 = hexdec($endcolor[3]);

$diff1 = $end_1 - $start_1;
$diff2 = $end_2 - $start_2;
$diff3 = $end_3 - $start_3;

$step1 = round($diff1 / $len);
$step2 = round($diff2 / $len);
$step3 = round($diff3 / $len);

$red = $start_1;
$blue = $start_2;
$green = $start_3;

for($i = 0; $i < $len; $i++)
{
$hex[] = sprintf(”%02X%02X%02X”, $red, $blue, $green);

$red += $step1;
$blue += $step2;
$green += $step3;
}

for($i = 0, $k = 0; $i < $realLen; $i++)
{
$char = substr($string, $i, 1);

if($char != ” “)
{
print(’<font color=”‘ . $hex[$k] . ‘”>$char</font>’);
$k++;
}
else
print(” “);

}

}

Leave a Reply


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.