static private Color ParseContextColor(string trimmedColor, IFormatProvider formatProvider, ITypeDescriptorContext context) { if (!trimmedColor.StartsWith(s_ContextColor, StringComparison.OrdinalIgnoreCase)) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } string tokens = trimmedColor.Substring(s_ContextColor.Length); tokens = tokens.Trim(); string[] preSplit = tokens.Split(new Char[] { ' ' }); if (preSplit.GetLength(0) < 2) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } tokens = tokens.Substring(preSplit[0].Length); TokenizerHelper th = new TokenizerHelper(tokens, formatProvider); string[] split = tokens.Split(new Char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); int numTokens = split.GetLength(0); float alpha = Convert.ToSingle(th.NextTokenRequired(), formatProvider); float[] values = new float[numTokens - 1]; for (int i = 0; i < numTokens - 1; i++) { values[i] = Convert.ToSingle(th.NextTokenRequired(), formatProvider); } string profileString = preSplit[0]; UriHolder uriHolder = TypeConverterHelper.GetUriFromUriContext(context, profileString); Uri profileUri; if (uriHolder.BaseUri != null) { profileUri = new Uri(uriHolder.BaseUri, uriHolder.OriginalUri); } else { profileUri = uriHolder.OriginalUri; } Color result = Color.FromAValues(alpha, values, profileUri); // If the number of color values found does not match the number of channels in the profile, we must throw if (result.ColorContext.NumChannels != values.Length) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } return(result); }
static private Color ParseScRgbColor(string trimmedColor, IFormatProvider formatProvider) { if (!trimmedColor.StartsWith("sc#", StringComparison.Ordinal)) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } string tokens = trimmedColor.Substring(3, trimmedColor.Length - 3); // The tokenizer helper will tokenize a list based on the IFormatProvider. TokenizerHelper th = new TokenizerHelper(tokens, formatProvider); float[] values = new float[4]; for (int i = 0; i < 3; i++) { values[i] = Convert.ToSingle(th.NextTokenRequired(), formatProvider); } if (th.NextToken()) { values[3] = Convert.ToSingle(th.GetCurrentToken(), formatProvider); // We should be out of tokens at this point if (th.NextToken()) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } return(Color.FromScRgb(values[0], values[1], values[2], values[3])); } else { return(Color.FromScRgb(1.0f, values[0], values[1], values[2])); } }
/// <summary> /// Parse - returns an instance converted from the provided string using /// the culture "en-US" /// <param name="source"> string with Point4D data </param> /// </summary> public static Point4D Parse(string source) { IFormatProvider formatProvider = System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS; TokenizerHelper th = new TokenizerHelper(source, formatProvider); Point4D value; String firstToken = th.NextTokenRequired(); value = new Point4D( Convert.ToDouble(firstToken, formatProvider), Convert.ToDouble(th.NextTokenRequired(), formatProvider), Convert.ToDouble(th.NextTokenRequired(), formatProvider), Convert.ToDouble(th.NextTokenRequired(), formatProvider)); // There should be no more tokens in this string. th.LastTokenRequired(); return value; }
/// <summary> /// ConvertFrom /// </summary> public override object ConvertFrom( ITypeDescriptorContext context, CultureInfo cultureInfo, object value) { string stringValue = value as string; if (value == null) { throw new NotSupportedException(SR.Get(SRID.Converter_ConvertFromNotSupported)); } TokenizerHelper th = new TokenizerHelper(stringValue, cultureInfo); return new KeySpline( Convert.ToDouble(th.NextTokenRequired(), cultureInfo), Convert.ToDouble(th.NextTokenRequired(), cultureInfo), Convert.ToDouble(th.NextTokenRequired(), cultureInfo), Convert.ToDouble(th.NextTokenRequired(), cultureInfo)); }
/// <summary> /// Parse - returns an instance converted from the provided string using /// the culture "en-US" /// <param name="source"> string with Int32Rect data </param> /// </summary> public static Int32Rect Parse(string source) { IFormatProvider formatProvider = System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS; TokenizerHelper th = new TokenizerHelper(source, formatProvider); Int32Rect value; String firstToken = th.NextTokenRequired(); // The token will already have had whitespace trimmed so we can do a // simple string compare. if (firstToken == "Empty") { value = Empty; } else { value = new Int32Rect( Convert.ToInt32(firstToken, formatProvider), Convert.ToInt32(th.NextTokenRequired(), formatProvider), Convert.ToInt32(th.NextTokenRequired(), formatProvider), Convert.ToInt32(th.NextTokenRequired(), formatProvider)); } // There should be no more tokens in this string. th.LastTokenRequired(); return value; }
static private Color ParseScRgbColor(string trimmedColor, IFormatProvider formatProvider) { if (!trimmedColor.StartsWith("sc#", StringComparison.Ordinal)) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } string tokens = trimmedColor.Substring(3, trimmedColor.Length - 3); // The tokenizer helper will tokenize a list based on the IFormatProvider. TokenizerHelper th = new TokenizerHelper(tokens, formatProvider); float[] values = new float[4]; for (int i = 0; i < 3; i++) { values[i] = Convert.ToSingle(th.NextTokenRequired(), formatProvider); } if (th.NextToken()) { values[3] = Convert.ToSingle(th.GetCurrentToken(), formatProvider); // We should be out of tokens at this point if (th.NextToken()) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } return Color.FromScRgb(values[0], values[1], values[2], values[3]); } else { return Color.FromScRgb(1.0f, values[0], values[1], values[2]); } }
static private Color ParseContextColor(string trimmedColor, IFormatProvider formatProvider, ITypeDescriptorContext context) { if (!trimmedColor.StartsWith(s_ContextColor, StringComparison.OrdinalIgnoreCase)) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } string tokens = trimmedColor.Substring(s_ContextColor.Length); tokens = tokens.Trim(); string[] preSplit = tokens.Split(new Char[] { ' ' }); if (preSplit.GetLength(0)< 2) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } tokens = tokens.Substring(preSplit[0].Length); TokenizerHelper th = new TokenizerHelper(tokens, formatProvider); string[] split = tokens.Split(new Char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); int numTokens = split.GetLength(0); float alpha = Convert.ToSingle(th.NextTokenRequired(), formatProvider); float[] values = new float[numTokens - 1]; for (int i = 0; i < numTokens - 1; i++) { values[i] = Convert.ToSingle(th.NextTokenRequired(), formatProvider); } string profileString = preSplit[0]; UriHolder uriHolder = TypeConverterHelper.GetUriFromUriContext(context,profileString); Uri profileUri; if (uriHolder.BaseUri != null) { profileUri = new Uri(uriHolder.BaseUri, uriHolder.OriginalUri); } else { profileUri = uriHolder.OriginalUri; } Color result = Color.FromAValues(alpha, values, profileUri); // If the number of color values found does not match the number of channels in the profile, we must throw if (result.ColorContext.NumChannels != values.Length) { throw new FormatException(SR.Get(SRID.Parsers_IllegalToken)); } return result; }