コード例 #1
0
        /// <summary>
        /// ParseBrush
        /// <param name="brush"> string with brush description </param>
        /// <param name="formatProvider">IFormatProvider for processing string</param>
        /// <param name="context">ITypeDescriptorContext</param>
        /// </summary>
        internal static Brush ParseBrush(string brush, IFormatProvider formatProvider, ITypeDescriptorContext context)
        {
            bool   isPossibleKnownColor;
            bool   isNumericColor;
            bool   isScRgbColor;
            bool   isContextColor;
            string trimmedColor = KnownColors.MatchColor(brush, out isPossibleKnownColor, out isNumericColor, out isContextColor, out isScRgbColor);

            if (trimmedColor.Length == 0)
            {
                throw new FormatException(SR.Get(SRID.Parser_Empty));
            }

            // Note that because trimmedColor is exactly brush.Trim() we don't have to worry about
            // extra tokens as we do with TokenizerHelper.  If we return one of the solid color
            // brushes then the ParseColor routine (or ColorStringToKnownColor) matched the entire
            // input.
            if (isNumericColor)
            {
                return(new SolidColorBrush(ParseHexColor(trimmedColor)));
            }

            if (isContextColor)
            {
                return(new SolidColorBrush(ParseContextColor(trimmedColor, formatProvider, context)));
            }

            if (isScRgbColor)
            {
                return(new SolidColorBrush(ParseScRgbColor(trimmedColor, formatProvider)));
            }

            if (isPossibleKnownColor)
            {
                SolidColorBrush scp = KnownColors.ColorStringToKnownBrush(trimmedColor);

                if (scp != null)
                {
                    return(scp);
                }
            }

            // If it's not a color, so the content is illegal.
            throw new FormatException(SR.Get(SRID.Parsers_IllegalToken));
        }
コード例 #2
0
        /// <summary>
        /// ParseColor
        /// <param name="color"> string with color description </param>
        /// <param name="formatProvider">IFormatProvider for processing string</param>
        /// <param name="context">ITypeDescriptorContext</param>
        /// </summary>
        internal static Color ParseColor(string color, IFormatProvider formatProvider, ITypeDescriptorContext context)
        {
            bool   isPossibleKnowColor;
            bool   isNumericColor;
            bool   isScRgbColor;
            bool   isContextColor;
            string trimmedColor = KnownColors.MatchColor(color, out isPossibleKnowColor, out isNumericColor, out isContextColor, out isScRgbColor);

            if ((isPossibleKnowColor == false) &&
                (isNumericColor == false) &&
                (isScRgbColor == false) &&
                (isContextColor == false))
            {
                throw new FormatException(SR.Get(SRID.Parsers_IllegalToken));
            }

            //Is it a number?
            if (isNumericColor)
            {
                return(ParseHexColor(trimmedColor));
            }
            else if (isContextColor)
            {
                return(ParseContextColor(trimmedColor, formatProvider, context));
            }
            else if (isScRgbColor)
            {
                return(ParseScRgbColor(trimmedColor, formatProvider));
            }
            else
            {
                KnownColor kc = KnownColors.ColorStringToKnownColor(trimmedColor);

                if (kc == KnownColor.UnknownColor)
                {
                    throw new FormatException(SR.Get(SRID.Parsers_IllegalToken));
                }

                return(Color.FromUInt32((uint)kc));
            }
        }