Skip to content

Commit 63ad9e7

Browse files
committed
Fixed FormatException when ColorHelper in non-US
1 parent 7807070 commit 63ad9e7

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

Microsoft.Toolkit.Uwp/Helpers/ColorHelper.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Globalization;
67
using System.Reflection;
8+
using Microsoft.Toolkit.Diagnostics;
79
using Windows.UI;
810
using Color = Windows.UI.Color;
911

@@ -22,10 +24,7 @@ public static class ColorHelper
2224
/// <returns>The created <see cref="Color"/>.</returns>
2325
public static Color ToColor(this string colorString)
2426
{
25-
if (string.IsNullOrEmpty(colorString))
26-
{
27-
throw new ArgumentException(nameof(colorString));
28-
}
27+
Guard.IsNotNullOrEmpty(colorString, nameof(colorString));
2928

3029
if (colorString[0] == '#')
3130
{
@@ -80,8 +79,7 @@ public static Color ToColor(this string colorString)
8079
return Color.FromArgb(255, r, g, b);
8180
}
8281

83-
default:
84-
throw new FormatException(string.Format("The {0} string passed in the colorString argument is not a recognized Color format.", colorString));
82+
default: return ThrowHelper.ThrowFormatException<Color>("The string passed in the colorString argument is not a recognized Color format.");
8583
}
8684
}
8785

@@ -91,24 +89,24 @@ public static Color ToColor(this string colorString)
9189

9290
if (values.Length == 4)
9391
{
94-
var scA = double.Parse(values[0].Substring(3));
95-
var scR = double.Parse(values[1]);
96-
var scG = double.Parse(values[2]);
97-
var scB = double.Parse(values[3]);
92+
var scA = double.Parse(values[0].Substring(3), CultureInfo.InvariantCulture);
93+
var scR = double.Parse(values[1], CultureInfo.InvariantCulture);
94+
var scG = double.Parse(values[2], CultureInfo.InvariantCulture);
95+
var scB = double.Parse(values[3], CultureInfo.InvariantCulture);
9896

9997
return Color.FromArgb((byte)(scA * 255), (byte)(scR * 255), (byte)(scG * 255), (byte)(scB * 255));
10098
}
10199

102100
if (values.Length == 3)
103101
{
104-
var scR = double.Parse(values[0].Substring(3));
105-
var scG = double.Parse(values[1]);
106-
var scB = double.Parse(values[2]);
102+
var scR = double.Parse(values[0].Substring(3), CultureInfo.InvariantCulture);
103+
var scG = double.Parse(values[1], CultureInfo.InvariantCulture);
104+
var scB = double.Parse(values[2], CultureInfo.InvariantCulture);
107105

108106
return Color.FromArgb(255, (byte)(scR * 255), (byte)(scG * 255), (byte)(scB * 255));
109107
}
110108

111-
throw new FormatException(string.Format("The {0} string passed in the colorString argument is not a recognized Color format (sc#[scA,]scR,scG,scB).", colorString));
109+
return ThrowHelper.ThrowFormatException<Color>("The string passed in the colorString argument is not a recognized Color format (sc#[scA,]scR,scG,scB).");
112110
}
113111

114112
var prop = typeof(Colors).GetTypeInfo().GetDeclaredProperty(colorString);
@@ -118,7 +116,7 @@ public static Color ToColor(this string colorString)
118116
return (Color)prop.GetValue(null);
119117
}
120118

121-
throw new FormatException(string.Format("The {0} string passed in the colorString argument is not a recognized Color.", colorString));
119+
return ThrowHelper.ThrowFormatException<Color>("The string passed in the colorString argument is not a recognized Color.");
122120
}
123121

124122
/// <summary>

0 commit comments

Comments
 (0)