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?
