Eric Biron
Eric Biron

Reputation: 77

LinearGradientBrush with InterpolateColors does not render correctly with small position values

I was trying to make a ColorBlend editor with draggable position marker when I noticed that sometime, it does not seems to render correctly.

Using a LinearGradientBrush with an InterpolationColors fading from white to blue to white again and the middle position for blue is 0.02f. The middle gradient does not seems to be aligned with the position I provided.

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    var colorBlend = new ColorBlend {
        Colors = new[] { Color.White, Color.Blue, Color.White },
        Positions = new[] { 0f, 0.02f, 1f }
    };

    var rect = new RectangleF(10, 10, 500, 50);

    //colors in constructor are not supposed to matter, and InterpolationColors should override them
    using(var brush = new LinearGradientBrush(rect, Color.Aqua, Color.Black, LinearGradientMode.Horizontal)
        {InterpolationColors = colorBlend})
        e.Graphics.FillRectangle(brush, rect);

    for (var i = 0; i < colorBlend.Positions.Length; i++)
    {
        var x = rect.X + colorBlend.Positions[i] * rect.Width;
        e.Graphics.DrawLine(Pens.Black, x, rect.Bottom, x, rect.Bottom + 20);
    }
}

GradientTest

The pure blue should be aligned with the middle marker, but the gradient continues a little after the marker before going back to white again.

When I use a position of 0.2f for the blue, the gradient seems fine.

Upvotes: 1

Views: 139

Answers (0)

Related Questions