Ejemplo n.º 1
0
        private static Border ToBorder(StyleObject[] values)
        {
            // At least a border style MUST be specified.
            // Arguments are allowed to occur in any order.

            BorderStyle?borderStyle = values.Select(value => value.GetString())
                                      .Select(value => PropertyUtilities.TryParseBorderStyle(value, out BorderStyle result) ? (BorderStyle?)result : null)
                                      .Where(result => result != null)
                                      .FirstOrDefault();

            if (!borderStyle.HasValue)
            {
                throw new ArgumentException(nameof(values));
            }

            double borderWidth = values.Select(value => value.GetString())
                                 .Select(value => PropertyUtilities.TryParseNumber(value, out double result) ? (double?)result : null)
                                 .Where(result => result != null)
                                 .FirstOrDefault() ?? 0.0;

            Color borderColor = values.Select(value => value.GetString())
                                .Select(value => PropertyUtilities.TryParseColor(value, out Color result) ? (Color?)result : null)
                                .Where(result => result != null)
                                .FirstOrDefault() ?? default;

            return(new Border(borderWidth, borderStyle.Value, borderColor));
        }
Ejemplo n.º 2
0
        public double GetNumber()
        {
            switch (Type)
            {
            case StyleObjectType.Number:
                return((double)value);

            case StyleObjectType.String:
                return(PropertyUtilities.ParseNumber(GetString()));

            default:
                throw GetInvalidOperationException <double>();
            }
        }
Ejemplo n.º 3
0
        public BorderStyle GetBorderStyle()
        {
            switch (Type)
            {
            case StyleObjectType.BorderStyle:
                return((BorderStyle)value);

            case StyleObjectType.String:
                return(PropertyUtilities.ParseBorderStyle(GetString()));

            default:
                throw GetInvalidOperationException <BorderStyle>();
            }
        }
Ejemplo n.º 4
0
        public Color GetColor()
        {
            switch (Type)
            {
            case StyleObjectType.Color:
                return((Color)value);

            case StyleObjectType.String:
                return(PropertyUtilities.ParseColor(GetString()));

            default:
                throw GetInvalidOperationException <Color>();
            }
        }
Ejemplo n.º 5
0
        private StyleObject ReadFunction(IStyleSheetLexer lexer, string functionName)
        {
            List <StyleObject> functionArgs = new List <StyleObject>();

            bool exitLoop = false;

            while (!exitLoop && lexer.Read(out IStyleSheetLexerToken token))
            {
                switch (token.Type)
                {
                case StyleSheetLexerTokenType.Value:
                    functionArgs.Add(new StyleObject(token.Value));
                    break;

                case StyleSheetLexerTokenType.Function:
                    functionArgs.Add(ReadFunction(lexer, token.Value));
                    break;

                case StyleSheetLexerTokenType.FunctionArgumentsStart:
                    break;

                case StyleSheetLexerTokenType.FunctionArgumentSeparator:
                    break;

                case StyleSheetLexerTokenType.FunctionArgumentsEnd:
                    exitLoop = true;
                    break;

                default:
                    throw new UnexpectedTokenException($"Unexpected token: {token.Type}.");
                }
            }

            StyleObject returnValue = PropertyUtilities.EvaluateFunction(functionName, functionArgs.ToArray(), options.FileReader);

            if (returnValue.Type == StyleObjectType.Image)
            {
                disposableResources.Add(returnValue.GetImage());
            }

            return(returnValue);
        }
Ejemplo n.º 6
0
        public string GetString()
        {
            switch (Type)
            {
            case StyleObjectType.BorderStyle:
                return(PropertyUtilities.ToString((BorderStyle)value));

            case StyleObjectType.Color:
                return(((Color)value).ToString());

            case StyleObjectType.Image:
                return(((IImage)value).ToString());

            case StyleObjectType.Number:
                return(((double)value).ToString(CultureInfo.InvariantCulture));

            case StyleObjectType.String:
                return((string)value);

            default:
                throw new Exception("The underlying type cannot be converted to a string.");
            }
        }
 public override string ToString()
 {
     return(ToString(PropertyUtilities.ToString(Value).ToLowerInvariant()));
 }
Ejemplo n.º 8
0
 public ColorProperty(PropertyType propertyType, string propertyValue, bool inheritable = true) :
     base(propertyType, PropertyUtilities.ParseColor(propertyValue), inheritable)
 {
 }