public partial class Form1 : Form { public Form1() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) { const int samples = 100; int height = 60; int width = this.ClientRectangle.Width / samples; for (int i = 0; i < samples; i++) { Color color = FromScale((double)i / samples); Rectangle rect = new Rectangle(width * i, 0, width, height); using (Brush brush = new SolidBrush(color)) { e.Graphics.FillRectangle(brush, rect); } } } static Color[] colors = { Color.Blue, Color.Cyan, Color.Yellow, Color.Red }; static Color FromScale(double value) { if (value < 0 || value > 1) throw new ArgumentException("value must be in [0~1]"); value *= (colors.Length - 1); int section = Math.Min((int)Math.Floor(value), colors.Length - 2); double t = value - section; Color c1 = colors[section], c2 = colors[section + 1]; return Color.FromArgb( Interpolate(t, c1.R, c2.R), Interpolate(t, c1.G, c2.G), Interpolate(t, c1.B, c2.B) ); } static int Interpolate(double t, int i, int j) { int value = (int)(i * (1 - t) + j * t); return Math.Min(255, Math.Max(0, value)); } }
意思也就是sample值是0-100 就对应的的是blue到red的过程中 对吧?
如果只要画图,不用根据数值求颜色,那么可以直接用LinearGradientBrush。 画刻度可以用Graphics.DrawLine和Graphics.DrawString。 public Form1() { InitializeComponent(); }protected override void OnPaint(PaintEventArgs e) { DrawTicks(e.Graphics, new Rectangle(10, 10, 30, 300)); }void DrawTicks(Graphics graphics, Rectangle fillRect) { // 填充颜色 using (LinearGradientBrush brush = new LinearGradientBrush(fillRect, Color.Empty, Color.Empty, -90)) { brush.InterpolationColors = new ColorBlend() { Positions = new float[] { 0f, 0.333f, 0.666f, 1f }, Colors = new Color[] { Color.Blue, Color.Cyan, Color.Yellow, Color.Red }, }; graphics.FillRectangle(brush, fillRect); } // 画刻度 float h = fillRect.Height / 10f, l = fillRect.X, r = fillRect.X + fillRect.Width - 1; Pen pen = Pens.Gray; StringFormat vCentered = new StringFormat() { LineAlignment = StringAlignment.Center }; for (int i = 1; i < 10; i++) { float y = h * i; graphics.DrawLine(pen, l, y, l + 3, y); graphics.DrawLine(pen, r - 3, y, r, y); RectangleF textBound = new RectangleF(r, y, 32, 0); textBound.Inflate(0, 15); graphics.DrawString("0." + i, SystemFonts.DefaultFont, Brushes.Black, textBound, vCentered); } }
{
public Form1()
{
InitializeComponent();
} protected override void OnPaint(PaintEventArgs e)
{
const int samples = 100;
int height = 60;
int width = this.ClientRectangle.Width / samples; for (int i = 0; i < samples; i++)
{
Color color = FromScale((double)i / samples);
Rectangle rect = new Rectangle(width * i, 0, width, height);
using (Brush brush = new SolidBrush(color))
{
e.Graphics.FillRectangle(brush, rect);
}
}
} static Color[] colors = { Color.Blue, Color.Cyan, Color.Yellow, Color.Red };
static Color FromScale(double value)
{
if (value < 0 || value > 1) throw new ArgumentException("value must be in [0~1]"); value *= (colors.Length - 1);
int section = Math.Min((int)Math.Floor(value), colors.Length - 2);
double t = value - section; Color c1 = colors[section], c2 = colors[section + 1];
return Color.FromArgb(
Interpolate(t, c1.R, c2.R),
Interpolate(t, c1.G, c2.G),
Interpolate(t, c1.B, c2.B)
);
}
static int Interpolate(double t, int i, int j)
{
int value = (int)(i * (1 - t) + j * t);
return Math.Min(255, Math.Max(0, value));
}
}
画刻度可以用Graphics.DrawLine和Graphics.DrawString。
public Form1()
{
InitializeComponent();
}protected override void OnPaint(PaintEventArgs e)
{
DrawTicks(e.Graphics, new Rectangle(10, 10, 30, 300));
}void DrawTicks(Graphics graphics, Rectangle fillRect)
{
// 填充颜色
using (LinearGradientBrush brush = new LinearGradientBrush(fillRect, Color.Empty, Color.Empty, -90))
{
brush.InterpolationColors = new ColorBlend()
{
Positions = new float[] { 0f, 0.333f, 0.666f, 1f },
Colors = new Color[] { Color.Blue, Color.Cyan, Color.Yellow, Color.Red },
};
graphics.FillRectangle(brush, fillRect);
} // 画刻度
float h = fillRect.Height / 10f, l = fillRect.X, r = fillRect.X + fillRect.Width - 1;
Pen pen = Pens.Gray;
StringFormat vCentered = new StringFormat() { LineAlignment = StringAlignment.Center };
for (int i = 1; i < 10; i++)
{
float y = h * i;
graphics.DrawLine(pen, l, y, l + 3, y);
graphics.DrawLine(pen, r - 3, y, r, y);
RectangleF textBound = new RectangleF(r, y, 32, 0); textBound.Inflate(0, 15);
graphics.DrawString("0." + i, SystemFonts.DefaultFont, Brushes.Black, textBound, vCentered);
}
}