note 51400 added to function.dechex

From: Date: Tue, 29 Mar 2005 20:18:59 +0000
Subject: note 51400 added to function.dechex
Groups: php.notes 
Request: Send a blank email to php-notes+get-87161@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/function.dechex.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+51400 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51400&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51400&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51400&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51400&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51400&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+51400&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#87161) next »