+10  A: 
Paul Dixon
If R, G, B and n are integer types, make sure you re-write those expressions to do the multiplication first or you won't get smooth transitions. i.e. R=(255*n)/100
Ferruccio
It's much, much better to do this in HSV space.
DJClayworth
@DJClayworth, yea I went with HSV.
Simucal
+1; I was going to ask a new question about how to improve a color algorithm i have for coloring a bar chart in HTML/PHP... SO suggested this question as similar and your answer helped me fix the issue without having to ask the question! Thanks!
beggs
+3  A: 

I have previously asked the same (extremely similar) question here:

http://stackoverflow.com/questions/168838/color-scaling-function

Tomas Pajonk
A: 

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?

TK
Nice to know how to do it yourself though
Paul Dixon
This is true, altthough, this method could work for any color gradient without having to worry too much about the Gradient Stops.
TK
+2  A: 

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.

NeARAZ
A: 

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.

efotinis
+5  A: 

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.

Rafał Dowgird
A: 

i need to write a program that produces a set of colors that are linearly interploated between any two positions in HLS space? can someone please help me with this please?

kunal
i need the program to be implemented in C/C++..
kunal