Skip to content

Instantly share code, notes, and snippets.

@bendauphinee
Created April 11, 2014 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bendauphinee/10472641 to your computer and use it in GitHub Desktop.
Save bendauphinee/10472641 to your computer and use it in GitHub Desktop.
This code can be used to generate heatmap blue to red color gradients. Blog: http://blog.bendauphinee.com/2011/12/php-heatmap-gradient-color-generator/
<?php
// Generate an array of color hex codes between a start and an end color
function heatSteps ($config) {
$config['colorStart'] = (isset($config['colorStart']) == true) ? hexdec($config['colorStart']) : 0xdee7f8;
$config['colorEnd'] = (isset($config['colorEnd']) == true) ? hexdec($config['colorEnd']) : 0xff0f15;
$config['colorSteps'] = (isset($config['colorSteps']) == true) ? $config['colorSteps'] : 10;
$config['colorStart'] = (($config['colorStart'] >= 0x000000) && ($config['colorStart'] <= 0xffffff)) ? $config['colorStart'] : 0x000000;
$config['colorEnd'] = (($config['colorEnd'] >= 0x000000) && ($config['colorEnd'] <= 0xffffff)) ? $config['colorEnd'] : 0xffffff;
$config['colorSteps'] = (($config['colorSteps'] > 0) && ($config['colorSteps'] < 256)) ? $config['colorSteps'] : 256;
$theR0 = ($config['colorStart'] & 0xff0000) >> 16;
$theG0 = ($config['colorStart'] & 0x00ff00) >> 8;
$theB0 = ($config['colorStart'] & 0x0000ff) >> 0;
$theR1 = ($config['colorEnd'] & 0xff0000) >> 16;
$theG1 = ($config['colorEnd'] & 0x00ff00) >> 8;
$theB1 = ($config['colorEnd'] & 0x0000ff) >> 0;
$colorSteps = array();
for($i = 0; $i <= $config['colorSteps']; $i++){
$theR = interpolateHeatSteps($theR0, $theR1, $i, $config['colorSteps']);
$theG = interpolateHeatSteps($theG0, $theG1, $i, $config['colorSteps']);
$theB = interpolateHeatSteps($theB0, $theB1, $i, $config['colorSteps']);
$colorSteps[] = dechex(((($theR << 8) | $theG) << 8) | $theB);
}
return($colorSteps);
}
function interpolateHeatSteps ($pBegin, $pEnd, $pStep, $pMax) {
if ($pBegin < $pEnd) {
return(($pEnd - $pBegin) * ($pStep / $pMax)) + $pBegin;
} else {
return(($pBegin - $pEnd) * (1 - ($pStep / $pMax))) + $pEnd;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment