// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) 2014 OxyPlot contributors // // // Extension method used to convert to/from Windows/Windows.Media classes. // // -------------------------------------------------------------------------------------------------------------------- #if OXYPLOT_COREDRAWING namespace OxyPlot.Core.Drawing #else namespace OxyPlot.WindowsForms #endif { using System; using System.Drawing; /// /// Extension method used to convert to/from Windows/Windows.Media classes. /// public static class DrawingConverterExtensions { /// /// Calculate the distance between two points. /// /// The first point. /// The second point. /// The distance. public static double DistanceTo(this Point p1, Point p2) { double dx = p1.X - p2.X; double dy = p1.Y - p2.Y; return Math.Sqrt((dx * dx) + (dy * dy)); } /// /// Converts a color to a Brush. /// /// The color. /// A SolidColorBrush. public static Brush ToBrush(this OxyColor c) { return new SolidBrush(c.ToColor()); } /// /// Converts an OxyColor to a Color. /// /// The color. /// A Color. public static Color ToColor(this OxyColor c) { return Color.FromArgb(c.A, c.R, c.G, c.B); } /// /// Converts a HorizontalAlignment to a HorizontalTextAlign. /// /// The alignment. /// A HorizontalTextAlign. public static OxyPlot.HorizontalAlignment ToHorizontalTextAlign(this HorizontalAlignment alignment) { switch (alignment) { case HorizontalAlignment.Center: return OxyPlot.HorizontalAlignment.Center; case HorizontalAlignment.Right: return OxyPlot.HorizontalAlignment.Right; default: return OxyPlot.HorizontalAlignment.Left; } } /// /// Converts a to an . /// /// The color to convert. /// An . public static OxyColor ToOxyColor(this Color color) { return OxyColor.FromArgb(color.A, color.R, color.G, color.B); } /// /// Converts a to an . /// /// The brush to convert. /// An . public static OxyColor ToOxyColor(this Brush brush) { var scb = brush as SolidBrush; return scb != null ? scb.Color.ToOxyColor() : OxyColors.Undefined; } /// /// Converts a Thickness to an OxyThickness. /// /// The screen point. /// use pixel alignment conversion if set to true. /// An OxyPlot thickness. public static Point ToPoint(this ScreenPoint pt, bool aliased) { // adding 0.5 to get pixel boundary alignment, seems to work // http://weblogs.asp.net/mschwarz/archive/2008/01/04/silverlight-rectangles-paths-and-line-comparison.aspx // http://www.wynapse.com/Silverlight/Tutor/Silverlight_Rectangles_Paths_And_Lines_Comparison.aspx if (aliased) { return new Point((int)pt.X, (int)pt.Y); } return new Point((int)Math.Round(pt.X), (int)Math.Round(pt.Y)); } /// /// Converts an to a . /// /// The rectangle. /// use pixel alignment if set to true. /// A . public static Rectangle ToRect(this OxyRect r, bool aliased) { if (aliased) { var x = (int)r.Left; var y = (int)r.Top; var ri = (int)r.Right; var bo = (int)r.Bottom; return new Rectangle(x, y, ri - x, bo - y); } return new Rectangle( (int)Math.Round(r.Left), (int)Math.Round(r.Top), (int)Math.Round(r.Width), (int)Math.Round(r.Height)); } /// /// Converts a point to a ScreenPoint. /// /// The point. /// A screen point. public static ScreenPoint ToScreenPoint(this Point pt) { return new ScreenPoint(pt.X, pt.Y); } /// /// Converts a Point array to a ScreenPoint array. /// /// The points. /// A ScreenPoint array. public static ScreenPoint[] ToScreenPointArray(this Point[] points) { if (points == null) { return null; } var pts = new ScreenPoint[points.Length]; for (int i = 0; i < points.Length; i++) { pts[i] = points[i].ToScreenPoint(); } return pts; } } }