moel@391: // -------------------------------------------------------------------------------------------------------------------- moel@391: // moel@391: // The MIT License (MIT) moel@391: // moel@391: // Copyright (c) 2012 Oystein Bjorke moel@391: // moel@391: // Permission is hereby granted, free of charge, to any person obtaining a moel@391: // copy of this software and associated documentation files (the moel@391: // "Software"), to deal in the Software without restriction, including moel@391: // without limitation the rights to use, copy, modify, merge, publish, moel@391: // distribute, sublicense, and/or sell copies of the Software, and to moel@391: // permit persons to whom the Software is furnished to do so, subject to moel@391: // the following conditions: moel@391: // moel@391: // The above copyright notice and this permission notice shall be included moel@391: // in all copies or substantial portions of the Software. moel@391: // moel@391: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS moel@391: // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF moel@391: // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. moel@391: // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY moel@391: // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, moel@391: // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE moel@391: // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. moel@391: // moel@391: // moel@391: // Extension method used to convert to/from Windows/Windows.Media classes. moel@391: // moel@391: // -------------------------------------------------------------------------------------------------------------------- moel@391: namespace OxyPlot.WindowsForms moel@391: { moel@391: using System; moel@391: using System.Drawing; moel@391: using System.Windows.Forms; moel@391: moel@391: /// moel@391: /// Extension method used to convert to/from Windows/Windows.Media classes. moel@391: /// moel@391: public static class ConverterExtensions moel@391: { moel@391: /// moel@391: /// Calculate the distance between two points. moel@391: /// moel@391: /// moel@391: /// The first point. moel@391: /// moel@391: /// moel@391: /// The second point. moel@391: /// moel@391: /// moel@391: /// The distance. moel@391: /// moel@391: public static double DistanceTo(this Point p1, Point p2) moel@391: { moel@391: double dx = p1.X - p2.X; moel@391: double dy = p1.Y - p2.Y; moel@391: return Math.Sqrt((dx * dx) + (dy * dy)); moel@391: } moel@391: moel@391: /// moel@391: /// Converts a color to a Brush. moel@391: /// moel@391: /// moel@391: /// The color. moel@391: /// moel@391: /// moel@391: /// A SolidColorBrush. moel@391: /// moel@391: public static Brush ToBrush(this OxyColor c) moel@391: { moel@391: return new SolidBrush(c.ToColor()); moel@391: } moel@391: moel@391: /// moel@391: /// Converts an OxyColor to a Color. moel@391: /// moel@391: /// moel@391: /// The color. moel@391: /// moel@391: /// moel@391: /// A Color. moel@391: /// moel@391: public static Color ToColor(this OxyColor c) moel@391: { moel@391: return Color.FromArgb(c.A, c.R, c.G, c.B); moel@391: } moel@391: moel@391: /// moel@391: /// Converts a HorizontalAlignment to a HorizontalTextAlign. moel@391: /// moel@391: /// moel@391: /// The alignment. moel@391: /// moel@391: /// moel@391: /// A HorizontalTextAlign. moel@391: /// moel@391: public static OxyPlot.HorizontalAlignment ToHorizontalTextAlign(this HorizontalAlignment alignment) moel@391: { moel@391: switch (alignment) moel@391: { moel@391: case HorizontalAlignment.Center: moel@391: return OxyPlot.HorizontalAlignment.Center; moel@391: case HorizontalAlignment.Right: moel@391: return OxyPlot.HorizontalAlignment.Right; moel@391: default: moel@391: return OxyPlot.HorizontalAlignment.Left; moel@391: } moel@391: } moel@391: moel@391: /// moel@391: /// Converts a Color to an OxyColor. moel@391: /// moel@391: /// moel@391: /// The color. moel@391: /// moel@391: /// moel@391: /// An OxyColor. moel@391: /// moel@391: public static OxyColor ToOxyColor(this Color color) moel@391: { moel@391: return OxyColor.FromArgb(color.A, color.R, color.G, color.B); moel@391: } moel@391: moel@391: /// moel@391: /// Converts a nullable Color to an OxyColor. moel@391: /// moel@391: /// moel@391: /// The color. moel@391: /// moel@391: /// moel@391: /// An OxyColor. moel@391: /// moel@391: public static OxyColor ToOxyColor(this Color? color) moel@391: { moel@391: return color.HasValue ? color.Value.ToOxyColor() : null; moel@391: } moel@391: moel@391: /// moel@391: /// Converts a Brush to an OxyColor. moel@391: /// moel@391: /// moel@391: /// The brush. moel@391: /// moel@391: /// moel@391: /// An oxycolor. moel@391: /// moel@391: public static OxyColor ToOxyColor(this Brush brush) moel@391: { moel@391: var scb = brush as SolidBrush; moel@391: return scb != null ? scb.Color.ToOxyColor() : null; moel@391: } moel@391: moel@391: /// moel@391: /// Converts a Thickness to an OxyThickness. moel@391: /// moel@391: /// moel@391: /// An OxyPlot thickness. moel@391: /// moel@391: /// moel@391: /// Converts a ScreenPoint to a Point. moel@391: /// moel@391: /// moel@391: /// The screen point. moel@391: /// moel@391: /// moel@391: /// use pixel alignment conversion if set to true. moel@391: /// moel@391: /// moel@391: /// A point. moel@391: /// moel@391: public static Point ToPoint(this ScreenPoint pt, bool aliased) moel@391: { moel@391: // adding 0.5 to get pixel boundary alignment, seems to work moel@391: // http://weblogs.asp.net/mschwarz/archive/2008/01/04/silverlight-rectangles-paths-and-line-comparison.aspx moel@391: // http://www.wynapse.com/Silverlight/Tutor/Silverlight_Rectangles_Paths_And_Lines_Comparison.aspx moel@391: if (aliased) moel@391: { moel@391: return new Point((int)pt.X, (int)pt.Y); moel@391: } moel@391: moel@391: return new Point((int)Math.Round(pt.X), (int)Math.Round(pt.Y)); moel@391: } moel@391: moel@391: /// moel@391: /// Converts an OxyRect to a Rect. moel@391: /// moel@391: /// moel@391: /// The rectangle. moel@391: /// moel@391: /// moel@391: /// use pixel alignment if set to true. moel@391: /// moel@391: /// moel@391: /// A rect. moel@391: /// moel@391: public static Rectangle ToRect(this OxyRect r, bool aliased) moel@391: { moel@391: if (aliased) moel@391: { moel@391: var x = (int)r.Left; moel@391: var y = (int)r.Top; moel@391: var ri = (int)r.Right; moel@391: var bo = (int)r.Bottom; moel@391: return new Rectangle(x, y, ri - x, bo - y); moel@391: } moel@391: moel@391: return new Rectangle( moel@391: (int)Math.Round(r.Left), (int)Math.Round(r.Top), (int)Math.Round(r.Width), (int)Math.Round(r.Height)); moel@391: } moel@391: moel@391: /// moel@391: /// Converts a point to a ScreenPoint. moel@391: /// moel@391: /// moel@391: /// The point. moel@391: /// moel@391: /// moel@391: /// A screen point. moel@391: /// moel@391: public static ScreenPoint ToScreenPoint(this Point pt) moel@391: { moel@391: return new ScreenPoint(pt.X, pt.Y); moel@391: } moel@391: moel@391: /// moel@391: /// Converts a Point array to a ScreenPoint array. moel@391: /// moel@391: /// moel@391: /// The points. moel@391: /// moel@391: /// moel@391: /// A ScreenPoint array. moel@391: /// moel@391: public static ScreenPoint[] ToScreenPointArray(this Point[] points) moel@391: { moel@391: if (points == null) moel@391: { moel@391: return null; moel@391: } moel@391: moel@391: var pts = new ScreenPoint[points.Length]; moel@391: for (int i = 0; i < points.Length; i++) moel@391: { moel@391: pts[i] = points[i].ToScreenPoint(); moel@391: } moel@391: moel@391: return pts; moel@391: } moel@391: moel@391: } moel@391: }