Colorfade
<?php
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”);
// Converting hexa decimal to decimal
$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;
// Calculating hex[] array using the start and step value
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++)
{
// Code to fetch each character with different color
$char = substr($string, $i, 1);
if($char != ” “)
{
print(”<font color=’ . $hex[$k] . ‘>$char</font>”);
$k++;
}
else
print(” ”);
}
}
echo colorfade_string(”aabbcc”, “aaabbb”, “This is Sample text”);
?>
In above function, we can pass start color,end shade color and the string. In colorfade_string() funtion, we can display text with fading effect.
