带有InterpolateColors的LinearGradientBrush无法在较小的位置值下正确呈现

时间:2019-05-03 13:41:05

标签: c# gdi+ lineargradientbrush

当我发现某个时候,它似乎无法正确呈现时,我试图用可拖动的位置标记制作一个ColorBlend编辑器。

使用带有InterpolationColor的LinearGradientBrush,颜色再次从白色变为蓝色,变为白色,蓝色的中间位置为0.02f。中间渐变似乎与我提供的位置不一致。

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

纯蓝色应该与中间标记对齐,但是渐变会在标记之后持续一点,然后再次变回白色。

当我对蓝色使用0.2f的位置时,渐变似乎很好。

0 个答案:

没有答案