1

当我注意到有时它似乎无法正确渲染时,我试图制作一个带有可拖动位置标记的 ColorBlend 编辑器。

使用带有 InterpolationColors 的 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);
    }
}

梯度测试

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

当我为蓝色使用 0.2f 的位置时,渐变看起来很好。

4

0 回答 0