-1

I have an IEnumerable of Color which I want to use as the basis for a brush.

At the moment, I convert the IEnumerable into a Bitmap, into a bitmapsource, into an imagebrush, but this is a bit slow, is there any brush class which can do what I want in a faster way?

edit, What I want to do: Use the brush in a pen to draw a line in a drawing visual, where the IEnumerable of Color is used as line color. If I have a collection of { Colours.Green, Colours.Red}, I want the resulting line to be half green, half red.

Nattfrosten
  • 1,999
  • 4
  • 16
  • 21

1 Answers1

0

Here's a method that would convert your IEnumerable to a LinearGradientBrush. There are 2 gradient stops for each color in order to create a hard transition between colours rather than a gradient.

LinearGradientBrush CreateBrush(IEnumerable<Color> colors) {

    var colorArray = colors.ToArray();
    var step = 1.0 / colorArray.Length;

    var gradientStops = new GradientStopCollection();

    for (int i = 0; i < colorArray.Length; i++) {
        var color = colorArray[i];
        gradientStops.Add(new GradientStop(color, i * step));
        gradientStops.Add(new GradientStop(color, (i + 1) * step));
    }

    return new LinearGradientBrush(gradientStops);
}
wilford
  • 586
  • 1
  • 6
  • 17