Creates the stepped gradient brush (any number of steps). - CSharp System.Windows.Media

CSharp examples for System.Windows.Media:Brush

Description

Creates the stepped gradient brush (any number of steps).

Demo Code


using System.Windows.Media;
using System.Windows;
using System.Linq;
using System.Collections.Generic;

public class Main{
        /// <summary>
        /// Creates the stepped gradient brush (any number of steps).
        /// </summary>
        /// <param name="gradient">
        /// The gradient.
        /// </param>
        /// <param name="steps">
        /// The number of steps.
        /// </param>
        /// <returns>
        /// </returns>
        public static LinearGradientBrush CreateSteppedGradientBrush(LinearGradientBrush gradient, int steps)
        {//from  w  w  w .j a  v a  2s.co  m
            var brush = new LinearGradientBrush { StartPoint = gradient.StartPoint, EndPoint = gradient.EndPoint };
            int n = gradient.GradientStops.Count;
            for (int i = 0; i < steps; i++)
            {
                double index = 1.0 * i / (steps - 1) * (n - 1);
                var i0 = (int)index;
                double f = index - i0;
                int i1 = i0 + 1;
                if (i1 >= n)
                {
                    i1 = n - 1;
                }

                var c0 = gradient.GradientStops[i0].Color;
                var c1 = gradient.GradientStops[i1].Color;
                var gs0 = new GradientStop();
                var gs1 = new GradientStop();
                gs0.Color = ColorHelper.Interpolate(c0, c1, f);
                gs1.Color = gs0.Color;
                gs0.Offset = 1.0 * i / steps;
                gs1.Offset = 1.0 * (i + 1) / steps;
                brush.GradientStops.Add(gs0);
                brush.GradientStops.Add(gs1);
            }

            return brush;
        }
        /// <summary>
        /// Creates the stepped gradient brush (same number of steps as the number of stops in the gradient).
        /// </summary>
        /// <param name="gradient">
        /// The gradient.
        /// </param>
        /// <returns>
        /// </returns>
        public static LinearGradientBrush CreateSteppedGradientBrush(LinearGradientBrush gradient)
        {
            var brush = new LinearGradientBrush { StartPoint = gradient.StartPoint, EndPoint = gradient.EndPoint };
            for (int i = 0; i + 1 < gradient.GradientStops.Count; i++)
            {
                var gs0 = gradient.GradientStops[i].Clone();
                var gs1 = gs0.Clone();
                gs1.Offset = gradient.GradientStops[i + 1].Offset;
                brush.GradientStops.Add(gs0);
                brush.GradientStops.Add(gs1);
            }

            return brush;
        }
        /// <summary>
        /// Creates a 'stepped' gradient brush from a list of colors.
        /// </summary>
        /// <param name="colors">The colors.</param>
        /// <param name="horizontal">if set to <c>true</c> [horizontal].</param>
        /// <returns>A gradientbrush.</returns>
        public static LinearGradientBrush CreateSteppedGradientBrush(IList<Color> colors, bool horizontal = true)
        {
            var brush = new LinearGradientBrush { StartPoint = new Point(0, 0), EndPoint = horizontal ? new Point(1, 0) : new Point(0, 1) };
            int n = colors.Count;
            for (int i = 0; i < n; i++)
            {
                var gs0 = new GradientStop(colors[i], (double)i / n);
                var gs1 = new GradientStop(colors[i], (double)(i + 1) / n);
                brush.GradientStops.Add(gs0);
                brush.GradientStops.Add(gs1);
            }

            return brush;
        }
}

Related Tutorials