aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/RealTimeGraphX-master/RealTimeGraphX.WPF/ExtensionMethods.cs
blob: 39f1139249718413dcaee22799b50571b4532aa2 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RealTimeGraphX.WPF
{
    /// <summary>
    /// Contains a collection of extension methods.
    /// </summary>
    internal static class ExtensionMethods
    {
        /// <summary>
        /// Converts this WPF color to a GDI color.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <returns></returns>
        internal static Color ToGdiColor(this System.Windows.Media.Color color)
        {
            return Color.FromArgb(color.A, color.R, color.G, color.B);
        }

        /// <summary>
        /// Converts this WPF brush to a GDI brush.
        /// </summary>
        /// <param name="brush">The brush.</param>
        /// <returns></returns>
        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);
            }
        }

        /// <summary>
        /// Determines whether this dependency object is running in design mode.
        /// </summary>
        /// <param name="obj">The object.</param>
        internal static bool IsInDesignMode(this System.Windows.DependencyObject obj)
        {
            return (DesignerProperties.GetIsInDesignMode(obj));
        }
    }
}