|
| 1 | +#region Copyright © 2015 Couchcoding |
| 2 | + |
| 3 | +// File: BrushCache.cs |
| 4 | +// Package: Logbert |
| 5 | +// Project: Logbert |
| 6 | +// |
| 7 | +// The MIT License (MIT) |
| 8 | +// |
| 9 | +// Copyright (c) 2015 Couchcoding |
| 10 | +// |
| 11 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | +// of this software and associated documentation files (the "Software"), to deal |
| 13 | +// in the Software without restriction, including without limitation the rights |
| 14 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | +// copies of the Software, and to permit persons to whom the Software is |
| 16 | +// furnished to do so, subject to the following conditions: |
| 17 | +// |
| 18 | +// The above copyright notice and this permission notice shall be included in |
| 19 | +// all copies or substantial portions of the Software. |
| 20 | +// |
| 21 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | +// THE SOFTWARE. |
| 28 | + |
| 29 | +#endregion |
| 30 | + |
| 31 | +using System.Collections.Generic; |
| 32 | +using System.Drawing; |
| 33 | + |
| 34 | +namespace Com.Couchcoding.Logbert.Helper |
| 35 | +{ |
| 36 | + /// <summary> |
| 37 | + /// Implements a cache for <see cref="Brushes"/> nad <see cref="Pen"/>s. |
| 38 | + /// </summary> |
| 39 | + public static class GdiCache |
| 40 | + { |
| 41 | + #region Private Fields |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// The one and only <see cref="Brush"/> cache. |
| 45 | + /// </summary> |
| 46 | + private static readonly Dictionary<Color, Brush> mBrushCache = new Dictionary<Color,Brush>(); |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// The one and only <see cref="Pen"/> cache. |
| 50 | + /// </summary> |
| 51 | + private static readonly Dictionary<Color, Pen> mPenCache = new Dictionary<Color, Pen>(); |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Simple object for thread synchronization to access the <see cref="Brush"/>es.. |
| 55 | + /// </summary> |
| 56 | + private static readonly object mBrushSync = new object(); |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Simple object for thread synchronization to access the <see cref="Pen"/>s.. |
| 60 | + /// </summary> |
| 61 | + private static readonly object mPenSync = new object(); |
| 62 | + |
| 63 | + #endregion |
| 64 | + |
| 65 | + #region Public Methods |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Gets a cached <see cref="Brush"/> that matches the given <paramref name="color"/>. |
| 69 | + /// </summary> |
| 70 | + /// <param name="color">The <see cref="Color"/> to get a <see cref="Brush"/> for</param> |
| 71 | + /// <returns>The cached <see cref="Brush"/> that matches the given <paramref name="color"/>.</returns> |
| 72 | + public static Brush GetBrushFromColor(Color color) |
| 73 | + { |
| 74 | + lock (mBrushSync) |
| 75 | + { |
| 76 | + if (!mBrushCache.ContainsKey(color)) |
| 77 | + { |
| 78 | + mBrushCache.Add(color, new SolidBrush(color)); |
| 79 | + } |
| 80 | + |
| 81 | + return mBrushCache[color]; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Gets a cached <see cref="Pen"/> that matches the given <paramref name="color"/>. |
| 87 | + /// </summary> |
| 88 | + /// <param name="color">The <see cref="Color"/> to get a <see cref="Pen"/> for</param> |
| 89 | + /// <returns>The cached <see cref="Pen"/> that matches the given <paramref name="color"/>.</returns> |
| 90 | + public static Pen GetPenFromColor(Color color) |
| 91 | + { |
| 92 | + lock (mPenSync) |
| 93 | + { |
| 94 | + if (!mPenCache.ContainsKey(color)) |
| 95 | + { |
| 96 | + mPenCache.Add(color, new Pen(color)); |
| 97 | + } |
| 98 | + |
| 99 | + return mPenCache[color]; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + #endregion |
| 104 | + } |
| 105 | +} |
0 commit comments