public static Gdi.Brush ToGdi(this IBrush brush, Position pos1, Position pos2) { Gdi.Brush gdiBrush = brush switch { SolidBrush solidBrush => new Gdi.SolidBrush(solidBrush.Color.ToGdi()), GradientBrush gradienBrush => new Gdi.Drawing2D.LinearGradientBrush(pos1.ToGdi(), pos2.ToGdi(), gradienBrush.Color1.ToGdi(), gradienBrush.Color2.ToGdi()), _ => throw new NotImplementedException($"Brush of type {brush.GetType().Name} not implemented"), }; return(gdiBrush); }
private static void InitSkiaPaintForBrush(SKPaint paint, IBrush brush, IUIElement uiElement) { if (brush is ISolidColorBrush solidColorBrush) { paint.Color = ToSkiaColor(solidColorBrush.Color); } else if (brush is IGradientBrush gradientBrush) { paint.Shader = ToSkiaShader(gradientBrush, uiElement); } else { throw new InvalidOperationException($"Brush type {brush.GetType()} isn't currently supported"); } }
/// <summary> /// Verifies if the given brush is the same as the otherBrush. /// </summary> /// <param name="brush">Brush</param> /// <param name="otherBrush">Brush</param> /// <returns>true if yes, otherwise false</returns> public static bool IsEqualTo(this IBrush brush, IBrush otherBrush) { if (brush.GetType() != otherBrush.GetType()) { return(false); } if (ReferenceEquals(brush, otherBrush)) { return(true); } // Are both instances of SolidColorBrush var solidBrushA = brush as SolidColorBrush; var solidBrushB = otherBrush as SolidColorBrush; if ((solidBrushA != null) && (solidBrushB != null)) { return((solidBrushA.Color.Equals(solidBrushB.Color)) && solidBrushA.Opacity.IsCloseTo(solidBrushB.Opacity)); } // Are both instances of LinearGradientBrush var linGradBrushA = brush as LinearGradientBrush; var linGradBrushB = otherBrush as LinearGradientBrush; if ((linGradBrushA != null) && (linGradBrushB != null)) { var result = //(linGradBrushA.ColorInterpolationMode == linGradBrushB.ColorInterpolationMode) // && (linGradBrushA.EndPoint == linGradBrushB.EndPoint) //&& (linGradBrushA.MappingMode == linGradBrushB.MappingMode) && linGradBrushA.Opacity.IsCloseTo(linGradBrushB.Opacity) && (linGradBrushA.StartPoint == linGradBrushB.StartPoint) && (linGradBrushA.SpreadMethod == linGradBrushB.SpreadMethod) && (linGradBrushA.GradientStops.Count == linGradBrushB.GradientStops.Count); if (!result) { return(false); } for (var i = 0; i < linGradBrushA.GradientStops.Count; i++) { result = (linGradBrushA.GradientStops[i].Color.Equals(linGradBrushB.GradientStops[i].Color)) && linGradBrushA.GradientStops[i].Offset.IsCloseTo(linGradBrushB.GradientStops[i].Offset); if (!result) { break; } } return(result); } // Are both instances of RadialGradientBrush var radGradBrushA = brush as RadialGradientBrush; var radGradBrushB = otherBrush as RadialGradientBrush; if ((radGradBrushA != null) && (radGradBrushB != null)) { var result = //(radGradBrushA.ColorInterpolationMode == radGradBrushB.ColorInterpolationMode) //&& (radGradBrushA.GradientOrigin == radGradBrushB.GradientOrigin) //&& (radGradBrushA.MappingMode == radGradBrushB.MappingMode) && radGradBrushA.Opacity.IsCloseTo(radGradBrushB.Opacity) //&& radGradBrushA.RadiusX.IsCloseTo(radGradBrushB.RadiusX) //&& radGradBrushA.RadiusY.IsCloseTo(radGradBrushB.RadiusY) && (radGradBrushA.SpreadMethod == radGradBrushB.SpreadMethod) && (radGradBrushA.GradientStops.Count == radGradBrushB.GradientStops.Count); if (!result) { return(false); } for (var i = 0; i < radGradBrushA.GradientStops.Count; i++) { result = (radGradBrushA.GradientStops[i].Color.Equals(radGradBrushB.GradientStops[i].Color)) && radGradBrushA.GradientStops[i].Offset.IsCloseTo(radGradBrushB.GradientStops[i].Offset); if (!result) { break; } } return(result); } // Are both instances of ImageBrush var imgBrushA = brush as ImageBrush; var imgBrushB = otherBrush as ImageBrush; if ((imgBrushA != null) && (imgBrushB != null)) { var result = (imgBrushA.AlignmentX == imgBrushB.AlignmentX) && (imgBrushA.AlignmentY == imgBrushB.AlignmentY) && imgBrushA.Opacity.IsCloseTo(imgBrushB.Opacity) && (imgBrushA.Stretch == imgBrushB.Stretch) && (imgBrushA.TileMode == imgBrushB.TileMode) //&& (imgBrushA.Viewbox == imgBrushB.Viewbox) //&& (imgBrushA.ViewboxUnits == imgBrushB.ViewboxUnits) //&& (imgBrushA.Viewport == imgBrushB.Viewport) //&& (imgBrushA.ViewportUnits == imgBrushB.ViewportUnits) //&& (imgBrushA.ImageSource == imgBrushB.ImageSource) ; return(result); } return(false); }
public static SKPaint ToSKPaint(this IBrush brush) { if (brush is SolidColorBrush solidColorBrush) { return(new SKPaint() { Color = solidColorBrush.Color.ToSKColor(), IsAntialias = true, StrokeWidth = 1 }); } throw new NotImplementedException($"Unsupported brush type is passed in! Brush type: '{brush.GetType().FullName}'"); }