The easiest and most universal way to do this is with two functions: one for creating the hexadecimal HTML value from RGB arrays and one for crowding out RGB arrays ...
// Creates an HTML HEX color from an array of color integers array(r,g,b)... function rgb($rgb) { $ret = ''; foreach ($rgb as $x) { // Make sure the RGB values are 0-255... $x = max(0, min(255, $x)); // create a 2 digit hex value for this color component... $ret .= ($x < 16 ? '0'.dechex($x) : dechex($x)); } return '#'.$ret; } // Returns a fade of $from into $to by $amount ($from and $to are RGB arrays)... function fade($from, $to, $amount) { $rgb = array(); // Go through the RGB values and adjust the values by $amount... for ($i = 0; $i <= 2; $i++) { $rgb[$i] = (($to[$i] - $from[$i]) * $amount) + $from[$i]; } return $rgb; }
Then you can extrude any color with any other color for any amount, for example, this example, which disappears from red to green with 100 steps ...
for ($fadeAmount = 0; $fadeAmount < 1; $fadeAmount += 0.01) { $color = rgb(fade(array(255,0,0), array(0,255,0), $fadeAmount)); echo "<div style='color:$color'>$fadeAmount</div>"; }
source share