PHP Red to Green RGB Color Heatmap

Viewed 8415

I can't seem to get this figured out. I have a scaled set of values (0...1) that I need to associate colors with. The highest (1) being red and the lowest (0) being green.

I cannot seem to find how to get an RGB color between red and green for a value that is between 0 and 1.

Here is my scaling function I am going to use to scale the values:

function scale_value($value, $srcmin, $srcmax, $destmin = 0, $destmax = 1)
{
    # How Far In Source Range Are We
    $pos = (($value - $srcmin) / ($srcmax - $srcmin));
    return ($pos * ($destmax - $destmin)) + $destmin;
}

I figured scaling them from 0 to 1 will make the next part I am struggling with much easier.

Here is one very crummy attempt at doing this I came up with, failed pretty badly.

function make_color($value)
{
    $red = $value > 0.5
       ? (1 - 2 * ($value - 0.5) / 1)
       : 1;
    $green = $value > 0.5
        ? 1
        : 2 * ($value / 1);
    $blue = 0;
    return "rgb($red,$green,$blue)";
}

Does anyone have any experience using PHP to determine the color to use for a value that falls between 1 and 0?

4 Answers

This will allow you to choose your own color gradient scale.

enter image description here

function Gradient($HexFrom, $HexTo, $ColorSteps) {
    // credit: Hailwood (stackoverflow)
    $FromRGB['r'] = hexdec(substr($HexFrom, 0, 2));
    $FromRGB['g'] = hexdec(substr($HexFrom, 2, 2));
    $FromRGB['b'] = hexdec(substr($HexFrom, 4, 2));

    $ToRGB['r'] = hexdec(substr($HexTo, 0, 2));
    $ToRGB['g'] = hexdec(substr($HexTo, 2, 2));
    $ToRGB['b'] = hexdec(substr($HexTo, 4, 2));

    $StepRGB['r'] = ($FromRGB['r'] - $ToRGB['r']) / ($ColorSteps - 1);
    $StepRGB['g'] = ($FromRGB['g'] - $ToRGB['g']) / ($ColorSteps - 1);
    $StepRGB['b'] = ($FromRGB['b'] - $ToRGB['b']) / ($ColorSteps - 1);

    $GradientColors = array();

    for($i = 0; $i <= $ColorSteps; $i++) {
        $RGB['r'] = floor($FromRGB['r'] - ($StepRGB['r'] * $i));
        $RGB['g'] = floor($FromRGB['g'] - ($StepRGB['g'] * $i));
        $RGB['b'] = floor($FromRGB['b'] - ($StepRGB['b'] * $i));

        $HexRGB['r'] = sprintf('%02x', ($RGB['r']));
        $HexRGB['g'] = sprintf('%02x', ($RGB['g']));
        $HexRGB['b'] = sprintf('%02x', ($RGB['b']));

        $GradientColors[] = implode(NULL, $HexRGB);
    }
    $GradientColors = array_filter($GradientColors, function($val){
        return (strlen($val) == 6 ? true : false );
    });
    return $GradientColors;
}

function ColorFromGradient($n, $min, $max, $colors)
{
    $tablecolors = [];
    $prevcolor = array_shift($colors);
    foreach ($colors as $color) {
        $tablecolors = array_merge($tablecolors, Gradient($prevcolor, $color, 10));
        $prevcolor = $color;
    }
    $max = $max-$min;
    $n-= $min;
    if ($n > $max) $n = $max;
    
    $ncolor = round(count($tablecolors)/$max * $n)-1;
    
    return $tablecolors[$ncolor];
}


// USAGE: Generate the Gradient Table
echo ColorFromGradient(25, 0, 100, ['FFFFFF', 'FFFF00', 'FF0000']); // WHITE -> YELLOW -> RED
   //this makes very good colors between red and green
   //return green value between 0 - 255
   //$red = 255 * ($percentage*0.01); //between 0 - 1
    $red = 255 * 0.5;
    $green = getredgreen($red);
    echo "rgb(".$red.",".$green.",0)";
    
    function getredgreen($hodnota){
      $odpich = 120;
      $maxrozdil = 100;
      if ($hodnota > ($odpich - $maxrozdil) 
       && $hodnota < ($odpich + $maxrozdil))
      {
      $rozdilek = abs($hodnota - $odpich);
      $okolik = $maxrozdil - $rozdilek;
      $hodnota = $hodnota + $okolik;
      } 
      return $hodnota;
    }
Related