Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Utils/Utils.cs @ 17712

Last change on this file since 17712 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 7.9 KB
Line 
1using System;
2using System.Diagnostics;
3using System.Drawing;
4using System.Drawing.Drawing2D;
5using System.Runtime.Serialization;
6namespace Netron.Diagramming.Core {
7  /// <summary>
8  /// Static class of utility methods
9  /// </summary>
10  public static class Utils {
11    public static void DrawRoundRect(Graphics g, Pen p, Rectangle rectangle) {
12      DrawRoundRect(g, p, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, 7F);
13    }
14    public static void DrawRoundRect(Graphics g, Pen p, float X, float Y, float width, float height, float radius) {
15
16      GraphicsPath path = new GraphicsPath();
17      path.AddLine(X + radius, Y, X + width - (radius * 2), Y);
18      path.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
19      path.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2));
20      path.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
21      path.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height);
22      path.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
23      path.AddLine(X, Y + height - (radius * 2), X, Y + radius);
24      path.AddArc(X, Y, radius * 2, radius * 2, 180, 90);
25      path.CloseFigure();
26
27      g.DrawPath(p, path);
28      path.Dispose();
29    }
30
31    /// <summary>
32    /// Returns the bounding rectangle of the given collection of entities.
33    /// </summary>
34    /// <param name="collection"></param>
35    /// <returns></returns>
36    public static Rectangle BoundingRectangle(CollectionBase<IDiagramEntity> collection) {
37      //get the bounding rectangle
38      bool first = true;
39      Rectangle rec = Rectangle.Empty;
40      foreach (IDiagramEntity entity in collection) {
41        if (first) {
42          rec = entity.Rectangle;
43          first = false;
44        } else
45          rec = Rectangle.Union(rec, entity.Rectangle);
46      }
47      return rec;
48    }
49
50    /// <summary>
51    /// Depth-first traversal of an <see cref="IGroup"/>
52    /// </summary>
53    /// <param name="group"></param>
54    /// <param name="collection"></param>
55    public static void TraverseCollect(IGroup group, ref CollectionBase<IDiagramEntity> collection) {
56      #region Checks
57      if (group == null)
58        throw new InconsistencyException("Cannot collect entities of a 'null' IGroup");
59      if (collection == null)
60        throw new InconsistencyException("You need to instantiate a collection before using this method.");
61      #endregion
62
63      foreach (IDiagramEntity entity in group.Entities) {
64        if (entity is IGroup)
65          TraverseCollect(entity as IGroup, ref collection);
66        else
67          collection.Add(entity);
68      }
69
70    }
71
72    // Given H,S,L in range of 0-1
73
74    // Returns a Color (RGB struct) in range of 0-255
75
76    /// <summary>
77    /// HSL to RGB conversion.
78    /// </summary>
79    /// <param name="h">The h.</param>
80    /// <param name="sl">The sl.</param>
81    /// <param name="l">The l.</param>
82    /// <returns></returns>
83    public static ColorRGB HSL2RGB(double h, double sl, double l) {
84
85      double v;
86      double r, g, b;
87      r = l;   // default to gray
88      g = l;
89      b = l;
90      v = (l <= 0.5) ? (l * (1.0 + sl)) : (l + sl - l * sl);
91      if (v > 0) {
92        double m;
93        double sv;
94        int sextant;
95        double fract, vsf, mid1, mid2;
96
97        m = l + l - v;
98        sv = (v - m) / v;
99        h *= 6.0;
100        sextant = (int)h;
101        fract = h - sextant;
102        vsf = v * sv * fract;
103        mid1 = m + vsf;
104        mid2 = v - vsf;
105        switch (sextant) {
106          case 0:
107            r = v;
108            g = mid1;
109            b = m;
110            break;
111          case 1:
112            r = mid2;
113            g = v;
114            b = m;
115            break;
116          case 2:
117            r = m;
118            g = v;
119            b = mid1;
120            break;
121          case 3:
122            r = m;
123            g = mid2;
124            b = v;
125            break;
126          case 4:
127            r = mid1;
128            g = m;
129            b = v;
130            break;
131          case 5:
132            r = v;
133            g = m;
134            b = mid2;
135            break;
136        }
137      }
138      ColorRGB rgb = new ColorRGB();
139      rgb.R = Convert.ToByte(r * 255.0f);
140      rgb.G = Convert.ToByte(g * 255.0f);
141      rgb.B = Convert.ToByte(b * 255.0f);
142      return rgb;
143
144    }
145
146    // Given a Color (RGB Struct) in range of 0-255
147
148    // Return H,S,L in range of 0-1
149
150    /// <summary>
151    /// RGB to HSL conversion
152    /// </summary>
153    /// <param name="rgb">The RGB.</param>
154    /// <param name="h">The h.</param>
155    /// <param name="s">The s.</param>
156    /// <param name="l">The l.</param>
157    public static void RGB2HSL(ColorRGB rgb, out double h, out double s, out double l) {
158      double r = rgb.R / 255.0;
159      double g = rgb.G / 255.0;
160      double b = rgb.B / 255.0;
161      double v;
162      double m;
163      double vm;
164      double r2, g2, b2;
165
166      h = 0; // default to black
167      s = 0;
168      l = 0;
169      v = Math.Max(r, g);
170      v = Math.Max(v, b);
171      m = Math.Min(r, g);
172      m = Math.Min(m, b);
173
174      l = (m + v) / 2.0;
175      if (l <= 0.0) {
176        return;
177      }
178      vm = v - m;
179      s = vm;
180      if (s > 0.0) {
181        s /= (l <= 0.5) ? (v + m) : (2.0 - v - m);
182      } else {
183        return;
184      }
185      r2 = (v - r) / vm;
186      g2 = (v - g) / vm;
187      b2 = (v - b) / vm;
188      if (r == v) {
189        h = (g == m ? 5.0 + b2 : 1.0 - g2);
190      } else if (g == v) {
191        h = (b == m ? 1.0 + r2 : 3.0 - b2);
192      } else {
193        h = (r == m ? 3.0 + g2 : 5.0 - r2);
194      }
195      h /= 6.0;
196    }
197    /// <summary>
198    /// Constrains the type.
199    /// </summary>
200    /// <param name="type">The type.</param>
201    public static void ConstrainType(Type type) {
202      bool serializable = type.IsSerializable;
203      if (serializable == false) {
204        string message = "The type " + type + " is not serializable";
205        throw new SerializationException(message);
206      }
207      bool genericType = type.IsGenericType;
208
209      if (genericType) {
210        Type[] typeArguments = type.GetGenericArguments();
211        Debug.Assert(typeArguments.Length >= 1);
212        Array.ForEach(typeArguments, ConstrainType);
213      }
214    }
215
216
217  }
218  /// <summary>
219  /// Utility struct for color conversions
220  /// </summary>
221  public struct ColorRGB {
222    #region Fields
223    private byte r;
224    private byte g;
225    private byte b;
226    #endregion
227
228    /// <summary>
229    /// Gets or sets the Red value.
230    /// </summary>
231    /// <value>The R.</value>
232    public byte R {
233      get { return r; }
234      set { r = value; }
235    }
236
237
238    /// <summary>
239    /// Gets or sets the Green value.
240    /// </summary>
241    /// <value>The G.</value>
242    public byte G {
243      get { return g; }
244      set { g = value; }
245    }
246
247
248
249    /// <summary>
250    /// Gets or sets the Blue value.
251    /// </summary>
252    /// <value>The B.</value>
253    public byte B {
254      get { return b; }
255      set { b = value; }
256    }
257    /// <summary>
258    /// Initializes a new instance of the <see cref="T:ColorRGB"/> class.
259    /// </summary>
260    /// <param name="value">The value.</param>
261    public ColorRGB(Color value) {
262      this.r = value.R;
263      this.g = value.G;
264      this.b = value.B;
265    }
266    /// <summary>
267    /// Implicit conversion of the specified RGB.
268    /// </summary>
269    /// <param name="rgb">The RGB.</param>
270    /// <returns></returns>
271    public static implicit operator Color(ColorRGB rgb) {
272      Color c = Color.FromArgb(rgb.R, rgb.G, rgb.B);
273      return c;
274    }
275    /// <summary>
276    /// Explicit conversion of the specified c.
277    /// </summary>
278    /// <param name="c">The c.</param>
279    /// <returns></returns>
280    public static explicit operator ColorRGB(Color c) {
281      return new ColorRGB(c);
282    }
283  }
284
285
286
287
288}
Note: See TracBrowser for help on using the repository browser.