Switched to SharpLibDisplay.
1 // --------------------------------------------------------------------------------------------------------------------
2 // <copyright file="ConverterExtensions.cs" company="OxyPlot">
3 // The MIT License (MIT)
5 // Copyright (c) 2012 Oystein Bjorke
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be included
16 // in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // Extension method used to convert to/from Windows/Windows.Media classes.
29 // --------------------------------------------------------------------------------------------------------------------
30 namespace OxyPlot.WindowsForms
34 using System.Windows.Forms;
37 /// Extension method used to convert to/from Windows/Windows.Media classes.
39 public static class ConverterExtensions
42 /// Calculate the distance between two points.
53 public static double DistanceTo(this Point p1, Point p2)
55 double dx = p1.X - p2.X;
56 double dy = p1.Y - p2.Y;
57 return Math.Sqrt((dx * dx) + (dy * dy));
61 /// Converts a color to a Brush.
67 /// A SolidColorBrush.
69 public static Brush ToBrush(this OxyColor c)
71 return new SolidBrush(c.ToColor());
75 /// Converts an OxyColor to a Color.
83 public static Color ToColor(this OxyColor c)
85 return Color.FromArgb(c.A, c.R, c.G, c.B);
89 /// Converts a HorizontalAlignment to a HorizontalTextAlign.
91 /// <param name="alignment">
95 /// A HorizontalTextAlign.
97 public static OxyPlot.HorizontalAlignment ToHorizontalTextAlign(this HorizontalAlignment alignment)
101 case HorizontalAlignment.Center:
102 return OxyPlot.HorizontalAlignment.Center;
103 case HorizontalAlignment.Right:
104 return OxyPlot.HorizontalAlignment.Right;
106 return OxyPlot.HorizontalAlignment.Left;
111 /// Converts a Color to an OxyColor.
113 /// <param name="color">
119 public static OxyColor ToOxyColor(this Color color)
121 return OxyColor.FromArgb(color.A, color.R, color.G, color.B);
125 /// Converts a nullable Color to an OxyColor.
127 /// <param name="color">
133 public static OxyColor ToOxyColor(this Color? color)
135 return color.HasValue ? color.Value.ToOxyColor() : null;
139 /// Converts a Brush to an OxyColor.
141 /// <param name="brush">
147 public static OxyColor ToOxyColor(this Brush brush)
149 var scb = brush as SolidBrush;
150 return scb != null ? scb.Color.ToOxyColor() : null;
154 /// Converts a Thickness to an OxyThickness.
157 /// An OxyPlot thickness.
160 /// Converts a ScreenPoint to a Point.
162 /// <param name="pt">
163 /// The screen point.
165 /// <param name="aliased">
166 /// use pixel alignment conversion if set to <c>true</c>.
171 public static Point ToPoint(this ScreenPoint pt, bool aliased)
173 // adding 0.5 to get pixel boundary alignment, seems to work
174 // http://weblogs.asp.net/mschwarz/archive/2008/01/04/silverlight-rectangles-paths-and-line-comparison.aspx
175 // http://www.wynapse.com/Silverlight/Tutor/Silverlight_Rectangles_Paths_And_Lines_Comparison.aspx
178 return new Point((int)pt.X, (int)pt.Y);
181 return new Point((int)Math.Round(pt.X), (int)Math.Round(pt.Y));
185 /// Converts an OxyRect to a Rect.
190 /// <param name="aliased">
191 /// use pixel alignment if set to <c>true</c>.
196 public static Rectangle ToRect(this OxyRect r, bool aliased)
202 var ri = (int)r.Right;
203 var bo = (int)r.Bottom;
204 return new Rectangle(x, y, ri - x, bo - y);
207 return new Rectangle(
208 (int)Math.Round(r.Left), (int)Math.Round(r.Top), (int)Math.Round(r.Width), (int)Math.Round(r.Height));
212 /// Converts a point to a ScreenPoint.
214 /// <param name="pt">
220 public static ScreenPoint ToScreenPoint(this Point pt)
222 return new ScreenPoint(pt.X, pt.Y);
226 /// Converts a Point array to a ScreenPoint array.
228 /// <param name="points">
232 /// A ScreenPoint array.
234 public static ScreenPoint[] ToScreenPointArray(this Point[] points)
241 var pts = new ScreenPoint[points.Length];
242 for (int i = 0; i < points.Length; i++)
244 pts[i] = points[i].ToScreenPoint();