/ src / settings-ui / Settings.UI.Library / Helpers / SettingsUtilities.cs
SettingsUtilities.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 System.Drawing;
 6  using System.Globalization;
 7  
 8  namespace Microsoft.PowerToys.Settings.UI.Library.Helpers
 9  {
10      public static class SettingsUtilities
11      {
12          public static string ToRGBHex(string color)
13          {
14              if (color == null)
15              {
16                  return "#FFFFFF";
17              }
18  
19              // Using InvariantCulture as these are expected to be hex codes.
20              bool success = int.TryParse(
21                  color.Replace("#", string.Empty),
22                  System.Globalization.NumberStyles.HexNumber,
23                  CultureInfo.InvariantCulture,
24                  out int argb);
25  
26              if (success)
27              {
28                  Color clr = Color.FromArgb(argb);
29                  return "#" + clr.R.ToString("X2", CultureInfo.InvariantCulture) +
30                      clr.G.ToString("X2", CultureInfo.InvariantCulture) +
31                      clr.B.ToString("X2", CultureInfo.InvariantCulture);
32              }
33              else
34              {
35                  return "#FFFFFF";
36              }
37          }
38  
39          public static string ToARGBHex(string color)
40          {
41              if (color == null)
42              {
43                  return "#FFFFFFFF";
44              }
45  
46              // Using InvariantCulture as these are expected to be hex codes.
47              bool success = int.TryParse(
48                  color.Replace("#", string.Empty),
49                  System.Globalization.NumberStyles.HexNumber,
50                  CultureInfo.InvariantCulture,
51                  out int argb);
52  
53              if (success)
54              {
55                  Color clr = Color.FromArgb(argb);
56                  return "#" + clr.A.ToString("X2", CultureInfo.InvariantCulture) +
57                      clr.R.ToString("X2", CultureInfo.InvariantCulture) +
58                      clr.G.ToString("X2", CultureInfo.InvariantCulture) +
59                      clr.B.ToString("X2", CultureInfo.InvariantCulture);
60              }
61              else
62              {
63                  return "#FFFFFFFF";
64              }
65          }
66      }
67  }