Skip to content

Instantly share code, notes, and snippets.

@tjnurmin
Last active July 17, 2021 23:44
Show Gist options
  • Save tjnurmin/6670022 to your computer and use it in GitHub Desktop.
Save tjnurmin/6670022 to your computer and use it in GitHub Desktop.
Generate heat map colors through interpolation
// declare start, mid, end colors
$c = array(
array(0,0,0),
array(128,128,128),
array(255,255,255)
);
$loops = 127;
for ($t = 0; $t <= $loops; $t++)
{
$e = $t / $loops;
echo "(";
echo intval((1 - $e) * $c[0][0] + $e * $c[1][0]) . ",";
echo intval((1 - $e) * $c[0][1] + $e * $c[1][1]) . ",";
echo intval((1 - $e) * $c[0][2] + $e * $c[1][2]);
echo "),<br />";
}
$loops = 128;
for ($t = 0; $t <= $loops; $t++)
{
$e = $t / $loops;
if ($t > 0) // skip the mid color which is already in the array
{
echo "(";
echo intval((1 - $e) * $c[1][0] + $e * $c[2][0]) . ",";
echo intval((1 - $e) * $c[1][1] + $e * $c[2][1]) . ",";
echo intval((1 - $e) * $c[1][2] + $e * $c[2][2]);
echo "),<br />";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment