aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/RealTimeGraphX.WPF/ExtensionMethods/BrushExtensions.cs
blob: 41721312f404c03a9c5395ff3841029a8f8ada91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;

internal static class BrushExtensions
{
    internal static Brush ToGdiBrush(this System.Windows.Media.Brush brush)
    {
        if (brush.GetType() == typeof(System.Windows.Media.SolidColorBrush))
        {
            return new SolidBrush((brush as System.Windows.Media.SolidColorBrush).Color.ToGdiColor());
        }
        else if (brush.GetType() == typeof(System.Windows.Media.LinearGradientBrush))
        {
            System.Windows.Media.LinearGradientBrush b = brush as System.Windows.Media.LinearGradientBrush;

            double angle = Math.Atan2(b.EndPoint.Y - b.StartPoint.Y, b.EndPoint.X - b.StartPoint.X) * 180 / Math.PI;

            LinearGradientBrush gradient = new LinearGradientBrush(new Rectangle(0, 0, 200, 100), Color.Black, Color.Black, (float)angle);

            ColorBlend blend = new ColorBlend();

            List<Color> colors = new List<Color>();
            List<float> offsets = new List<float>();

            foreach (var stop in b.GradientStops)
            {
                colors.Add(stop.Color.ToGdiColor());
                offsets.Add((float)stop.Offset);
            }

            blend.Colors = colors.ToArray();
            blend.Positions = offsets.ToArray();

            gradient.InterpolationColors = blend;

            return gradient;
        }
        else
        {
            return new LinearGradientBrush(new PointF(0, 0), new Point(200, 100), Color.Black, Color.Black);
        }
    }
}