|
|
|
@ -381,9 +381,9 @@ namespace Avalonia.Media |
|
|
|
|
|
|
|
if (components.Length == 3) // RGB
|
|
|
|
{ |
|
|
|
if (InternalTryParseByte(components[0], out byte red) && |
|
|
|
InternalTryParseByte(components[1], out byte green) && |
|
|
|
InternalTryParseByte(components[2], out byte blue)) |
|
|
|
if (InternalTryParseByte(components[0].AsSpan(), out byte red) && |
|
|
|
InternalTryParseByte(components[1].AsSpan(), out byte green) && |
|
|
|
InternalTryParseByte(components[2].AsSpan(), out byte blue)) |
|
|
|
{ |
|
|
|
color = new Color(0xFF, red, green, blue); |
|
|
|
return true; |
|
|
|
@ -391,10 +391,10 @@ namespace Avalonia.Media |
|
|
|
} |
|
|
|
else if (components.Length == 4) // RGBA
|
|
|
|
{ |
|
|
|
if (InternalTryParseByte(components[0], out byte red) && |
|
|
|
InternalTryParseByte(components[1], out byte green) && |
|
|
|
InternalTryParseByte(components[2], out byte blue) && |
|
|
|
InternalTryParseDouble(components[3], out double alpha)) |
|
|
|
if (InternalTryParseByte(components[0].AsSpan(), out byte red) && |
|
|
|
InternalTryParseByte(components[1].AsSpan(), out byte green) && |
|
|
|
InternalTryParseByte(components[2].AsSpan(), out byte blue) && |
|
|
|
InternalTryParseDouble(components[3].AsSpan(), out double alpha)) |
|
|
|
{ |
|
|
|
color = new Color((byte)Math.Round(alpha * 255.0), red, green, blue); |
|
|
|
return true; |
|
|
|
@ -402,17 +402,14 @@ namespace Avalonia.Media |
|
|
|
} |
|
|
|
|
|
|
|
// Local function to specially parse a byte value with an optional percentage sign
|
|
|
|
bool InternalTryParseByte(string inString, out byte outByte) |
|
|
|
bool InternalTryParseByte(ReadOnlySpan<char> inString, out byte outByte) |
|
|
|
{ |
|
|
|
// The percent sign, if it exists, must be at the end of the number
|
|
|
|
int percentIndex = inString.IndexOf("%", StringComparison.Ordinal); |
|
|
|
int percentIndex = inString.IndexOf("%".AsSpan(), StringComparison.Ordinal); |
|
|
|
|
|
|
|
if (percentIndex >= 0) |
|
|
|
{ |
|
|
|
var result = double.TryParse( |
|
|
|
inString.Substring(0, percentIndex), |
|
|
|
NumberStyles.Number, |
|
|
|
CultureInfo.InvariantCulture, |
|
|
|
var result = inString.Slice(0, percentIndex).TryParseNumberToDouble( |
|
|
|
out double percentage); |
|
|
|
|
|
|
|
outByte = (byte)Math.Round((percentage / 100.0) * 255.0); |
|
|
|
@ -420,37 +417,28 @@ namespace Avalonia.Media |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return byte.TryParse( |
|
|
|
inString, |
|
|
|
NumberStyles.Number, |
|
|
|
CultureInfo.InvariantCulture, |
|
|
|
return inString.TryParseNumberToByte( |
|
|
|
out outByte); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Local function to specially parse a double value with an optional percentage sign
|
|
|
|
bool InternalTryParseDouble(string inString, out double outDouble) |
|
|
|
bool InternalTryParseDouble(ReadOnlySpan<char> inString, out double outDouble) |
|
|
|
{ |
|
|
|
// The percent sign, if it exists, must be at the end of the number
|
|
|
|
int percentIndex = inString.IndexOf("%", StringComparison.Ordinal); |
|
|
|
int percentIndex = inString.IndexOf("%".AsSpan(), StringComparison.Ordinal); |
|
|
|
|
|
|
|
if (percentIndex >= 0) |
|
|
|
{ |
|
|
|
var result = double.TryParse( |
|
|
|
inString.Substring(0, percentIndex), |
|
|
|
NumberStyles.Number, |
|
|
|
CultureInfo.InvariantCulture, |
|
|
|
out double percentage); |
|
|
|
var result = inString.Slice(0, percentIndex).TryParseNumberToDouble( |
|
|
|
out double percentage); |
|
|
|
|
|
|
|
outDouble = percentage / 100.0; |
|
|
|
return result; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return double.TryParse( |
|
|
|
inString, |
|
|
|
NumberStyles.Number, |
|
|
|
CultureInfo.InvariantCulture, |
|
|
|
return inString.TryParseNumberToDouble( |
|
|
|
out outDouble); |
|
|
|
} |
|
|
|
} |
|
|
|
|