public void GetBrush_ShouldReturnBrushWithOpacity()
        {
            var styleAttributes = new Dictionary<string, string> {{"fill-opacity", "0.2"}};
            var expectedBrush = new SolidBrush(new Color(0, 0, 0, 0.2));

            var actualBrush = _stylesParser.GetBrush(styleAttributes, new Dictionary<string, XElement>(), null);

            actualBrush.Should().NotBeNull();
            ((SolidBrush) actualBrush).ShouldBeEquivalentTo(expectedBrush);
        }
        public void GetBrush_ShouldReturnBrushWithFillModeEvenOdd()
        {
            var styleAttributes = new Dictionary<string, string> {{"fill-rule", "evenodd"}};
            var expectedBrush = new SolidBrush
            {
                FillMode = FillMode.EvenOdd
            };

            var actualBrush = _stylesParser.GetBrush(styleAttributes, new Dictionary<string, XElement>(), null);

            actualBrush.Should().NotBeNull();
            ((SolidBrush) actualBrush).ShouldBeEquivalentTo(expectedBrush);
        }
        public void GetBrush_ShouldReturnBrushWithColor_IfFillHasValidColor_White()
        {
            var styleAttributes = new Dictionary<string, string> {{"fill", "#FFFFFF"}};
            var expectedBrush = new SolidBrush
            {
                Color = new Color(1, 1, 1)
            };

            var actualBrush = _stylesParser.GetBrush(styleAttributes, new Dictionary<string, XElement>(), null);

            actualBrush.Should().NotBeNull();
            ((SolidBrush) actualBrush).ShouldBeEquivalentTo(expectedBrush);
        }
Example #4
0
        public BaseBrush GetBrush(Dictionary<string, string> styleAttributes,Dictionary<string, XElement> defs, Pen pen)
        {
            BaseBrush baseBrush = null;

              var fillOpacity = GetString(styleAttributes, "fill-opacity");
              if (!string.IsNullOrWhiteSpace(fillOpacity))
              {
            if (baseBrush == null)
              baseBrush = new SolidBrush();
            var sb = baseBrush as SolidBrush;
            if (sb != null)
              sb.Color = sb.Color.WithAlpha(_valuesParser.ReadNumber(fillOpacity));
              }

              var fillRule = GetString(styleAttributes, "fill-rule");
              if (!string.IsNullOrWhiteSpace(fillRule))
              {
            if (baseBrush == null)
              baseBrush = new SolidBrush();
            var sb = baseBrush as SolidBrush;
            if (sb != null)
            {
              if (fillRule.Equals("evenodd"))
              {
            sb.FillMode = FillMode.EvenOdd;
              }
            }
              }

              var fill = GetString(styleAttributes, "fill").Trim();
              if (string.IsNullOrEmpty(fill))
              {
            // No change
              }
              else if (fill == "none")
              {
            baseBrush = null;
              }
              else
              {
            Color color;
            if (Colors.TryParse(fill, out color))
            {
              var sb = baseBrush as SolidBrush;
              if (sb == null)
              {
            baseBrush = new SolidBrush(color);
              }
              else
              {
            if (sb.Color.Alpha == 1 || pen == null)
              sb.Color = color;
            else
              sb.Color = color.WithAlpha(sb.Color.Alpha);
              }
            }
            else
            {
              var urlM = _fillUrlRe.Match(fill);
              if (urlM.Success)
              {
            var id = urlM.Groups[1].Value.Trim();
            XElement defE;
            if (defs.TryGetValue(id, out defE))
            {
              switch (defE.Name.LocalName)
              {
                case "linearGradient":
                  baseBrush = CreateLinearGradientBrush(defE);
                  break;
                case "radialGradient":
                  baseBrush = CreateRadialGradientBrush(defE);
                  break;
                default:
                  throw new NotSupportedException("Fill " + defE.Name);
              }
            }
            else
            {
              throw new Exception("Invalid fill url reference: " + id);
            }
              }
              else
              {
            throw new NotSupportedException("Fill " + fill);
              }
            }
              }

              return baseBrush;
        }
        public void GetBrush_ShouldReturnDefaultBrush_IfFillRuleIsDefault()
        {
            var styleAttributes = new Dictionary<string, string> {{"fill-rule", "nonzero"}};

            var expectedBrush = new SolidBrush
            {
                FillMode = FillMode.NonZero
            };

            var actualBrush = _stylesParser.GetBrush(styleAttributes, new Dictionary<string, XElement>(), null);

            actualBrush.Should().NotBeNull();
            ((SolidBrush) actualBrush).ShouldBeEquivalentTo(expectedBrush);
        }