/ src / modules / cmdpal / Microsoft.CmdPal.UI / Helpers / OptionalColorToBrushConverter.cs
OptionalColorToBrushConverter.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using Microsoft.CommandPalette.Extensions;
 6  using Microsoft.UI;
 7  using Microsoft.UI.Xaml.Media;
 8  using Color = Windows.UI.Color;
 9  
10  namespace Microsoft.CmdPal.UI.Helpers;
11  
12  public static partial class OptionalColorBrushCacheProvider
13  {
14      private static readonly Dictionary<OptionalColor, SolidColorBrush> _brushCache = [];
15  
16      public static SolidColorBrush? Convert(OptionalColor color)
17      {
18          if (!color.HasValue)
19          {
20              return null;
21          }
22  
23          if (!_brushCache.TryGetValue(color, out var brush))
24          {
25              // Create and cache the brush if we see this color for the first time.
26              brush = new SolidColorBrush(Color.FromArgb(color.Color.A, color.Color.R, color.Color.G, color.Color.B));
27              _brushCache[color] = brush;
28          }
29  
30          return brush;
31      }
32  }