3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
using System ;
6
+ using System . Globalization ;
6
7
using System . Reflection ;
8
+ using Microsoft . Toolkit . Diagnostics ;
7
9
using Windows . UI ;
8
10
using Color = Windows . UI . Color ;
9
11
@@ -22,10 +24,7 @@ public static class ColorHelper
22
24
/// <returns>The created <see cref="Color"/>.</returns>
23
25
public static Color ToColor ( this string colorString )
24
26
{
25
- if ( string . IsNullOrEmpty ( colorString ) )
26
- {
27
- throw new ArgumentException ( nameof ( colorString ) ) ;
28
- }
27
+ Guard . IsNotNullOrEmpty ( colorString , nameof ( colorString ) ) ;
29
28
30
29
if ( colorString [ 0 ] == '#' )
31
30
{
@@ -80,8 +79,7 @@ public static Color ToColor(this string colorString)
80
79
return Color . FromArgb ( 255 , r , g , b ) ;
81
80
}
82
81
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." ) ;
85
83
}
86
84
}
87
85
@@ -91,24 +89,24 @@ public static Color ToColor(this string colorString)
91
89
92
90
if ( values . Length == 4 )
93
91
{
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 ) ;
98
96
99
97
return Color . FromArgb ( ( byte ) ( scA * 255 ) , ( byte ) ( scR * 255 ) , ( byte ) ( scG * 255 ) , ( byte ) ( scB * 255 ) ) ;
100
98
}
101
99
102
100
if ( values . Length == 3 )
103
101
{
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 ) ;
107
105
108
106
return Color . FromArgb ( 255 , ( byte ) ( scR * 255 ) , ( byte ) ( scG * 255 ) , ( byte ) ( scB * 255 ) ) ;
109
107
}
110
108
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)." ) ;
112
110
}
113
111
114
112
var prop = typeof ( Colors ) . GetTypeInfo ( ) . GetDeclaredProperty ( colorString ) ;
@@ -118,7 +116,7 @@ public static Color ToColor(this string colorString)
118
116
return ( Color ) prop . GetValue ( null ) ;
119
117
}
120
118
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." ) ;
122
120
}
123
121
124
122
/// <summary>
0 commit comments