Exemple #1
0
		public static SKShader CreateLinearGradient (SKPoint start, SKPoint end, SKColor [] colors, float [] colorPos, SKShaderTileMode mode, SKMatrix localMatrix)
		{
			if (colors == null)
				throw new ArgumentNullException (nameof (colors));
			if (colorPos == null) {
				return GetObject<SKShader> (SkiaApi.sk_shader_new_linear_gradient (new SKPoint [] { start, end }, colors, IntPtr.Zero, colors.Length, mode, ref localMatrix));
			} else {
				if (colors.Length != colorPos.Length)
					throw new ArgumentException ("The number of colors must match the number of color positions.");
				return GetObject<SKShader> (SkiaApi.sk_shader_new_linear_gradient (new SKPoint [] { start, end }, colors, colorPos, colors.Length, mode, ref localMatrix));
			}
		}
Exemple #2
0
		public static SKShader CreateTwoPointConicalGradient (SKPoint start, float startRadius, SKPoint end, float endRadius, SKColor [] colors, float [] colorPos, SKShaderTileMode mode, SKMatrix localMatrix)
		{
			if (colors == null)
				throw new ArgumentNullException (nameof (colors));
			if (colorPos == null) {
				return GetObject<SKShader> (SkiaApi.sk_shader_new_two_point_conical_gradient (ref start, startRadius, ref end, endRadius, colors, IntPtr.Zero, colors.Length, mode, ref localMatrix));
			} else {
				if (colors.Length != colorPos.Length)
					throw new ArgumentException ("The number of colors must match the number of color positions.");
				return GetObject<SKShader> (SkiaApi.sk_shader_new_two_point_conical_gradient (ref start, startRadius, ref end, endRadius, colors, colorPos, colors.Length, mode, ref localMatrix));
			}
		}
Exemple #3
0
		public extern static sk_shader_t sk_shader_new_radial_gradient(ref SKPoint center, float radius, [In] SKColor[] colors, IntPtr colorPosZero, int count, SKShaderTileMode mode, IntPtr matrixZero);
Exemple #4
0
		public extern static sk_shader_t sk_shader_new_radial_gradient(ref SKPoint center, float radius, [In] SKColor[] colors, float[] colorPos, int count, SKShaderTileMode mode, ref SKMatrix matrix);
Exemple #5
0
		public extern static sk_shader_t sk_shader_new_bitmap(sk_bitmap_t src, SKShaderTileMode tmx, SKShaderTileMode tmy, IntPtr matrixZero);
Exemple #6
0
 public static SKShader CreateLinearGradient(SKPoint start, SKPoint end, SKColorF[] colors, SKColorSpace colorspace, SKShaderTileMode mode) =>
 CreateLinearGradient(start, end, colors, colorspace, null, mode);
Exemple #7
0
 public SKLinearGradient(SKPoint start, SKPoint end, float[] positions, SKColor[] colors, SKShaderTileMode tileMode, SKMatrix matrix)
 {
     Start     = start;
     End       = end;
     Positions = positions;
     Colors    = colors;
     TileMode  = tileMode;
     Matrix    = matrix;
 }
Exemple #8
0
 public SKShader ToShader(SKShaderTileMode tileX, SKShaderTileMode tileY) =>
 SKShader.GetObject(SkiaApi.sk_image_make_shader(Handle, tileX, tileY, null));
Exemple #9
0
        public static SKShader CreateSweepGradient(SKPoint center, SKColor [] colors, float [] colorPos, SKShaderTileMode tileMode, float startAngle, float endAngle, SKMatrix localMatrix)
        {
            if (colors == null)
            {
                throw new ArgumentNullException(nameof(colors));
            }
            if (colorPos != null && colors.Length != colorPos.Length)
            {
                throw new ArgumentException("The number of colors must match the number of color positions.");

                fixed(SKColor *c = colors)
                fixed(float *cp = colorPos)
                {
                    return(GetObject <SKShader> (SkiaApi.sk_shader_new_sweep_gradient(&center, c, cp, colors.Length, tileMode, startAngle, endAngle, &localMatrix)));
                }
        }
Exemple #10
0
 public static SKShader CreateSweepGradient(SKPoint center, SKColor[] colors, SKShaderTileMode tileMode, float startAngle, float endAngle) =>
 CreateSweepGradient(center, colors, null, tileMode, startAngle, endAngle);
Exemple #11
0
 public static SKShader CreateRadialGradient(SKPoint center, float radius, SKColor[] colors, SKShaderTileMode mode) =>
 CreateRadialGradient(center, radius, colors, null, mode);
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            SKColor[]        colors   = { SKColors.Red, SKColors.Green, SKColors.Blue };
            SKShaderTileMode tileMode =
                (SKShaderTileMode)(tileModePicker.SelectedIndex == -1 ?
                                   0 : tileModePicker.SelectedItem);

            using (SKPaint paint = new SKPaint())
            {
                paint.Shader = SKShader.CreateLinearGradient(touchPoints[0].Center,
                                                             touchPoints[1].Center,
                                                             colors,
                                                             null,
                                                             tileMode);
                canvas.DrawRect(info.Rect, paint);
            }

            // Display the touch points here rather than by TouchPoint
            using (SKPaint paint = new SKPaint())
            {
                paint.Style       = SKPaintStyle.Stroke;
                paint.Color       = SKColors.Black;
                paint.StrokeWidth = 3;

                foreach (TouchPoint touchPoint in touchPoints)
                {
                    canvas.DrawCircle(touchPoint.Center, touchPoint.Radius, paint);
                }

                // Draw gradient line connecting touchpoints
                canvas.DrawLine(touchPoints[0].Center, touchPoints[1].Center, paint);

                // Draw lines perpendicular to the gradient line
                SKPoint vector = touchPoints[1].Center - touchPoints[0].Center;
                float   length = (float)Math.Sqrt(Math.Pow(vector.X, 2) +
                                                  Math.Pow(vector.Y, 2));
                vector.X /= length;
                vector.Y /= length;
                SKPoint rotate90 = new SKPoint(-vector.Y, vector.X);
                rotate90.X *= 200;
                rotate90.Y *= 200;

                canvas.DrawLine(touchPoints[0].Center,
                                touchPoints[0].Center + rotate90,
                                paint);

                canvas.DrawLine(touchPoints[0].Center,
                                touchPoints[0].Center - rotate90,
                                paint);

                canvas.DrawLine(touchPoints[1].Center,
                                touchPoints[1].Center + rotate90,
                                paint);

                canvas.DrawLine(touchPoints[1].Center,
                                touchPoints[1].Center - rotate90,
                                paint);
            }
        }
        internal PaintWrapper CreatePaint(IBrush brush, Size targetSize)
        {
            SKPaint paint = new SKPaint();
            var     rv    = new PaintWrapper(paint);

            paint.IsStroke = false;

            // TODO: SkiaSharp does not contain alpha yet!
            double opacity = brush.Opacity * _currentOpacity;

            //paint.SetAlpha(paint.GetAlpha() * opacity);
            paint.IsAntialias = true;

            SKColor color = new SKColor(255, 255, 255, 255);

            var solid = brush as ISolidColorBrush;

            if (solid != null)
            {
                color = solid.Color.ToSKColor();
            }

            paint.Color = (new SKColor(color.Red, color.Green, color.Blue, (byte)(color.Alpha * opacity)));

            if (solid != null)
            {
                return(rv);
            }

            var gradient = brush as GradientBrush;

            if (gradient != null)
            {
                var tileMode    = gradient.SpreadMethod.ToSKShaderTileMode();
                var stopColors  = gradient.GradientStops.Select(s => s.Color.ToSKColor()).ToArray();
                var stopOffsets = gradient.GradientStops.Select(s => (float)s.Offset).ToArray();

                var linearGradient = brush as LinearGradientBrush;
                if (linearGradient != null)
                {
                    var start = linearGradient.StartPoint.ToPixels(targetSize).ToSKPoint();
                    var end   = linearGradient.EndPoint.ToPixels(targetSize).ToSKPoint();

                    // would be nice to cache these shaders possibly?
                    var shader = SKShader.CreateLinearGradient(start, end, stopColors, stopOffsets, tileMode);
                    paint.Shader = shader;
                    shader.Dispose();
                }
                else
                {
                    var radialGradient = brush as RadialGradientBrush;
                    if (radialGradient != null)
                    {
                        var center = radialGradient.Center.ToPixels(targetSize).ToSKPoint();
                        var radius = (float)radialGradient.Radius;

                        // TODO: There is no SetAlpha in SkiaSharp
                        //paint.setAlpha(128);

                        // would be nice to cache these shaders possibly?
                        var shader = SKShader.CreateRadialGradient(center, radius, stopColors, stopOffsets, tileMode);
                        paint.Shader = shader;
                        shader.Dispose();
                    }
                }

                return(rv);
            }

            var tileBrush = brush as TileBrush;

            if (tileBrush != null)
            {
                var helper = new TileBrushImplHelper(tileBrush, targetSize);
                var bitmap = new BitmapImpl((int)helper.IntermediateSize.Width, (int)helper.IntermediateSize.Height);
                rv.AddDisposable(bitmap);
                using (var ctx = bitmap.CreateDrawingContext())
                    helper.DrawIntermediate(ctx);
                SKMatrix         translation = SKMatrix.MakeTranslation(-(float)helper.DestinationRect.X, -(float)helper.DestinationRect.Y);
                SKShaderTileMode tileX       =
                    tileBrush.TileMode == TileMode.None
                        ? SKShaderTileMode.Clamp
                        : tileBrush.TileMode == TileMode.FlipX || tileBrush.TileMode == TileMode.FlipXY
                            ? SKShaderTileMode.Mirror
                            : SKShaderTileMode.Repeat;

                SKShaderTileMode tileY =
                    tileBrush.TileMode == TileMode.None
                        ? SKShaderTileMode.Clamp
                        : tileBrush.TileMode == TileMode.FlipY || tileBrush.TileMode == TileMode.FlipXY
                            ? SKShaderTileMode.Mirror
                            : SKShaderTileMode.Repeat;
                paint.Shader = SKShader.CreateBitmap(bitmap.Bitmap, tileX, tileY, translation);
                paint.Shader.Dispose();
            }

            return(rv);
        }
Exemple #14
0
 public static SKShader CreateLinearGradient(SKPoint start, SKPoint end, SKColor [] colors, float [] colorPos, SKShaderTileMode mode)
 {
     if (colors == null)
     {
         throw new ArgumentNullException(nameof(colors));
     }
     if (colorPos == null)
     {
         return(GetObject <SKShader> (SkiaApi.sk_shader_new_linear_gradient(new SKPoint [] { start, end }, colors, IntPtr.Zero, colors.Length, mode, IntPtr.Zero)));
     }
     else
     {
         if (colors.Length != colorPos.Length)
         {
             throw new ArgumentException("The number of colors must match the number of color positions.");
         }
         return(GetObject <SKShader> (SkiaApi.sk_shader_new_linear_gradient(new SKPoint [] { start, end }, colors, colorPos, colors.Length, mode, IntPtr.Zero)));
     }
 }
Exemple #15
0
 public static SKShader CreateTwoPointConicalGradient(SKPoint start, float startRadius, SKPoint end, float endRadius, SKColor [] colors, float [] colorPos, SKShaderTileMode mode, SKMatrix localMatrix)
 {
     if (colors == null)
     {
         throw new ArgumentNullException(nameof(colors));
     }
     if (colorPos == null)
     {
         return(GetObject <SKShader> (SkiaApi.sk_shader_new_two_point_conical_gradient(ref start, startRadius, ref end, endRadius, colors, IntPtr.Zero, colors.Length, mode, ref localMatrix)));
     }
     else
     {
         if (colors.Length != colorPos.Length)
         {
             throw new ArgumentException("The number of colors must match the number of color positions.");
         }
         return(GetObject <SKShader> (SkiaApi.sk_shader_new_two_point_conical_gradient(ref start, startRadius, ref end, endRadius, colors, colorPos, colors.Length, mode, ref localMatrix)));
     }
 }
Exemple #16
0
 public static SKShader CreateTwoPointConicalGradient(SKPoint start, float startRadius, SKPoint end, float endRadius, SKColor[] colors, SKShaderTileMode mode) =>
 CreateTwoPointConicalGradient(start, startRadius, end, endRadius, colors, null, mode);
Exemple #17
0
 public static SKShader CreateRadialGradient(SKPoint center, float radius, SKColor [] colors, float [] colorPos, SKShaderTileMode mode)
 {
     if (colors == null)
     {
         throw new ArgumentNullException(nameof(colors));
     }
     if (colorPos == null)
     {
         return(GetObject <SKShader> (SkiaApi.sk_shader_new_radial_gradient(ref center, radius, colors, IntPtr.Zero, colors.Length, mode, IntPtr.Zero)));
     }
     else
     {
         if (colors.Length != colorPos.Length)
         {
             throw new ArgumentException("The number of colors must match the number of color positions.");
         }
         return(GetObject <SKShader> (SkiaApi.sk_shader_new_radial_gradient(ref center, radius, colors, colorPos, colors.Length, mode, IntPtr.Zero)));
     }
 }
Exemple #18
0
        public static SKShader CreateTwoPointConicalGradient(SKPoint start, float startRadius, SKPoint end, float endRadius, SKColor [] colors, float [] colorPos, SKShaderTileMode mode)
        {
            if (colors == null)
            {
                throw new ArgumentNullException(nameof(colors));
            }
            if (colorPos != null && colors.Length != colorPos.Length)
            {
                throw new ArgumentException("The number of colors must match the number of color positions.");

                fixed(SKColor *c = colors)
                fixed(float *cp = colorPos)
                {
                    return(GetObject <SKShader> (SkiaApi.sk_shader_new_two_point_conical_gradient(&start, startRadius, &end, endRadius, c, cp, colors.Length, mode, null)));
                }
        }
Exemple #19
0
 public SKShader ToShader(SKShaderTileMode tileX, SKShaderTileMode tileY, SKMatrix localMatrix) =>
 SKShader.GetObject(SkiaApi.sk_image_make_shader(Handle, tileX, tileY, &localMatrix));
Exemple #20
0
 public static SKShader CreateTwoPointConicalGradient(SKPoint start, float startRadius, SKPoint end, float endRadius, SKColor [] colors, float [] colorPos, SKShaderTileMode mode, SKMatrix localMatrix)
 {
     if (colors == null)
         throw new ArgumentNullException(nameof(colors)); }
Exemple #21
0
        public static SKShader CreateLinearGradient(SKPoint start, SKPoint end, SKColor[] colors, float[] colorPos, SKShaderTileMode mode, SKMatrix localMatrix)
        {
            if (colors == null)
            {
                throw new ArgumentNullException(nameof(colors));
            }
            if (colorPos != null && colors.Length != colorPos.Length)
            {
                throw new ArgumentException("The number of colors must match the number of color positions.");
            }

            var points = stackalloc SKPoint[] { start, end };

            fixed(SKColor *c = colors)
            fixed(float *cp = colorPos)
            {
                return(GetObject(SkiaApi.sk_shader_new_linear_gradient(points, (uint *)c, cp, colors.Length, mode, &localMatrix)));
            }
        }
Exemple #22
0
 public SKShader ToShader(SKShaderTileMode tileX, SKShaderTileMode tileY)
 {
     return(GetObject <SKShader> (SkiaApi.sk_image_make_shader(Handle, tileX, tileY, IntPtr.Zero)));
 }
Exemple #23
0
		public extern static sk_shader_t sk_shader_new_bitmap(sk_bitmap_t src, SKShaderTileMode tmx, SKShaderTileMode tmy, ref SKMatrix matrix);
Exemple #24
0
 public SKShader ToShader(SKShaderTileMode tileX, SKShaderTileMode tileY, SKMatrix localMatrix)
 {
     return(GetObject <SKShader> (SkiaApi.sk_image_make_shader(Handle, tileX, tileY, ref localMatrix)));
 }
Exemple #25
0
		public extern static sk_shader_t sk_shader_new_linear_gradient([In] SKPoint[] points, [In] SKColor[] colors, float[] colorPos, int count, SKShaderTileMode mode, ref SKMatrix matrix);
Exemple #26
0
 public SKShader ToShader(SKShaderTileMode tmx, SKShaderTileMode tmy) =>
 SKShader.GetObject(SkiaApi.sk_picture_make_shader(Handle, tmx, tmy, null, null));
Exemple #27
0
		public extern static sk_shader_t sk_shader_new_linear_gradient([In] SKPoint[] points, [In] SKColor[] colors, IntPtr colorPosZero, int count, SKShaderTileMode mode, IntPtr matrixZero);
Exemple #28
0
 public SKShader ToShader(SKShaderTileMode tmx, SKShaderTileMode tmy, SKMatrix localMatrix, SKRect tile) =>
 SKShader.GetObject(SkiaApi.sk_picture_make_shader(Handle, tmx, tmy, &localMatrix, &tile));
Exemple #29
0
		public extern static sk_shader_t sk_shader_new_two_point_conical_gradient(ref SKPoint start, float startRadius, ref SKPoint end, float endRadius, [In] SKColor[] colors, IntPtr colorPosZero, int count, SKShaderTileMode mode, IntPtr matrixZero);
        internal PaintWrapper CreatePaint(IBrush brush, Size targetSize)
        {
            SKPaint paint = new SKPaint();
            var     rv    = new PaintWrapper(paint);

            paint.IsStroke = false;

            // TODO: SkiaSharp does not contain alpha yet!
            double opacity = brush.Opacity * _currentOpacity;

            //paint.SetAlpha(paint.GetAlpha() * opacity);
            paint.IsAntialias = true;

            SKColor color = new SKColor(255, 255, 255, 255);

            var solid = brush as ISolidColorBrush;

            if (solid != null)
            {
                color = solid.Color.ToSKColor();
            }

            paint.Color = (new SKColor(color.Red, color.Green, color.Blue, (byte)(color.Alpha * opacity)));

            if (solid != null)
            {
                return(rv);
            }

            var gradient = brush as IGradientBrush;

            if (gradient != null)
            {
                var tileMode    = gradient.SpreadMethod.ToSKShaderTileMode();
                var stopColors  = gradient.GradientStops.Select(s => s.Color.ToSKColor()).ToArray();
                var stopOffsets = gradient.GradientStops.Select(s => (float)s.Offset).ToArray();

                var linearGradient = brush as ILinearGradientBrush;
                if (linearGradient != null)
                {
                    var start = linearGradient.StartPoint.ToPixels(targetSize).ToSKPoint();
                    var end   = linearGradient.EndPoint.ToPixels(targetSize).ToSKPoint();

                    // would be nice to cache these shaders possibly?
                    using (var shader = SKShader.CreateLinearGradient(start, end, stopColors, stopOffsets, tileMode))
                        paint.Shader = shader;
                }
                else
                {
                    var radialGradient = brush as IRadialGradientBrush;
                    if (radialGradient != null)
                    {
                        var center = radialGradient.Center.ToPixels(targetSize).ToSKPoint();
                        var radius = (float)radialGradient.Radius;

                        // TODO: There is no SetAlpha in SkiaSharp
                        //paint.setAlpha(128);

                        // would be nice to cache these shaders possibly?
                        using (var shader = SKShader.CreateRadialGradient(center, radius, stopColors, stopOffsets, tileMode))
                            paint.Shader = shader;
                    }
                }

                return(rv);
            }

            var tileBrush      = brush as ITileBrush;
            var visualBrush    = brush as IVisualBrush;
            var tileBrushImage = default(BitmapImpl);

            if (visualBrush != null)
            {
                if (_visualBrushRenderer != null)
                {
                    var intermediateSize = _visualBrushRenderer.GetRenderTargetSize(visualBrush);

                    if (intermediateSize.Width >= 1 && intermediateSize.Height >= 1)
                    {
                        var intermediate = new BitmapImpl((int)intermediateSize.Width, (int)intermediateSize.Height);

                        using (var ctx = intermediate.CreateDrawingContext(_visualBrushRenderer))
                        {
                            ctx.Clear(Colors.Transparent);
                            _visualBrushRenderer.RenderVisualBrush(ctx, visualBrush);
                        }

                        rv.AddDisposable(tileBrushImage);
                        tileBrushImage = intermediate;
                    }
                }
                else
                {
                    throw new NotSupportedException("No IVisualBrushRenderer was supplied to DrawingContextImpl.");
                }
            }
            else
            {
                tileBrushImage = (BitmapImpl)((tileBrush as IImageBrush)?.Source?.PlatformImpl);
            }

            if (tileBrush != null && tileBrushImage != null)
            {
                var calc   = new TileBrushCalculator(tileBrush, new Size(tileBrushImage.PixelWidth, tileBrushImage.PixelHeight), targetSize);
                var bitmap = new BitmapImpl((int)calc.IntermediateSize.Width, (int)calc.IntermediateSize.Height);
                rv.AddDisposable(bitmap);
                using (var context = bitmap.CreateDrawingContext(null))
                {
                    var rect = new Rect(0, 0, tileBrushImage.PixelWidth, tileBrushImage.PixelHeight);

                    context.Clear(Colors.Transparent);
                    context.PushClip(calc.IntermediateClip);
                    context.Transform = calc.IntermediateTransform;
                    context.DrawImage(tileBrushImage, 1, rect, rect);
                    context.PopClip();
                }

                SKMatrix         translation = SKMatrix.MakeTranslation(-(float)calc.DestinationRect.X, -(float)calc.DestinationRect.Y);
                SKShaderTileMode tileX       =
                    tileBrush.TileMode == TileMode.None
                        ? SKShaderTileMode.Clamp
                        : tileBrush.TileMode == TileMode.FlipX || tileBrush.TileMode == TileMode.FlipXY
                            ? SKShaderTileMode.Mirror
                            : SKShaderTileMode.Repeat;

                SKShaderTileMode tileY =
                    tileBrush.TileMode == TileMode.None
                        ? SKShaderTileMode.Clamp
                        : tileBrush.TileMode == TileMode.FlipY || tileBrush.TileMode == TileMode.FlipXY
                            ? SKShaderTileMode.Mirror
                            : SKShaderTileMode.Repeat;
                using (var shader = SKShader.CreateBitmap(bitmap.Bitmap, tileX, tileY, translation))
                    paint.Shader = shader;
            }

            return(rv);
        }
Exemple #31
0
		public static SKShader CreateBitmap (SKBitmap src, SKShaderTileMode tmx, SKShaderTileMode tmy, SKMatrix localMatrix)
		{
			if (src == null)
				throw new ArgumentNullException (nameof (src));
			return GetObject<SKShader> (SkiaApi.sk_shader_new_bitmap (src.Handle, tmx, tmy, ref localMatrix));
		}
 public SKRadialGradient(SKPoint center, float radius, float[] positions, SKColor[] colors, SKShaderTileMode tileMode, SKMatrix matrix, bool absolute)
 {
     Absolute  = absolute;
     Center    = center;
     Radius    = radius;
     Positions = positions;
     Colors    = colors;
     TileMode  = tileMode;
     Matrix    = matrix;
 }
Exemple #33
0
		public static SKShader CreateRadialGradient (SKPoint center, float radius, SKColor [] colors, float [] colorPos, SKShaderTileMode mode)
		{
			if (colors == null)
				throw new ArgumentNullException (nameof (colors));
			if (colorPos == null) {
				return GetObject<SKShader> (SkiaApi.sk_shader_new_radial_gradient (ref center, radius, colors, IntPtr.Zero, colors.Length, mode, IntPtr.Zero));
			} else {
				if (colors.Length != colorPos.Length)
					throw new ArgumentException ("The number of colors must match the number of color positions.");
				return GetObject<SKShader> (SkiaApi.sk_shader_new_radial_gradient (ref center, radius, colors, colorPos, colors.Length, mode, IntPtr.Zero));
			}
		}
 /// <summary>
 /// Initializes a new instance of the <see cref="LinearGradientShader"/> class.
 /// </summary>
 /// <param name="start">The start.</param>
 /// <param name="end">The end.</param>
 /// <param name="colors">The colors.</param>
 /// <param name="colorPos">The color position.</param>
 /// <param name="mode">The mode.</param>
 public LinearGradientShader(SKPoint start, SKPoint end, SKColor[] colors, float[] colorPos, SKShaderTileMode mode)
 {
     _start    = start;
     _end      = end;
     _colors   = colors;
     _mode     = mode;
     _colorPos = colorPos;
 }