// -------------------------------------------------------------------------------------------------------------------- // // The MIT License (MIT) // // Copyright (c) 2012 Oystein Bjorke // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // // Extension method used to convert to/from Windows/Windows.Media classes. // // -------------------------------------------------------------------------------------------------------------------- namespace OxyPlot.WindowsForms { using System; using System.Drawing; using System.Windows.Forms; /// /// Extension method used to convert to/from Windows/Windows.Media classes. /// public static class ConverterExtensions { /// /// 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 Color to an OxyColor. /// /// /// The color. /// /// /// An OxyColor. /// public static OxyColor ToOxyColor(this Color color) { return OxyColor.FromArgb(color.A, color.R, color.G, color.B); } /// /// Converts a nullable Color to an OxyColor. /// /// /// The color. /// /// /// An OxyColor. /// public static OxyColor ToOxyColor(this Color? color) { return color.HasValue ? color.Value.ToOxyColor() : null; } /// /// Converts a Brush to an OxyColor. /// /// /// The brush. /// /// /// An oxycolor. /// public static OxyColor ToOxyColor(this Brush brush) { var scb = brush as SolidBrush; return scb != null ? scb.Color.ToOxyColor() : null; } /// /// Converts a Thickness to an OxyThickness. /// /// /// An OxyPlot thickness. /// /// /// Converts a ScreenPoint to a Point. /// /// /// The screen point. /// /// /// use pixel alignment conversion if set to true. /// /// /// A point. /// 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 OxyRect to a Rect. /// /// /// The rectangle. /// /// /// use pixel alignment if set to true. /// /// /// A rect. /// 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; } } }