We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21560
    • 145 Posts
    Some part of my code that I use:

    There are basically two functions, one that uses an array of colors and divides them equally over 100% and another that does the actual fading between 2 colors.

    The functions:
    function getColor($start, $end, $percent) {
    	$sr=hexdec(substr($start, 0, 2));
    	$sg=hexdec(substr($start, 2, 2));
    	$sb=hexdec(substr($start, 4, 2));
    	
    	$er=hexdec(substr($end, 0, 2));
    	$eg=hexdec(substr($end, 2, 2));
    	$eb=hexdec(substr($end, 4, 2));
    	
    	$pc = $percent/100;
    	
    	$r = floor($sr+($pc*($er-$sr)) + .5);
    	$g = floor($sg+($pc*($eg-$sg)) + .5);
    	$b = floor($sb+($pc*($eb-$sb)) + .5);
    	
    	return(sprintf('%02X%02X%02X', $r, $g, $b));
    }
    function getGradient($colors, $percent) {
    	$div = count($colors) - 1;
    	$pct = (($percent * $div * 100) % 10000) / 100;   //Partial percentage, still quite precise.
    	$cur = floor($percent / 100 * $div); //Current start color.
    	if ($percentage < 100 && $percent > 0) {
    		$color = getColor($colors[$cur], $colors[$cur + 1], $pct);
    	} elseif ($percent >= 100) {
    		$color = $colors[$div];
    	} else {
    		$color = $colors[0];
    	}
    	return $color;
    }


    Usage is actually very easy, just make an array of HEX colors (in strings)
    $colors = array(’FF0000’,’FF9900’,’FFFF00’);

    And call it with the percentage you want (can be ’precise’)
    $current = getGradient($colors, 25);

    In this example, the returned color will be between FF0000(red) and FF9900(orange) (so around FF4500)
      [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
      • 21560
      • 145 Posts
      As a small sidenote. I’ve already improved it so that I can easily change the array of colors for the site.

      That, with a changed background, now gives my site a winter theme.
        [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com