I want to display to my user how many (percent wise) of their forms are compliant with the new standard. The way I want to let them know visually is the percent amount will be colored. It will be 0xFF0000 (pure red) for 0% and 0x00FF00 (pure green) at 100%. What is the best way to calculate the color for each step along the way?
A:
Have a look here:
http://stackoverflow.com/questions/1106959/how-do-i-calculate-a-four-colour-gradient
Tony
2010-06-08 14:41:57
no, look at http://stackoverflow.com/questions/668263/algorithm-question-need-to-dynamically-increment-from-00ff00-to-ff0000-over-time/669786#669786 which is an exact duplicate - a linear gradient from green to red.
Pete Kirkham
2010-06-08 15:41:20
+1
A:
You don't need to calculate it yourself - try using a LinearGradient
brush. (msdn)
LinearGradientBrush linGrBrush = new LinearGradientBrush(
new Point(0, 10),
new Point(200, 10),
Color.FromArgb(255, 255, 0, 0), // Opaque red
Color.FromArgb(255, 0, 0, 255)); // Opaque blue
Pen pen = new Pen(linGrBrush);
e.Graphics.DrawLine(pen, 0, 10, 200, 10);
e.Graphics.FillEllipse(linGrBrush, 0, 30, 200, 100);
e.Graphics.FillRectangle(linGrBrush, 0, 155, 500, 30);
Phil
2010-06-08 14:44:06
+1
A:
Colour space conversion (as suggested by Tony) will give you the best results. If however this is beyond the scope of what you are looking for, I suggest a simple algorithm that gets you yellow (0xFFFF00) for 50 %:
For values up to 50 % Start with 0xFF0000.
Add 0xFF * Percentage / 50 to the green component.
For values above 50 % Start with 0xFFFF00.
Subtract 0xFF * Percentage / 50 from the red component.
The results look good enough for my customers ;-)
Treb
2010-06-08 14:50:23