I have previously asked the same (extremely similar) question here:
http://stackoverflow.com/questions/168838/color-scaling-function
If I were asked to do this, could you not generate a color gradient?
I know in C# you can quite easily, then just access a color part way through the gradient. Is this possible in JAVA?
Linearly interpolating between green and red almost should work, except that in the middle there will be muddy brown color.
The most flexible and best looking solution is to have an image file somewhere that has the exact color ramp you want. And then lookup the pixel value in there. This has the flexibility that you can tweak the gradient to be just right.
If you still want to do it from code, then it's probably best to interpolate between green and yellow colors in the left side, and between yellow and red on the right side. In RGB space, green is (R=0, G=255, B=0), yellow is (R=255, G=255, B=0), red is (R=255, G=0, B=0) - this is assuming each color component goes from 0 to 255.
You need to linearly interpolate (LERP) the color components. Here's how it's done in general, given a start value v0, an end value v1 and the required ratio (a normalized float between 0.0 and 1.0):
v = v0 + ratio * (v1 - v0)
This gives v0 when ratio is 0.0, v1 when ratio is 1.0, and everything between in the other cases.
You can do this either on the RGB components, or using some other color scheme, like HSV or HLS. The latter two will be more visually pleasing, since they work on hue and brightness compoments that map better to our color perception.
Off the top of my head, here is the green-red hue transition in HSV space, translated to RGB:
blue = 0.0
if 0<=power<0.5: #first, green stays at 100%, red raises to 100%
green = 1.0
red = 2 * power
if 0.5<=power<=1: #then red stays at 100%, green decays
red = 1.0
green = 1.0 - 2 * (power-0.5)
The red, green, blue values in the above example are percentages, you'd probably want to multiply them by 255 to get the most used 0-255 range.