Skip to content

Instantly share code, notes, and snippets.

@meminuygur
Created September 23, 2017 00:17
Show Gist options
  • Save meminuygur/a600c4f57e7bc6d2e84cc739b70f77b9 to your computer and use it in GitHub Desktop.
Save meminuygur/a600c4f57e7bc6d2e84cc739b70f77b9 to your computer and use it in GitHub Desktop.
<?php
$begin = '75B4CA';
$end = '98F29F';
print_r(getRangeColors($begin, $end, 96));
function getRangeColors($begin, $end, $theNumSteps){
$theColorBegin = hexdec($begin);
$theColorEnd = hexdec($end);
$theColorBegin = (($theColorBegin >= 0x000000) && ($theColorBegin <= 0xffffff)) ? $theColorBegin : 0x000000;
$theColorEnd = (($theColorEnd >= 0x000000) && ($theColorEnd <= 0xffffff)) ? $theColorEnd : 0xffffff;
$theNumSteps = (($theNumSteps > 0) && ($theNumSteps < 256)) ? $theNumSteps : 16;
$theR0 = ($theColorBegin & 0xff0000) >> 16;
$theG0 = ($theColorBegin & 0x00ff00) >> 8;
$theB0 = ($theColorBegin & 0x0000ff) >> 0;
$theR1 = ($theColorEnd & 0xff0000) >> 16;
$theG1 = ($theColorEnd & 0x00ff00) >> 8;
$theB1 = ($theColorEnd & 0x0000ff) >> 0;
$result = [];
for ($i = 0; $i <= $theNumSteps; $i++) {
$theR = interpolate($theR0, $theR1, $i, $theNumSteps);
$theG = interpolate($theG0, $theG1, $i, $theNumSteps);
$theB = interpolate($theB0, $theB1, $i, $theNumSteps);
$theVal = ((($theR << 8) | $theG) << 8) | $theB;
$theTDARTag = sprintf("#%06X", $theVal);
$result[] = $theTDARTag;
}
return $result;
}
function interpolate($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