All pastes #76850 Raw Edit

Unnamed

public text v1 · immutable
·published 2006-07-02 05:11 UTC
rendered paste body
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 != " ")
		{
			$what = "<font color="" . $hex[$k] . "">$char</font>".$what;
			$k++;
		}
		else
			$what = "&nbsp;".$what;	
	}
 return $what;
	
}