note 51400 deleted from function.dechex by googleguy

From: Date: Tue, 22 Oct 2013 10:17:24 +0000
Subject: note 51400 deleted from function.dechex by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196979@lists.php.net to get a copy of this message
Note Submitter: manithu ---- If you want to fade some text from one color to another, use this function: <?php function colorFade($text, $color1, $color2, $HTMLelement = 'span') { //remove leading and trailing whitespaces. $text = trim($text); //split the characters of the text to an array. $char = array(); for ($i = 0; $i < strlen($text); $i++) { $char[$i] = substr($text, $i, 1); } //convert the hexadecimal colors in decimal. $decimalColor = array(); //first color $decimalColor[0] = hexdec(substr($color1, 0, 2)); $decimalColor[1] = hexdec(substr($color1, 2, 2)); $decimalColor[2] = hexdec(substr($color1, 4, 2)); //second color $decimalColor[3] = hexdec(substr($color2, 0, 2)); $decimalColor[4] = hexdec(substr($color2, 2, 2)); $decimalColor[5] = hexdec(substr($color2, 4, 2)); //formated chars are stored here (the key 0 is for the first //char and must not be removed). $newText = array(0 => null); //run over every character except the first and the last, //they will be added later. for ($i = 2; $i <= strlen($text)-1; $i++) { //check if the character is only a whitespace. If yes, //save the whitespace without adding color and proceed //to the next character. if (!trim($char[$i-1])) { $newText[] = $char[$i-1]; continue; } //calculate the (hex) color for this character. $color = array(); //red $color[] = dechex(($decimalColor[0] - $decimalColor[3]) / strlen($text) * $i + $decimalColor[3]); //green $color[] = dechex(($decimalColor[1] - $decimalColor[4]) / strlen($text) * $i + $decimalColor[4]); //blue $color[] = dechex(($decimalColor[2] - $decimalColor[5]) / strlen($text) * $i + $decimalColor[5]); //check if every color part is 2 chars long, if not, repeat it. foreach ($color as $k => $v) { if (strlen($v) < 2) { $color[$k] = str_repeat($v, 2); } } //merge the parts to the full hex color. $color = implode($color, ''); //Save the formated char. $newText[] = '<'.$HTMLelement.' style="color:#'.$color.'">'. $char[$i-1].'</'.$HTMLelement.'>'; } //add first char. $newText[0] = '<'.$HTMLelement.' style="color:#'.$color2.'">'. $char[0].'</'.$HTMLelement.'>'; //add last char. $newText[] = '<'.$HTMLelement.' style="color:#'.$color1.'">'. $char[strlen($text)-1].'</'.$HTMLelement.'>'; //merge the characters and return it. return implode($newText, ''); } ?> $color1 is the start color, $color2 the end color. I hope this helps somebody.

« previous php.notes (#196979) next »