External/OxyPlot/OxyPlot.WindowsForms/Helpers/ConverterExtensions.cs
author StephaneLenclud
Thu, 18 Apr 2013 23:25:10 +0200
branchMiniDisplay
changeset 444 9b09e2ee0968
permissions -rw-r--r--
Front View plug-in does not init if no sensor added.
Fixing some format to make strings shorter.
Now trying to start SoundGraphAccess.exe process from same directory.
Packed mode now can display three sensors along with the current time.
     1 // --------------------------------------------------------------------------------------------------------------------
     2 // <copyright file="ConverterExtensions.cs" company="OxyPlot">
     3 //   The MIT License (MIT)
     4 //
     5 //   Copyright (c) 2012 Oystein Bjorke
     6 //
     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:
    14 //
    15 //   The above copyright notice and this permission notice shall be included
    16 //   in all copies or substantial portions of the Software.
    17 //
    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.
    25 // </copyright>
    26 // <summary>
    27 //   Extension method used to convert to/from Windows/Windows.Media classes.
    28 // </summary>
    29 // --------------------------------------------------------------------------------------------------------------------
    30 namespace OxyPlot.WindowsForms
    31 {
    32     using System;
    33     using System.Drawing;
    34     using System.Windows.Forms;
    35 
    36     /// <summary>
    37     /// Extension method used to convert to/from Windows/Windows.Media classes.
    38     /// </summary>
    39     public static class ConverterExtensions
    40     {
    41         /// <summary>
    42         /// Calculate the distance between two points.
    43         /// </summary>
    44         /// <param name="p1">
    45         /// The first point.
    46         /// </param>
    47         /// <param name="p2">
    48         /// The second point.
    49         /// </param>
    50         /// <returns>
    51         /// The distance.
    52         /// </returns>
    53         public static double DistanceTo(this Point p1, Point p2)
    54         {
    55             double dx = p1.X - p2.X;
    56             double dy = p1.Y - p2.Y;
    57             return Math.Sqrt((dx * dx) + (dy * dy));
    58         }
    59 
    60         /// <summary>
    61         /// Converts a color to a Brush.
    62         /// </summary>
    63         /// <param name="c">
    64         /// The color.
    65         /// </param>
    66         /// <returns>
    67         /// A SolidColorBrush.
    68         /// </returns>
    69         public static Brush ToBrush(this OxyColor c)
    70         {
    71             return new SolidBrush(c.ToColor());
    72         }
    73 
    74         /// <summary>
    75         /// Converts an OxyColor to a Color.
    76         /// </summary>
    77         /// <param name="c">
    78         /// The color.
    79         /// </param>
    80         /// <returns>
    81         /// A Color.
    82         /// </returns>
    83         public static Color ToColor(this OxyColor c)
    84         {
    85             return Color.FromArgb(c.A, c.R, c.G, c.B);
    86         }
    87 
    88         /// <summary>
    89         /// Converts a HorizontalAlignment to a HorizontalTextAlign.
    90         /// </summary>
    91         /// <param name="alignment">
    92         /// The alignment.
    93         /// </param>
    94         /// <returns>
    95         /// A HorizontalTextAlign.
    96         /// </returns>
    97         public static OxyPlot.HorizontalAlignment ToHorizontalTextAlign(this HorizontalAlignment alignment)
    98         {
    99             switch (alignment)
   100             {
   101                 case HorizontalAlignment.Center:
   102                     return OxyPlot.HorizontalAlignment.Center;
   103                 case HorizontalAlignment.Right:
   104                     return OxyPlot.HorizontalAlignment.Right;
   105                 default:
   106                     return OxyPlot.HorizontalAlignment.Left;
   107             }
   108         }
   109 
   110         /// <summary>
   111         /// Converts a Color to an OxyColor.
   112         /// </summary>
   113         /// <param name="color">
   114         /// The color.
   115         /// </param>
   116         /// <returns>
   117         /// An OxyColor.
   118         /// </returns>
   119         public static OxyColor ToOxyColor(this Color color)
   120         {
   121             return OxyColor.FromArgb(color.A, color.R, color.G, color.B);
   122         }
   123 
   124         /// <summary>
   125         /// Converts a nullable Color to an OxyColor.
   126         /// </summary>
   127         /// <param name="color">
   128         /// The color.
   129         /// </param>
   130         /// <returns>
   131         /// An OxyColor.
   132         /// </returns>
   133         public static OxyColor ToOxyColor(this Color? color)
   134         {
   135             return color.HasValue ? color.Value.ToOxyColor() : null;
   136         }
   137 
   138         /// <summary>
   139         /// Converts a Brush to an OxyColor.
   140         /// </summary>
   141         /// <param name="brush">
   142         /// The brush.
   143         /// </param>
   144         /// <returns>
   145         /// An oxycolor.
   146         /// </returns>
   147         public static OxyColor ToOxyColor(this Brush brush)
   148         {
   149             var scb = brush as SolidBrush;
   150             return scb != null ? scb.Color.ToOxyColor() : null;
   151         }
   152 
   153         /// <summary>
   154         /// Converts a Thickness to an OxyThickness.
   155         /// </summary>
   156         /// <returns>
   157         /// An OxyPlot thickness.
   158         /// </returns>
   159         /// <summary>
   160         /// Converts a ScreenPoint to a Point.
   161         /// </summary>
   162         /// <param name="pt">
   163         /// The screen point.
   164         /// </param>
   165         /// <param name="aliased">
   166         /// use pixel alignment conversion if set to <c>true</c>.
   167         /// </param>
   168         /// <returns>
   169         /// A point.
   170         /// </returns>
   171         public static Point ToPoint(this ScreenPoint pt, bool aliased)
   172         {
   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
   176             if (aliased)
   177             {
   178                 return new Point((int)pt.X, (int)pt.Y);
   179             }
   180 
   181             return new Point((int)Math.Round(pt.X), (int)Math.Round(pt.Y));
   182         }
   183 
   184         /// <summary>
   185         /// Converts an OxyRect to a Rect.
   186         /// </summary>
   187         /// <param name="r">
   188         /// The rectangle.
   189         /// </param>
   190         /// <param name="aliased">
   191         /// use pixel alignment if set to <c>true</c>.
   192         /// </param>
   193         /// <returns>
   194         /// A rect.
   195         /// </returns>
   196         public static Rectangle ToRect(this OxyRect r, bool aliased)
   197         {
   198             if (aliased)
   199             {
   200                 var x = (int)r.Left;
   201                 var y = (int)r.Top;
   202                 var ri = (int)r.Right;
   203                 var bo = (int)r.Bottom;
   204                 return new Rectangle(x, y, ri - x, bo - y);
   205             }
   206 
   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));
   209         }
   210 
   211         /// <summary>
   212         /// Converts a point to a ScreenPoint.
   213         /// </summary>
   214         /// <param name="pt">
   215         /// The point.
   216         /// </param>
   217         /// <returns>
   218         /// A screen point.
   219         /// </returns>
   220         public static ScreenPoint ToScreenPoint(this Point pt)
   221         {
   222             return new ScreenPoint(pt.X, pt.Y);
   223         }
   224 
   225         /// <summary>
   226         /// Converts a Point array to a ScreenPoint array.
   227         /// </summary>
   228         /// <param name="points">
   229         /// The points.
   230         /// </param>
   231         /// <returns>
   232         /// A ScreenPoint array.
   233         /// </returns>
   234         public static ScreenPoint[] ToScreenPointArray(this Point[] points)
   235         {
   236             if (points == null)
   237             {
   238                 return null;
   239             }
   240 
   241             var pts = new ScreenPoint[points.Length];
   242             for (int i = 0; i < points.Length; i++)
   243             {
   244                 pts[i] = points[i].ToScreenPoint();
   245             }
   246 
   247             return pts;
   248         }
   249 
   250     }
   251 }