Reputation:
i have GRP tracks on map, which have difficulty from 1 to 10. I want these tracks to have different color depending on difficulty. Easy difficulty is green, hard is red and medium is orange. However, i need to have not just these 3 colors but smoother transitions between them. like one color for every point of difficulty or one for every half point. Is there any algorithm to compute these colors and theirs transitions? Like somehow increasing/decreasing rgb values? Thank you.
Upvotes: 1
Views: 232
Reputation: 14517
using System;
using System.Drawing;
using System.Windows.Forms;
public partial class MainForm : Form
{
protected override void OnPaint(PaintEventArgs e)
{
Color[] colors = new Color[11];
colors[0] = Color.Green;
colors[1] = Interpolate(Color.Green, Color.Orange, 0.2);
colors[2] = Interpolate(Color.Green, Color.Orange, 0.4);
colors[3] = Interpolate(Color.Green, Color.Orange, 0.6);
colors[4] = Interpolate(Color.Green, Color.Orange, 0.8);
colors[5] = Color.Orange;
colors[6] = Interpolate(Color.Orange, Color.Red, 0.2);
colors[7] = Interpolate(Color.Orange, Color.Red, 0.4);
colors[8] = Interpolate(Color.Orange, Color.Red, 0.6);
colors[9] = Interpolate(Color.Orange, Color.Red, 0.8);
colors[10] = Color.Red;
Rectangle rect = new Rectangle(10, 10, 20, 90);
for (int i = 0; i < colors.Length; i++)
{
e.Graphics.FillRectangle(new SolidBrush(colors[i]), rect);
rect.Offset(20, 0);
}
base.OnPaint(e);
}
private static Color Interpolate(Color a, Color b, double t)
{
int R = (int)(a.R + (b.R - a.R) * t);
int G = (int)(a.G + (b.G - a.G) * t);
int B = (int)(a.B + (b.B - a.B) * t);
return Color.FromArgb(R, G, B);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
Upvotes: 0
Reputation: 10896
What you're looking for is the ability to interpolate across colors. It might not be enough to simply interpolate across two colors (because you might get spurious colors in between) but to treat each color's RGB as a point in 3D space and interpolate along a poly-line (where each point is a color/intermediate color) that represents colors (in your case, difficulties). Here's a link that shows what I'm talking about.
Upvotes: 1
Reputation: 4519
Red = 255,0,0
Yellow = 255, 255, 0
Green = 0,255,0
Make a for loop which loops from level 1 to 10.
For each level create the color as something like:
Red = level * 25.5;
Green = 255 - (level * 25.5);
Blue = 0;
Then create the color from the RGB values.
Obviously this is only pseudo-code but it should give you an idea to get started.
Upvotes: 1