public static string ToImageString(this IHtmlValueConverter converter, Brush brush) { if (brush is LinearGradientBrush) { return(converter.ToImageString((LinearGradientBrush)brush)); } if (brush is RadialGradientBrush) { return(converter.ToImageString((RadialGradientBrush)brush)); } if (brush is ImageBrush) { return(converter.ToImageString((ImageBrush)brush)); } throw new Granular.Exception("Unexpected brush type \"{0}\"", brush.GetType()); }
public void HtmlValueConverterBasicTest() { IHtmlValueConverter converter = HtmlValueConverter.Default; Assert.AreEqual("1.23px", converter.ToPixelString(1.234)); Assert.AreEqual("123.46%", converter.ToPercentString(1.23456)); Assert.AreEqual("1.23deg", converter.ToDegreesString(1.234)); Assert.AreEqual("1.23px 5.68px", converter.ToPixelString(new Point(1.234, 5.678))); Assert.AreEqual("123.46% 567.89%", converter.ToPercentString(new Point(1.23456, 5.6789))); Assert.AreEqual("rgba(12, 34, 56, 0.25)", converter.ToColorString(Color.FromArgb(64, 12, 34, 56))); Assert.AreEqual("#0c2238", converter.ToColorString(Color.FromRgb(12, 34, 56))); Assert.AreEqual("1.23px", converter.ToPixelString(new Thickness(1.234))); Assert.AreEqual("2.35px 3.46px 4.57px 1.23px", converter.ToPixelString(new Thickness(1.234, 2.345, 3.456, 4.567))); Assert.AreEqual("2.35 3.46 4.57 1.23", converter.ToImplicitValueString(new Thickness(1.234, 2.345, 3.456, 4.567))); Assert.AreEqual("url(image-source)", converter.ToUrlString("image-source")); Assert.AreEqual("linear-gradient(135deg, #ff0000 0%, #0000ff 100%)", converter.ToLinearGradientString(new LinearGradientBrush(45, Colors.Red, Colors.Blue), new Rect(100, 100))); Assert.AreEqual("repeating-linear-gradient(135deg, #ff0000 0%, #0000ff 100%)", converter.ToLinearGradientString(new LinearGradientBrush(45, Colors.Red, Colors.Blue) { SpreadMethod = GradientSpreadMethod.Repeat }, new Rect(100, 100))); Assert.AreEqual("linear-gradient(135deg, #ff0000 0%, #0000ff 50%, #0000ff 50%, #ff0000 100%)", converter.ToLinearGradientString(new LinearGradientBrush(45, Colors.Red, Colors.Blue) { SpreadMethod = GradientSpreadMethod.Reflect }, new Rect(100, 100))); Assert.AreEqual("radial-gradient(ellipse 50% 50% at 50% 50%, #ff0000 0%, #0000ff 100%)", converter.ToRadialGradientString(new RadialGradientBrush(Colors.Red, Colors.Blue))); Assert.AreEqual("repeating-radial-gradient(ellipse 50% 50% at 50% 50%, #ff0000 0%, #0000ff 100%)", converter.ToRadialGradientString(new RadialGradientBrush(Colors.Red, Colors.Blue) { SpreadMethod = GradientSpreadMethod.Repeat })); Assert.AreEqual("#ff0000 0%, #0000ff 100%", converter.ToColorStopsString(new GradientStop[] { new GradientStop(Colors.Red, 0), new GradientStop(Colors.Blue, 1) })); Assert.AreEqual("#ff0000", converter.ToColorString(new SolidColorBrush(Colors.Red))); Assert.AreEqual("linear-gradient(135deg, #ff0000 0%, #0000ff 100%)", converter.ToImageString(new LinearGradientBrush(45, Colors.Red, Colors.Blue), new Rect(100, 100))); Assert.AreEqual("radial-gradient(ellipse 50% 50% at 50% 50%, #ff0000 0%, #0000ff 100%)", converter.ToImageString(new RadialGradientBrush(Colors.Red, Colors.Blue))); Assert.AreEqual("url(image-source)", converter.ToImageString(new ImageBrush() { ImageSource = "image-source" })); }
public static void SetBackground(this HtmlStyleDictionary style, Brush background, Rect targetRect, IHtmlValueConverter converter) { style.ClearValue("background-color"); style.ClearValue("background-image"); if (background is SolidColorBrush) { style.SetValue("background-color", converter.ToColorString((SolidColorBrush)background)); } else if (background != null) { style.SetValue("background-image", converter.ToImageString(background, targetRect)); } }
public static void SetBorderBrush(this HtmlStyleDictionary style, Brush borderBrush, IHtmlValueConverter converter) { style.ClearValue("border-color"); style.ClearValue("border-image-source"); if (borderBrush is SolidColorBrush) { style.SetValue("border-color", converter.ToColorString((SolidColorBrush)borderBrush)); } else if (borderBrush != null) { style.SetValue("border-image-source", converter.ToImageString(borderBrush)); } }
public static void SetBackground(this HtmlStyleDictionary style, Brush background, IHtmlValueConverter converter) { style.ClearValue("background-color"); style.ClearValue("background-image"); if (background is SolidColorBrush) { style.SetValue("background-color", converter.ToColorString((SolidColorBrush)background)); } else if (background != null) { style.SetValue("background-image", converter.ToImageString(background)); } }