Question

I´m trying to parse a LinearGradientBrush from Xaml strings, without any problem I use the ColorConverter.ConvertFromString for normal text or html color conversion.

Recently I came across some colors parsed from Adobe Illustrator into Expression Blend using the ScRGB formatting "sc#scA, scR, scG, scB". That seems to break the ColorConverter, as the ScA value from the Color struct seems to parse the value wrong; for example 0.2 becomes 2 and 0.5 becomes 5.

My code is fairly straightforward:

string colorStr = "sc#0.117647059, 0, 0, 0";
Color color = (Color)ColorConverter.ConvertFromString(colorStr);
Console.WriteLine(color.ScA);

Output: 1,176471E+08, Expected: 0,1176471

Is this a bug in the .NET Framework (I'm on 4)? Is there a workaround? Does anyone have a clean-fix?

Was it helpful?

Solution

You have a culture problem. Clearly you are living in a part of the world where the decimal point in a floating point value is a comma instead of a period. But your color string is formatted with a period. So when the value is converted to float, it sees the period in the string as a thousands separator, not a decimal point. And "0117647059" is indeed 1,176471e8.

You'll need to use the ColorConverter.ConvertFrom() overload, it accepts a CultureInfo reference. Pass CultureInfo.InvariantCulture. Or whack the Adobe tooling over the head somehow so it generates properly localized strings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top