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.
moel@391
     1
// --------------------------------------------------------------------------------------------------------------------
moel@391
     2
// <copyright file="ConverterExtensions.cs" company="OxyPlot">
moel@391
     3
//   The MIT License (MIT)
moel@391
     4
//
moel@391
     5
//   Copyright (c) 2012 Oystein Bjorke
moel@391
     6
//
moel@391
     7
//   Permission is hereby granted, free of charge, to any person obtaining a
moel@391
     8
//   copy of this software and associated documentation files (the
moel@391
     9
//   "Software"), to deal in the Software without restriction, including
moel@391
    10
//   without limitation the rights to use, copy, modify, merge, publish,
moel@391
    11
//   distribute, sublicense, and/or sell copies of the Software, and to
moel@391
    12
//   permit persons to whom the Software is furnished to do so, subject to
moel@391
    13
//   the following conditions:
moel@391
    14
//
moel@391
    15
//   The above copyright notice and this permission notice shall be included
moel@391
    16
//   in all copies or substantial portions of the Software.
moel@391
    17
//
moel@391
    18
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
moel@391
    19
//   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
moel@391
    20
//   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
moel@391
    21
//   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
moel@391
    22
//   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
moel@391
    23
//   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
moel@391
    24
//   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
moel@391
    25
// </copyright>
moel@391
    26
// <summary>
moel@391
    27
//   Extension method used to convert to/from Windows/Windows.Media classes.
moel@391
    28
// </summary>
moel@391
    29
// --------------------------------------------------------------------------------------------------------------------
moel@391
    30
namespace OxyPlot.WindowsForms
moel@391
    31
{
moel@391
    32
    using System;
moel@391
    33
    using System.Drawing;
moel@391
    34
    using System.Windows.Forms;
moel@391
    35
moel@391
    36
    /// <summary>
moel@391
    37
    /// Extension method used to convert to/from Windows/Windows.Media classes.
moel@391
    38
    /// </summary>
moel@391
    39
    public static class ConverterExtensions
moel@391
    40
    {
moel@391
    41
        /// <summary>
moel@391
    42
        /// Calculate the distance between two points.
moel@391
    43
        /// </summary>
moel@391
    44
        /// <param name="p1">
moel@391
    45
        /// The first point.
moel@391
    46
        /// </param>
moel@391
    47
        /// <param name="p2">
moel@391
    48
        /// The second point.
moel@391
    49
        /// </param>
moel@391
    50
        /// <returns>
moel@391
    51
        /// The distance.
moel@391
    52
        /// </returns>
moel@391
    53
        public static double DistanceTo(this Point p1, Point p2)
moel@391
    54
        {
moel@391
    55
            double dx = p1.X - p2.X;
moel@391
    56
            double dy = p1.Y - p2.Y;
moel@391
    57
            return Math.Sqrt((dx * dx) + (dy * dy));
moel@391
    58
        }
moel@391
    59
moel@391
    60
        /// <summary>
moel@391
    61
        /// Converts a color to a Brush.
moel@391
    62
        /// </summary>
moel@391
    63
        /// <param name="c">
moel@391
    64
        /// The color.
moel@391
    65
        /// </param>
moel@391
    66
        /// <returns>
moel@391
    67
        /// A SolidColorBrush.
moel@391
    68
        /// </returns>
moel@391
    69
        public static Brush ToBrush(this OxyColor c)
moel@391
    70
        {
moel@391
    71
            return new SolidBrush(c.ToColor());
moel@391
    72
        }
moel@391
    73
moel@391
    74
        /// <summary>
moel@391
    75
        /// Converts an OxyColor to a Color.
moel@391
    76
        /// </summary>
moel@391
    77
        /// <param name="c">
moel@391
    78
        /// The color.
moel@391
    79
        /// </param>
moel@391
    80
        /// <returns>
moel@391
    81
        /// A Color.
moel@391
    82
        /// </returns>
moel@391
    83
        public static Color ToColor(this OxyColor c)
moel@391
    84
        {
moel@391
    85
            return Color.FromArgb(c.A, c.R, c.G, c.B);
moel@391
    86
        }
moel@391
    87
moel@391
    88
        /// <summary>
moel@391
    89
        /// Converts a HorizontalAlignment to a HorizontalTextAlign.
moel@391
    90
        /// </summary>
moel@391
    91
        /// <param name="alignment">
moel@391
    92
        /// The alignment.
moel@391
    93
        /// </param>
moel@391
    94
        /// <returns>
moel@391
    95
        /// A HorizontalTextAlign.
moel@391
    96
        /// </returns>
moel@391
    97
        public static OxyPlot.HorizontalAlignment ToHorizontalTextAlign(this HorizontalAlignment alignment)
moel@391
    98
        {
moel@391
    99
            switch (alignment)
moel@391
   100
            {
moel@391
   101
                case HorizontalAlignment.Center:
moel@391
   102
                    return OxyPlot.HorizontalAlignment.Center;
moel@391
   103
                case HorizontalAlignment.Right:
moel@391
   104
                    return OxyPlot.HorizontalAlignment.Right;
moel@391
   105
                default:
moel@391
   106
                    return OxyPlot.HorizontalAlignment.Left;
moel@391
   107
            }
moel@391
   108
        }
moel@391
   109
moel@391
   110
        /// <summary>
moel@391
   111
        /// Converts a Color to an OxyColor.
moel@391
   112
        /// </summary>
moel@391
   113
        /// <param name="color">
moel@391
   114
        /// The color.
moel@391
   115
        /// </param>
moel@391
   116
        /// <returns>
moel@391
   117
        /// An OxyColor.
moel@391
   118
        /// </returns>
moel@391
   119
        public static OxyColor ToOxyColor(this Color color)
moel@391
   120
        {
moel@391
   121
            return OxyColor.FromArgb(color.A, color.R, color.G, color.B);
moel@391
   122
        }
moel@391
   123
moel@391
   124
        /// <summary>
moel@391
   125
        /// Converts a nullable Color to an OxyColor.
moel@391
   126
        /// </summary>
moel@391
   127
        /// <param name="color">
moel@391
   128
        /// The color.
moel@391
   129
        /// </param>
moel@391
   130
        /// <returns>
moel@391
   131
        /// An OxyColor.
moel@391
   132
        /// </returns>
moel@391
   133
        public static OxyColor ToOxyColor(this Color? color)
moel@391
   134
        {
moel@391
   135
            return color.HasValue ? color.Value.ToOxyColor() : null;
moel@391
   136
        }
moel@391
   137
moel@391
   138
        /// <summary>
moel@391
   139
        /// Converts a Brush to an OxyColor.
moel@391
   140
        /// </summary>
moel@391
   141
        /// <param name="brush">
moel@391
   142
        /// The brush.
moel@391
   143
        /// </param>
moel@391
   144
        /// <returns>
moel@391
   145
        /// An oxycolor.
moel@391
   146
        /// </returns>
moel@391
   147
        public static OxyColor ToOxyColor(this Brush brush)
moel@391
   148
        {
moel@391
   149
            var scb = brush as SolidBrush;
moel@391
   150
            return scb != null ? scb.Color.ToOxyColor() : null;
moel@391
   151
        }
moel@391
   152
moel@391
   153
        /// <summary>
moel@391
   154
        /// Converts a Thickness to an OxyThickness.
moel@391
   155
        /// </summary>
moel@391
   156
        /// <returns>
moel@391
   157
        /// An OxyPlot thickness.
moel@391
   158
        /// </returns>
moel@391
   159
        /// <summary>
moel@391
   160
        /// Converts a ScreenPoint to a Point.
moel@391
   161
        /// </summary>
moel@391
   162
        /// <param name="pt">
moel@391
   163
        /// The screen point.
moel@391
   164
        /// </param>
moel@391
   165
        /// <param name="aliased">
moel@391
   166
        /// use pixel alignment conversion if set to <c>true</c>.
moel@391
   167
        /// </param>
moel@391
   168
        /// <returns>
moel@391
   169
        /// A point.
moel@391
   170
        /// </returns>
moel@391
   171
        public static Point ToPoint(this ScreenPoint pt, bool aliased)
moel@391
   172
        {
moel@391
   173
            // adding 0.5 to get pixel boundary alignment, seems to work
moel@391
   174
            // http://weblogs.asp.net/mschwarz/archive/2008/01/04/silverlight-rectangles-paths-and-line-comparison.aspx
moel@391
   175
            // http://www.wynapse.com/Silverlight/Tutor/Silverlight_Rectangles_Paths_And_Lines_Comparison.aspx
moel@391
   176
            if (aliased)
moel@391
   177
            {
moel@391
   178
                return new Point((int)pt.X, (int)pt.Y);
moel@391
   179
            }
moel@391
   180
moel@391
   181
            return new Point((int)Math.Round(pt.X), (int)Math.Round(pt.Y));
moel@391
   182
        }
moel@391
   183
moel@391
   184
        /// <summary>
moel@391
   185
        /// Converts an OxyRect to a Rect.
moel@391
   186
        /// </summary>
moel@391
   187
        /// <param name="r">
moel@391
   188
        /// The rectangle.
moel@391
   189
        /// </param>
moel@391
   190
        /// <param name="aliased">
moel@391
   191
        /// use pixel alignment if set to <c>true</c>.
moel@391
   192
        /// </param>
moel@391
   193
        /// <returns>
moel@391
   194
        /// A rect.
moel@391
   195
        /// </returns>
moel@391
   196
        public static Rectangle ToRect(this OxyRect r, bool aliased)
moel@391
   197
        {
moel@391
   198
            if (aliased)
moel@391
   199
            {
moel@391
   200
                var x = (int)r.Left;
moel@391
   201
                var y = (int)r.Top;
moel@391
   202
                var ri = (int)r.Right;
moel@391
   203
                var bo = (int)r.Bottom;
moel@391
   204
                return new Rectangle(x, y, ri - x, bo - y);
moel@391
   205
            }
moel@391
   206
moel@391
   207
            return new Rectangle(
moel@391
   208
                (int)Math.Round(r.Left), (int)Math.Round(r.Top), (int)Math.Round(r.Width), (int)Math.Round(r.Height));
moel@391
   209
        }
moel@391
   210
moel@391
   211
        /// <summary>
moel@391
   212
        /// Converts a point to a ScreenPoint.
moel@391
   213
        /// </summary>
moel@391
   214
        /// <param name="pt">
moel@391
   215
        /// The point.
moel@391
   216
        /// </param>
moel@391
   217
        /// <returns>
moel@391
   218
        /// A screen point.
moel@391
   219
        /// </returns>
moel@391
   220
        public static ScreenPoint ToScreenPoint(this Point pt)
moel@391
   221
        {
moel@391
   222
            return new ScreenPoint(pt.X, pt.Y);
moel@391
   223
        }
moel@391
   224
moel@391
   225
        /// <summary>
moel@391
   226
        /// Converts a Point array to a ScreenPoint array.
moel@391
   227
        /// </summary>
moel@391
   228
        /// <param name="points">
moel@391
   229
        /// The points.
moel@391
   230
        /// </param>
moel@391
   231
        /// <returns>
moel@391
   232
        /// A ScreenPoint array.
moel@391
   233
        /// </returns>
moel@391
   234
        public static ScreenPoint[] ToScreenPointArray(this Point[] points)
moel@391
   235
        {
moel@391
   236
            if (points == null)
moel@391
   237
            {
moel@391
   238
                return null;
moel@391
   239
            }
moel@391
   240
moel@391
   241
            var pts = new ScreenPoint[points.Length];
moel@391
   242
            for (int i = 0; i < points.Length; i++)
moel@391
   243
            {
moel@391
   244
                pts[i] = points[i].ToScreenPoint();
moel@391
   245
            }
moel@391
   246
moel@391
   247
            return pts;
moel@391
   248
        }
moel@391
   249
moel@391
   250
    }
moel@391
   251
}