/// <summary> /// Parses a brush string. /// </summary> /// <param name="s">The brush string.</param> /// <returns>The <see cref="Color"/>.</returns> public static IBrush Parse(string s) { Contract.Requires <ArgumentNullException>(s != null); Contract.Requires <FormatException>(s.Length > 0); if (s[0] == '#') { return(new ImmutableSolidColorBrush(Color.Parse(s))); } var brush = KnownColors.GetKnownBrush(s); if (brush != null) { return(brush); } throw new FormatException($"Invalid brush string: '{s}'."); }
/// <summary> /// Parses a brush string. /// </summary> /// <param name="s">The brush string.</param> /// <returns>The <see cref="Color"/>.</returns> public static IBrush Parse(string s) { _ = s ?? throw new ArgumentNullException(nameof(s)); if (s.Length > 0) { if (s[0] == '#') { return(new ImmutableSolidColorBrush(Color.Parse(s))); } var brush = KnownColors.GetKnownBrush(s); if (brush != null) { return(brush); } } throw new FormatException($"Invalid brush string: '{s}'."); }
/// <summary> /// Parses a brush string. /// </summary> /// <param name="s">The brush string.</param> /// <returns>The <see cref="Color"/>.</returns> public static IBrush Parse(string s) { if (s[0] == '#') { return(new SolidColorBrush(Color.Parse(s))); } else { var upper = s.ToUpperInvariant(); var member = typeof(Brushes).GetTypeInfo().DeclaredProperties .FirstOrDefault(x => x.Name.ToUpperInvariant() == upper); if (member != null) { return((IBrush)member.GetValue(null)); } else { throw new FormatException($"Invalid brush string: '{s}'."); } } }
/// <summary> /// Refreshes the tile position or draws a new one if it did not exist in the canvas /// </summary> /// <param name="tileXy"></param> /// <returns>Rectangle object mapped to the tile</returns> public Rectangle DrawTile(TileXy tileXy) { if (tileXy.Tile == null) { return(null); } Rectangle rectangle; if (_tileRectangleMap.ContainsKey(tileXy.Tile)) { rectangle = _tileRectangleMap[tileXy.Tile]; } else { var color = Color.Parse(tileXy.Tile.Color); rectangle = NewRectangle(color); _tileRectangleMap.Add(tileXy.Tile, rectangle); } Canvas.SetLeft(rectangle, tileXy.X * FieldHelper.BlockWidth + 1); Canvas.SetTop(rectangle, tileXy.Y * FieldHelper.BlockHeight + 1); return(rectangle); }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { Type typeOfValue = value == null ? null : value.GetType(); var pstr = (string)(parameter.ToString()); var ss = pstr.Split(' '); object res = null; string str = null; bool matches = false; var matchFn = false; if (pstr.StartsWith("?")) { matchFn = true; if (pstr.StartsWith("?IsNull ")) { matches = value == null; } else if (pstr == "?Darker" || pstr.StartsWith("?Darker ")) { if (value == null) { return(null); } var brush = value as SolidColorBrush; var col = brush.Color; var f = 0.5; if (ss.Length > 1) { f = double.Parse(ss[1]); } var newCol = Color.FromArgb(col.A, (byte)(col.R / 255d * f * 255d), (byte)(col.G / 255d * f * 255d), (byte)(col.B / 255d * f * 255d)); res = new SolidColorBrush(newCol); return(res); } else if (pstr == "?Lighter" || pstr.StartsWith("?Lighter ")) { if (value == null) { return(null); } var brush = value as SolidColorBrush; var col = brush.Color; var f = 0.5; if (ss.Length > 1) { f = double.Parse(ss[1]); } var newCol = Color.FromArgb(col.A, (byte)(col.R + (1d - col.R / 255d) * f * 255d), (byte)(col.G + (1d - col.G / 255d) * f * 255d), (byte)(col.B / (1d - col.B / 255d) * f * 255d)); res = new SolidColorBrush(newCol); return(res); } else if (pstr.StartsWith("?Round ")) { var dec = int.Parse(ss[1]); if (typeOfValue == typeofDecimal) { res = Round((decimal)value, dec); } else if (typeOfValue == typeofFloat) { res = Round((float)value, dec); } else if (typeOfValue == typeofDouble) { res = Round((double)value, dec); } return(System.Convert.ChangeType(res, targetType)); } else { matchFn = false; } } if (!matchFn) { str = (string)(value == null ? "" : value.ToString()); if (targetType == typeofBoolean || typeOfValue == typeofBoolean) { var tl = str.ToLower(); if (tl == "true" || tl == "false") { str = tl; } } matches = str == ss[0].ToLower(); if (!matches && ss.Length <= 2) { return(null); } } var i = matches ? 1 : 2; if (targetType == typeofBoolean) { res = bool.Parse(ss[i]); } else if (targetType == typeofThickness) { res = new Thickness(int.Parse(ss[i])); } else if (targetType == typeofIBrush) { res = new SolidColorBrush(Color.Parse(ss[i])); } else if (targetType == typeofDouble) { res = double.Parse(ss[i]); } else { res = System.Convert.ChangeType(ss[i], targetType); } return(res); }