Victor wrote:
Can someone help me with the formula(s) to calculate the RGB
components for a gradient from green to red? I'm trying to display a
nice graphic with a gradient that reflects the profit or loss and I
can calculate the value of red or green at each point based on the
value of the profit or loss, but I need to calculate the values of the
other 2 colors.
The exact formula will depend on what you want for intermediate colors.
However, you should get decent results simply by calculating a linear
function in which one component starts at 100% and decreases to 0%,
while the other starts at 0% and increases to 100%.
For example:
Color ColorGradientGreenToRed(float index)
{
if (index < 0 || index 1.0)
{
throw new ArgumentException(
"index must be between 0 and 1");
}
return Color.FromArgb((1.0 - index) * 255, index * 255, 0);
}
For more complicated gradients (eg you aren't starting and ending at a
pure red, green, or blue value) it's often helpful to do the operation
in HSV space, where you only vary the hue according to your index and
then convert back to RGB. The general idea is the same though.
Pete