<p>What's the easiest way to convert a percentage to a color ranging from Green (100%) to Red (0%), with Yellow for 50%?</p>

<p>I'm using plain 32bit RGB - so each component is an integer between 0 and 255. I'm doing this in C#, but I guess for a problem like this the language doesn't really matter that much.</p>

<p>Based on Marius and Andy's answers I'm using the following solution:</p>

<pre><code>double red = (percent &lt; 50) ? 255 : 256 - (percent - 50) * 5.12;
double green = (percent &gt; 50) ? 255 : percent * 5.12;
var color = Color.FromArgb(255, (byte)red, (byte)green, 0);
</code></pre>

<p>Works perfectly - Only adjustment I had to make from Marius solution was to use 256, as (255 - (percent - 50) * 5.12 yield -1 when 100%, resulting in Yellow for some reason in Silverlight (-1, 255, 0) -> Yellow ...</p>

As yellow is a mix of red and green, you can probably start with
```
#F00
```
and then slide green up until you hit
```
#FF0
```
, then slide red down to
```
#0F0
```
:
```
```
for (int i = 0; i < 100; i++) {
    var red = i < 50
        ? 255
        : 255 - (256.0 / 100 * ((i - 50) * 2));
    var green = i < 50
        ? 256.0 / 100 * (i * 2)
        : 255;
    var col = Color.FromArgb((int) red, (int) green, 0);
}
```
```