Skip to content

Instantly share code, notes, and snippets.

@Ravaelles
Created April 16, 2018 09:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Ravaelles/84f8236dd8e4f629b72debab0751c0b1 to your computer and use it in GitHub Desktop.
Save Ravaelles/84f8236dd8e4f629b72debab0751c0b1 to your computer and use it in GitHub Desktop.
PHP function that converts number from given range into a color from given gradient of multiple colors
// See example: https://image.prntscr.com/image/cTIv8JkLR7yWEQxVev9SyA.jpeg
function numberToColor($value, $min, $max, $gradientColors = null)
{
// Ensure value is in range
if ($value < $min) {
$value = $min;
}
if ($value > $max) {
$value = $max;
}
// Normalize min-max range to [0, positive_value]
$max -= $min;
$value -= $min;
$min = 0;
// Calculate distance from min to max in [0,1]
$distFromMin = $value / $max;
// Define start and end color
if (count($gradientColors) == 0) {
return self::colorFromRange($value, $min, $max, ['#CC0000', '#EEEE00', '#00FF00']);
} else if (count($gradientColors) == 2) {
$startColor = $gradientColors[0];
$endColor = $gradientColors[1];
} else if (count($gradientColors) > 2) {
$startColor = $gradientColors[floor($distFromMin * (count($gradientColors) - 1))];
$endColor = $gradientColors[ceil($distFromMin * (count($gradientColors) - 1))];
$distFromMin *= count($gradientColors) - 1;
while ($distFromMin > 1) {
$distFromMin--;
}
} else {
die("Please pass more than one color or null to use default red-green colors.");
}
// Remove hex from string
if ($startColor[0] === '#') {
$startColor = substr($startColor, 1);
}
if ($endColor[0] === '#') {
$endColor = substr($endColor, 1);
}
// Parse hex
list($ra, $ga, $ba) = sscanf("#$startColor", "#%02x%02x%02x");
list($rz, $gz, $bz) = sscanf("#$endColor", "#%02x%02x%02x");
// Get rgb based on
$distFromMin = $distFromMin;
$distDiff = 1 - $distFromMin;
$r = intval(($rz * $distFromMin) + ($ra * $distDiff));
$r = min(max(0, $r), 255);
$g = intval(($gz * $distFromMin) + ($ga * $distDiff));
$g = min(max(0, $g), 255);
$b = intval(($bz * $distFromMin) + ($ba * $distDiff));
$b = min(max(0, $b), 255);
// Convert rgb back to hex
$rgbColorAsHex = '#' .
str_pad(dechex($r), 2, "0", STR_PAD_LEFT) .
str_pad(dechex($g), 2, "0", STR_PAD_LEFT) .
str_pad(dechex($b), 2, "0", STR_PAD_LEFT);
return $rgbColorAsHex;
}
// ====================================================
for ($i = 0; $i <= 20; $i++) {
$color = \App\Helpers\ColorHelper::colorFromRange(
$i, 0, 20
);
echo "<div style='font-weight:bold; display:inline-block; width:30px; font-size:16px; color: black; background-color:" . $color . "; text-align:center'>$i</div>";
}
echo "<br /><br />";
for ($i = 0; $i <= 20; $i++) {
$color = \App\Helpers\ColorHelper::colorFromRange(
$i, 0, 20, ['#CC0000', '#EEEE00', '#00AA00', '#0088EE']
);
echo "<div style='font-weight:bold; display:inline-block; width:30px; font-size:16px; color: black; background-color:" . $color . "; text-align:center'>$i</div>";
}
die;
@Rictus
Copy link

Rictus commented Apr 26, 2018

Really usefull function, thanks !

Change line 23 from :
return self::colorFromRange($value, $min, $max, ['#CC0000', '#EEEE00', '#00FF00']);
to :
return numberToColor($value, $min, $max, ['#CC0000', '#EEEE00', '#00FF00']);

@connect-technologies
Copy link

very nice!
thanks a lot!

@ivesbrito
Copy link

thanks 4 ur code! =)

@koreldan
Copy link

koreldan commented May 1, 2019

very usefull... for a nice result... thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment