note 97218 deleted from function.hexdec by danbrown
| From: | danbrown@php.net | Date: | Thu, 08 Apr 2010 16:28:30 +0000 |
| Subject: | note 97218 deleted from function.hexdec by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168038@lists.php.net to get a copy of this message | ||
Note Submitter: Adam @ My Learning UK Ltd
----
Here's a function I wrote to fade from one hex colour into another and returns all the colours
as a nice handy array.
Eg. $fade = hexFade('#ff0000','#00ff00',10);
/**
* Fade from start hex colour into another over x amount of steps
*
* @param mixed $hexStart Hex Colour to start. eg #ff0000
* @param mixed $hexEnd Hex Colour to end up with. eg. #ff00ff
* @param mixed $midSteps Steps to take to get from start colour to end colour
* @return array All colours used to get from start to finish, including start and finish.
*/
function hexFade($hStart,$hEnd,$midSteps=10){
$hStart = str_replace("#","",$hStart);
$hEnd = str_replace("#","",$hEnd);
$hS = array_map("hexdec",str_split($hStart,2));
$hE = array_map("hexdec",str_split($hEnd,2));
$cA = array("#$hStart");
for($i = $midSteps; $i >= 1; $i--){
$c1 = str_pad(dechex(($hS[0] - $hE[0])/($midSteps+1) * $i +
$hE[0]),2,"0",STR_PAD_LEFT);
$c2 = str_pad(dechex(($hS[1] - $hE[1])/($midSteps+1) * $i +
$hE[1]),2,"0",STR_PAD_LEFT);
$c3 = str_pad(dechex(($hS[2] - $hE[2])/($midSteps+1) * $i +
$hE[2]),2,"0",STR_PAD_LEFT);
$cA[] = "#$c1$c2$c3";
}
$cA[] = "#$hEnd";
return $cA;
}