Skip to content

Commit bc1e923

Browse files
Fix color converter
1 parent 4052958 commit bc1e923

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

src/ImageSharp.Web/Commands/Converters/ArrayConverter{T}.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public T[] ConvertFrom(
4444
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4545
private static string[] GetStringArray(string input, CultureInfo culture)
4646
{
47-
char separator = culture.TextInfo.ListSeparator[0];
47+
char separator = ConverterUtility.GetListSeparator(culture);
48+
49+
// TODO: Can we use StringSplit Enumerator here?
50+
// https://github.com/dotnet/runtime/issues/934
4851
return input.Split(separator).Select(s => s.Trim()).ToArray();
4952
}
5053
}

src/ImageSharp.Web/Commands/Converters/ColorConverter.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public sealed class ColorConverter : ICommandConverter<Color>
1717
/// The web color hexadecimal regex. Matches strings arranged
1818
/// in rgb, rgba, rrggbb, or rrggbbaa format to match web syntax.
1919
/// </summary>
20-
private static readonly Regex HexColorRegex = new("([0-9a-fA-F]{3}){1,2}", RegexOptions.Compiled);
20+
private static readonly Regex HexColorRegex = new("([0-9a-fA-F][^,;.-]\\B{3}){1,2}", RegexOptions.Compiled);
2121

2222
/// <summary>
2323
/// The number color regex.
@@ -41,12 +41,6 @@ public Color ConvertFrom(CommandParser parser, CultureInfo culture, string? valu
4141
return default;
4242
}
4343

44-
// Hex colors rgb, rrggbb, rrggbbaa
45-
if (HexColorRegex.IsMatch(value))
46-
{
47-
return Color.ParseHex(value);
48-
}
49-
5044
// Named colors
5145
IDictionary<string, Color> table = ColorConstantsTable.Value;
5246
if (table.TryGetValue(value, out Color color))
@@ -55,7 +49,7 @@ public Color ConvertFrom(CommandParser parser, CultureInfo culture, string? valu
5549
}
5650

5751
// Numeric r,g,b - r,g,b,a
58-
char separator = culture.TextInfo.ListSeparator[0];
52+
char separator = ConverterUtility.GetListSeparator(culture);
5953
if (value.IndexOf(separator) != -1)
6054
{
6155
string[] components = value.Split(separator);
@@ -82,6 +76,12 @@ public Color ConvertFrom(CommandParser parser, CultureInfo culture, string? valu
8276
}
8377
}
8478

79+
// Hex colors rgb, rrggbb, rrggbbaa
80+
if (HexColorRegex.IsMatch(value))
81+
{
82+
return Color.ParseHex(value);
83+
}
84+
8585
return default;
8686
}
8787

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System.Globalization;
5+
using System.Runtime.CompilerServices;
6+
using System.Runtime.InteropServices;
7+
8+
namespace SixLabors.ImageSharp.Web.Commands.Converters;
9+
internal static class ConverterUtility
10+
{
11+
/// <summary>
12+
/// Gets the <see cref="char"/> that is used by the given culture to separate items in a list.
13+
/// </summary>
14+
/// <param name="culture">The culture.</param>
15+
/// <returns>The <see cref="char"/>.</returns>
16+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
17+
public static char GetListSeparator(CultureInfo culture)
18+
=> MemoryMarshal.GetReference<char>(culture.TextInfo.ListSeparator);
19+
}

src/ImageSharp.Web/Commands/Converters/EnumConverter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public sealed class EnumConverter : ICommandConverter<object>
3434

3535
try
3636
{
37-
char separator = culture.TextInfo.ListSeparator[0];
37+
char separator = ConverterUtility.GetListSeparator(culture);
3838
if (value.IndexOf(separator) != -1)
3939
{
4040
long convertedValue = 0;
@@ -63,5 +63,8 @@ public sealed class EnumConverter : ICommandConverter<object>
6363
/// <returns>The <see cref="T:String[]"/>.</returns>
6464
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6565
private static string[] GetStringArray(string input, char separator)
66+
67+
// TODO: Can we use StringSplit Enumerator here?
68+
// https://github.com/dotnet/runtime/issues/934
6669
=> input.Split(separator).Select(s => s.Trim()).ToArray();
6770
}

src/ImageSharp.Web/Commands/Converters/ListConverter{T}.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public List<T> ConvertFrom(
4444
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4545
private static string[] GetStringArray(string input, CultureInfo culture)
4646
{
47-
char separator = culture.TextInfo.ListSeparator[0];
47+
char separator = ConverterUtility.GetListSeparator(culture);
48+
49+
// TODO: Can we use StringSplit Enumerator here?
50+
// https://github.com/dotnet/runtime/issues/934
4851
return input.Split(separator).Select(s => s.Trim()).ToArray();
4952
}
5053
}

0 commit comments

Comments
 (0)