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 { /// /// Contains a collection of extension methods. /// internal static class ExtensionMethods { /// /// Converts this WPF color to a GDI color. /// /// The color. /// internal static Color ToGdiColor(this System.Windows.Media.Color color) { return Color.FromArgb(color.A, color.R, color.G, color.B); } /// /// Converts this WPF brush to a GDI brush. /// /// The brush. /// 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 colors = new List(); List offsets = new List(); 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); } } /// /// Determines whether this dependency object is running in design mode. /// /// The object. internal static bool IsInDesignMode(this System.Windows.DependencyObject obj) { return (DesignerProperties.GetIsInDesignMode(obj)); } } }