gradient miedzy czterema kolorami w prostokacie

0

jak w temacie , jak zrobić gradient miedzy czterema punktami (wierzchołki prostokąta - każdy inny kolor) w tym prostokącie

z góry dzienks ; )

Zrobiłem coś takiego ale jeszcze wypełnia mi jakaś bielą od środka jak się tego pozbyć ?;/ a może inny sposób ? ; (

GraphicsPath gp = new GraphicsPath();
gp.AddRectangle(new System.Drawing.Rectangle(100, 100, 300, 300));

        PathGradientBrush pgb = new PathGradientBrush(gp);

       // pgb.CenterColor = System.Drawing.Color.White;

        pgb.SurroundColors = new System.Drawing.Color[] {
                             System.Drawing.Color.Red,
                             System.Drawing.Color.Blue,
                             System.Drawing.Color.Green,
                             System.Drawing.Color.Yellow};

        

        e.Graphics.FillPath(pgb,gp);
0

Ponizej masz dwa sposoby: szybki i ladny:

private Color Interpolate(Color color1, Color color2, float ratio1)
{
	float ratio2 = 1 - ratio1;
	byte a = (byte)Math.Round(color1.A * ratio1 + color2.A * ratio2);
	byte r = (byte)Math.Round(color1.R * ratio1 + color2.R * ratio2);
	byte g = (byte)Math.Round(color1.G * ratio1 + color2.G * ratio2);
	byte b = (byte)Math.Round(color1.B * ratio1 + color2.B * ratio2);
	return Color.FromArgb(a, r, g, b);
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
	Color[] colors = new Color[4] { Color.Red, Color.Green, Color.Yellow, Color.Blue };
	Rectangle rectangle = new Rectangle(10, 10, 255, 255);

	#region Samodzielna interpolacja
	Bitmap bitmap = new Bitmap(rectangle.Width, rectangle.Height);
	Graphics graphics = Graphics.FromImage(bitmap);
	float ratioX;
	float ratioY;
	for (int x = 0; x < rectangle.Width; x++)
	{
		ratioX = x / (float)rectangle.Width;
		for (int y = 0; y < rectangle.Height; y++)
		{
			ratioY = y / (float)rectangle.Height;
			bitmap.SetPixel(x, y, Interpolate(
				Interpolate(colors[2], colors[3], ratioX), 
				Interpolate(colors[1], colors[0], ratioX), ratioY));
		}
	}
	e.Graphics.DrawImage(bitmap, rectangle.Left + 300, rectangle.Top);
	#endregion

	#region Pedzel gradientowy
	GraphicsPath graphicsPath = new GraphicsPath();
	graphicsPath .AddRectangle(rectangle);
	PathGradientBrush pathGradientBrush = new PathGradientBrush(graphicsPath);
	pathGradientBrush.SurroundColors = colors;
	e.Graphics.FillRectangle(pathGradientBrush, rectangle);
	#endregion
}

Pierwszy kod nie jest optymalny, ale pokazuje zasade dzialania algorytmu.

0

Dziękować [browar] :-)

1 użytkowników online, w tym zalogowanych: 0, gości: 1