示例#1
0
        public static void ApplyFill(this SKPaint paint, Rect bounds, DrawingStyle style)
        {
            paint.Style       = SKPaintStyle.Fill;
            paint.IsAntialias = style.EdgeMode == EdgeMode.Unspecified;

            if (style.Fill is SolidColorBrush)
            {
                paint.Color = style.Fill.ToSKColor();
            }
            else
            {
                paint.Shader = style.Fill.ToSkiaShader(bounds.Width, bounds.Height);
            }

            if (style.HasOpacity)
            {
                paint.ColorFilter = SKColorFilter.CreateBlendMode(SKColors.Transparent.WithAlpha((byte)(style.Opacity * 255d)), SKBlendMode.DstIn);
            }

            if (style.Effect != null)
            {
                if (style.Effect is DropShadowEffect)
                {
                    var fx = style.Effect as DropShadowEffect;
                    paint.ImageFilter = SKImageFilter.CreateDropShadow(fx.ShadowDepth.ToFloat(), fx.ShadowDepth.ToFloat(), fx.BlurRadius.ToFloat(), fx.BlurRadius.ToFloat(), fx.Color.ToSKColor(), SKDropShadowImageFilterShadowMode.DrawShadowAndForeground);
                }
            }
        }
示例#2
0
        public static SKColorFilter GetContrastColorMatrix(int value)
        {
            value = Math.Clamp(value, -100, 100);

            float x;

            if (value < 0)
            {
                x = 127 + (float)value / 100 * 127;
            }
            else
            {
                x = value % 1;
                if (x == 0)
                {
                    x = (float)DELTA_INDEX[value];
                }
                else
                {
                    x = (float)DELTA_INDEX[value << 0] * (1 - x) + (float)DELTA_INDEX[(value << 0) + 1] * x;   // use linear interpolation for more granularity.
                }
                x = x * 127 + 127;
            }

            float[] mat =
            {
                x / 127,       0,       0, 0, 0.5f * (127 - x),
                0,       x / 127,       0, 0, 0.5f * (127 - x),
                0,             0, x / 127, 0, 0.5f * (127 - x),
                0,             0,       0, 1, 0
            };
            return(SKColorFilter.CreateColorMatrix(mat));
        }
示例#3
0
        /// <summary>
        /// Creates the image.
        /// </summary>
        /// <returns>The image.</returns>
        /// <param name="stream">Stream.</param>
        /// <param name="width">Width.</param>
        /// <param name="height">Height.</param>
        /// <param name="color">Color.</param>
        public static Task <Stream> CreateImage(Stream stream, double width, double height, Color color)
        {
            var screenScale = SvgImageSource.ScreenScale;

            var svg = new SkiaSharp.Extended.Svg.SKSvg();

            svg.Load(stream);

            var size   = CalcSize(svg.Picture.CullRect.Size, width, height);
            var scale  = CalcScale(svg.Picture.CullRect.Size, size, screenScale);
            var matrix = SKMatrix.MakeScale(scale.Item1, scale.Item2);

            using (var bitmap = new SKBitmap((int)(size.Width * screenScale), (int)(size.Height * screenScale)))
                using (var canvas = new SKCanvas(bitmap))
                    using (var paint = new SKPaint())
                    {
                        if (!color.IsDefault)
                        {
                            paint.ColorFilter = SKColorFilter.CreateBlendMode(ToSKColor(color), SKBlendMode.SrcIn);
                        }

                        canvas.Clear(SKColors.Transparent); // very very important!
                        canvas.DrawPicture(svg.Picture, ref matrix, paint);

                        using (var image = SKImage.FromBitmap(bitmap))
                            using (var encoded = image.Encode())
                            {
                                var imageStream = new MemoryStream();
                                encoded.SaveTo(imageStream);
                                imageStream.Position = 0;
                                return(Task.FromResult(imageStream as Stream));
                            }
                    }
        }
示例#4
0
        public SKPicture?RecordGraphic(Drawable?drawable, Attributes ignoreAttributes)
        {
            // TODO: Record using SKColorSpace.CreateSrgbLinear because .color-interpolation-filters. is by default linearRGB.
            if (drawable == null)
            {
                return(null);
            }

            if (drawable.TransformedBounds.Width <= 0f && drawable.TransformedBounds.Height <= 0f)
            {
                return(null);
            }

            using var skPictureRecorder = new SKPictureRecorder();
            using var skCanvas          = skPictureRecorder.BeginRecording(drawable.TransformedBounds);

#if USE_EXPERIMENTAL_LINEAR_RGB
            // TODO:
            using var skPaint       = new SKPaint();
            using var skColorFilter = SKColorFilter.CreateTable(null, SvgPaintingExtensions.s_SRGBtoLinearRGB, SvgPaintingExtensions.s_SRGBtoLinearRGB, SvgPaintingExtensions.s_SRGBtoLinearRGB);
            using var skImageFilter = SKImageFilter.CreateColorFilter(skColorFilter);
            skPaint.ImageFilter     = skImageFilter;
            skCanvas.SaveLayer(skPaint);
#endif

            drawable.Draw(skCanvas, ignoreAttributes, null);

#if USE_EXPERIMENTAL_LINEAR_RGB
            // TODO:
            skCanvas.Restore();
#endif

            return(skPictureRecorder.EndRecording());
        }
        public override void OnCanvasViewPaintSurface(SKPaintSurfaceEventArgs e)
        {
            SKImageInfo imageInfo = e.Info;
            SKSurface   surface   = e.Surface;
            SKCanvas    canvas    = surface.Canvas;

            if (isDead())
            {
                SKPoint rotatePoint = new SKPoint(rect.MidX, rect.MidY);

                canvas.RotateDegrees(deathAnimation, rotatePoint.X, rotatePoint.Y);
                canvas.DrawBitmap(getBitmap(), getRectangle());
                canvas.RotateDegrees(-deathAnimation, rotatePoint.X, rotatePoint.Y);

                deathAnimation += 12;
                ySpeed         += 0.2;
            }
            else
            {
                if (isAggro)
                {
                    SKPaint paint = new SKPaint();
                    paint.ColorFilter = SKColorFilter.CreateBlendMode(SKColor.FromHsl(356, 51, 43).WithAlpha(255), SKBlendMode.Modulate);

                    canvas.DrawBitmap(getBitmap(), getRectangle(), paint);
                }
                else
                {
                    canvas.DrawBitmap(getBitmap(), getRectangle());
                }
            }
        }
        //https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/effects/color-filters
        public static SKBitmap MakeGrayscale3(SKBitmap original)
        {
            // TODO TEST

            //create a blank bitmap the same size as original
            SKBitmap newBitmap = new SKBitmap(original.Width, original.Height);

            //get a graphics object from the new image
            using (SKCanvas g = new SKCanvas(newBitmap))
            {
                using (SKPaint paint = new SKPaint())
                {
                    paint.ColorFilter =
                        SKColorFilter.CreateColorMatrix(new float[]
                    {
                        0.21f, 0.72f, 0.07f, 0, 0,
                        0.21f, 0.72f, 0.07f, 0, 0,
                        0.21f, 0.72f, 0.07f, 0, 0,
                        0, 0, 0, 1, 0
                    });

                    g.DrawBitmap(newBitmap, new SKPoint(0, 0), paint);
                }
            }
            return(newBitmap);
        }
示例#7
0
        private void SKElement_OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            SKCanvas canvas = e.Surface.Canvas;

            canvas.Clear();

            if (Svg == null)
            {
                return;
            }

            SKImageInfo info = e.Info;

            canvas.Translate(info.Width / 2f, info.Height / 2f);

            SKRect bounds = Svg.ViewBox;
            float  xRatio = info.Width / bounds.Width;
            float  yRatio = info.Height / bounds.Height;

            float ratio = Math.Min(xRatio, yRatio);

            canvas.Scale(ratio);
            canvas.Translate(-bounds.MidX, -bounds.MidY);

            using (var paint = new SKPaint())
            {
                paint.ColorFilter = SKColorFilter.CreateBlendMode(Colors.Red.ToSKColor(), SKBlendMode.SrcIn);
                canvas.DrawPicture(Svg.Picture, paint);
            }
        }
示例#8
0
        private new void DrawBackground(SKCanvas c)
        {
            c.DrawRect(new SKRect(Margin, Margin, Width - Margin, Height - Margin),
                       new SKPaint
            {
                IsAntialias = true, FilterQuality = SKFilterQuality.High,
                Shader      = SKShader.CreateRadialGradient(new SKPoint(Width / 2, Height / 2), Width / 5 * 2,
                                                            new[] { Background[0], Background[1] },
                                                            SKShaderTileMode.Clamp)
            });

            for (var i = 0; i < _backgroundOverlay.Width; i++)
            {
                for (var j = 0; j < _backgroundOverlay.Height; j++)
                {
                    if (_backgroundOverlay.GetPixel(i, j) == SKColors.Black)
                    {
                        _backgroundOverlay.SetPixel(i, j, SKColors.Transparent);
                    }
                }
            }

            c.DrawBitmap(_backgroundOverlay, new SKRect(Margin, Margin, Width - Margin, Height - Margin), new SKPaint
            {
                IsAntialias = true,
                ColorFilter = SKColorFilter.CreateBlendMode(SKColors.Black.WithAlpha(150), SKBlendMode.DstIn),
                ImageFilter = SKImageFilter.CreateDropShadow(2, 2, 4, 4, new SKColor(0, 0, 0))
            });
            c.DrawColor(SKColors.Black.WithAlpha(125), SKBlendMode.DstIn);
        }
示例#9
0
        public static SKPicture?ToPicture(SvgFragment svgFragment)
        {
            var skSize   = SvgExtensions.GetDimensions(svgFragment);
            var skBounds = SKRect.Create(skSize);

            using var drawable = DrawableFactory.Create(svgFragment, skBounds, null, null, Attributes.None);
            if (drawable == null)
            {
                return(null);
            }

            drawable.PostProcess();

            if (skBounds.IsEmpty)
            {
                skBounds = GetBounds(drawable);
            }

            using var skPictureRecorder = new SKPictureRecorder();
            using var skCanvas          = skPictureRecorder.BeginRecording(skBounds);
#if USE_EXPERIMENTAL_LINEAR_RGB
            // TODO:
            using var skPaint       = new SKPaint();
            using var skColorFilter = SKColorFilter.CreateTable(null, SvgPaintingExtensions.s_LinearRGBtoSRGB, SvgPaintingExtensions.s_LinearRGBtoSRGB, SvgPaintingExtensions.s_LinearRGBtoSRGB);
            using var skImageFilter = SKImageFilter.CreateColorFilter(skColorFilter);
            skPaint.ImageFilter     = skImageFilter;
            skCanvas.SaveLayer(skPaint);
#endif
            drawable?.Draw(skCanvas, 0f, 0f);
#if USE_EXPERIMENTAL_LINEAR_RGB
            // TODO:
            skCanvas.Restore();
#endif
            return(skPictureRecorder.EndRecording());
        }
示例#10
0
        void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            SKImageInfo info   = e.Info;
            SKCanvas    canvas = e.Surface.Canvas;

            canvas.Clear();

            float startAngle = -90;
            float sweepAngle = 360 * (float)(PercentageComplete * .01);

            var arcPaint = new SKPaint();

            arcPaint.IsStroke    = true;
            arcPaint.StrokeCap   = SKStrokeCap.Round;
            arcPaint.StrokeWidth = 8;

            var padding = 10;
            var paint   = new SKPaint();

            paint.IsStroke    = true;
            paint.StrokeWidth = 3;
            paint.IsAntialias = true;
            paint.ColorFilter = SKColorFilter.CreateBlendMode(Color.FromHex("#7FFF").ToSKColor(), SKBlendMode.SrcIn);
            canvas.DrawCircle(info.Width / 2, info.Height / 2, info.Height / 2 - padding, paint);

            using (SKPath path = new SKPath())
            {
                var rect = new SKRect(padding, padding, info.Width - padding, info.Height - padding);
                arcPaint.ColorFilter = SKColorFilter.CreateBlendMode(Color.ToSKColor(), SKBlendMode.SrcIn);
                arcPaint.IsAntialias = true;
                path.AddArc(rect, startAngle, sweepAngle);
                canvas.DrawPath(path, arcPaint);
            }
        }
示例#11
0
        public static void Draw(SKCanvas canvas, SKSvg svg, float x, float y, float orientation = 0,
                                float offsetX = 0, float offsetY = 0, float opacity = 1f, float scale = 1f)
        {
            if (svg.Picture == null)
            {
                return;
            }

            canvas.Save();

            canvas.Translate(x, y);
            canvas.RotateDegrees(orientation, 0, 0); // todo: degrees or radians?
            canvas.Scale(scale, scale);

            var halfWidth  = svg.Picture.CullRect.Width / 2;
            var halfHeight = svg.Picture.CullRect.Height / 2;

            // 0/0 are assumed at center of image, but Svg has 0/0 at left top position
            canvas.Translate(-halfWidth + offsetX, -halfHeight - offsetY);

            var alpha        = Convert.ToByte(255 * opacity);
            var transparency = SKColors.White.WithAlpha(alpha);

            using (var cf = SKColorFilter.CreateBlendMode(transparency, SKBlendMode.DstIn))
            {
                using var skPaint = new SKPaint
                      {
                          IsAntialias = true,
                          ColorFilter = cf,
                      };
                canvas.DrawPicture(svg.Picture, skPaint);
            }

            canvas.Restore();
        }
        public SVGLayer(SKSvg pImage, string pName, bool pActive = true)
        {
            _Changed       = new VariableMonitor <bool>();
            _RenderChanged = new VariableMonitor <bool>();

            //Open the defined image

            mActive = pActive;
            mImage  = pImage;
            mName   = pName;

            //
            var transparency = Color.FromRgba(0, 0, 0, 0).ToSKColor();

            mDrawPaint             = new SKPaint();
            mDrawPaint.Color       = SKColors.Red;
            mDrawPaint.IsAntialias = true;
            mDrawPaint.BlendMode   = SKBlendMode.SrcOver;
            mDrawPaint.ColorFilter = SKColorFilter.CreateBlendMode(transparency, SKBlendMode.DstOver);

            mUndrawPaint             = new SKPaint();
            mUndrawPaint.BlendMode   = SKBlendMode.DstOut;
            mUndrawPaint.ColorFilter = SKColorFilter.CreateBlendMode(transparency, SKBlendMode.DstOver);

            Off();
        }
        public PathLayer(Polycurve pImage, string pName, bool pActive = true)
        {
            _Changed       = new VariableMonitor <bool>();
            _RenderChanged = new VariableMonitor <bool>();

            //Open the defined image
            mActive = pActive;
            mImage  = pImage;
            mName   = pName;

            //
            var transparency = Color.FromRgba(0, 0, 0, 0).ToSKColor();

            mDrawPaint             = new SKPaint();
            mDrawPaint.BlendMode   = SKBlendMode.Src;
            mDrawPaint.Color       = Globals.TextColor.ToSKColor();
            mDrawPaint.ColorFilter = SKColorFilter.CreateBlendMode(transparency, SKBlendMode.Dst);

            mUndrawPaint             = new SKPaint();
            mUndrawPaint.BlendMode   = SKBlendMode.Src;
            mUndrawPaint.Color       = Globals.BackgroundColor.ToSKColor();
            mUndrawPaint.ColorFilter = SKColorFilter.CreateBlendMode(transparency, SKBlendMode.Dst);

            Off();
        }
示例#14
0
        public async ValueTask <byte[]> GetBytesWithFilterAsync(
            int size,
            string topic = null,
            SKImageFilter imageFilter = null,
            SKColorFilter colorFilter = null,
            SKShader shader           = null,
            SKBlendMode blendMode     = SKBlendMode.Overlay)
        {
            Trace.WriteLine("Start");
            string url;

            if (string.IsNullOrEmpty(topic))
            {
                url = string.Format(URL_PATTERN_RND, size);
            }
            else
            {
                url = string.Format(URL_PATTERN, size, topic);
            }

            var imageInfo = new SKImageInfo(size, size);

            using (var http = new HttpClient())
            {
                var image = await http.GetByteArrayAsync(url);//.ConfigureAwait(false);

                Trace.WriteLine("Downloaded");
                using (var outStream = new MemoryStream())
                    using (var bitmap = SKBitmap.Decode(image, imageInfo))
                        using (var surface = SKSurface.Create(imageInfo))
                            using (var paint = new SKPaint())
                            {
                                SKCanvas canvas = surface.Canvas;
                                canvas.DrawColor(SKColors.White);
                                if (imageFilter != null)
                                {
                                    paint.ImageFilter = imageFilter;
                                }
                                if (colorFilter != null)
                                {
                                    paint.ColorFilter = colorFilter;
                                }

                                // draw the bitmap through the filter
                                var rect = SKRect.Create(imageInfo.Size);
                                canvas.DrawBitmap(bitmap, rect, paint);
                                if (shader != null)
                                {
                                    paint.Shader    = shader;
                                    paint.BlendMode = blendMode;
                                    canvas.DrawPaint(paint);
                                }
                                SKData data = surface.Snapshot().Encode(SKEncodedImageFormat.Jpeg, 80);
                                data.SaveTo(outStream);
                                byte[] manipedImage = outStream.ToArray();
                                Trace.WriteLine("End");
                                return(manipedImage);
                            }
            }
        }
示例#15
0
        public static SKImage ApplyMatrix(SKImage Source, float[] mat)
        {
            SKRectI irect  = new SKRectI((int)0, (int)0, Source.Width, Source.Height);
            var     result = Source.ApplyImageFilter(SKImageFilter.CreateColorFilter(SKColorFilter.CreateColorMatrix(mat)), irect, irect, out SKRectI active, out SKPoint activeclip);

            return(result);
        }
示例#16
0
        private static SKBitmap FilterToGrayscale(SKBitmap originalBitmap)
        {
            var grayed = new SKBitmap(originalBitmap.Width, originalBitmap.Height);

            using (var graybrush = new SKPaint())
            {
                graybrush.ColorFilter =
                    SKColorFilter.CreateColorMatrix(new[]
                {
                    0.21f, 0.72f, 0.07f, 0.0f, 0.0f,
                    0.21f, 0.72f, 0.07f, 0.0f, 0.0f,
                    0.21f, 0.72f, 0.07f, 0.0f, 0.0f,
                    0.0f, 0.0f, 0.0f, 1.0f, 0.0f
                });
                using (var tempCanvas = new SKCanvas(grayed))
                {
                    tempCanvas.DrawBitmap(
                        originalBitmap,
                        0,
                        0,
                        graybrush);
                }
            }

            return(grayed);
        }
        public static SKPaint GridPaint(float scale)
        {
            var temp = ScaledPaint(scale, _GridPaint);

            temp.ColorFilter = SKColorFilter.CreateBlendMode(temp.Color, SKBlendMode.Dst);
            temp.PathEffect  = SKPathEffect.CreateDash(new[] { 2 * scale, 2 * scale }, 0);
            return(temp);
        }
示例#18
0
文件: SvgImage.cs 项目: llenroc/Hunt
        void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            if (string.IsNullOrEmpty(Source))
            {
                return;
            }

            if (Clicked == null && !AddPadding)
            {
                _padding = 0;
            }

            try
            {
                if (_fileContent == null)
                {
                    _fileContent = Source.GetFileContents();
                }

                if (Clicked == null)
                {
                    _padding = 0;
                }

                var svg    = new SkiaSharp.Extended.Svg.SKSvg();
                var bytes  = System.Text.Encoding.UTF8.GetBytes(_fileContent);
                var stream = new MemoryStream(bytes);

                svg.Load(stream);
                var canvas = e.Surface.Canvas;
                using (var paint = new SKPaint())
                {
                    if (Color != Color.Lime)
                    {
                        //Set the paint color
                        paint.ColorFilter = SKColorFilter.CreateBlendMode(Color.ToSKColor(), SKBlendMode.SrcIn);
                    }

                    int multiplier = (int)(e.Info.Width / WidthRequest);

                    //Scale up the SVG image to fill the canvas
                    float canvasMin = Math.Min(e.Info.Width - _padding * 2 * multiplier, e.Info.Height - _padding * 2 * multiplier);
                    float svgMax    = Math.Max(svg.Picture.CullRect.Width, svg.Picture.CullRect.Height);
                    float scale     = canvasMin / svgMax;
                    var   matrix    = SKMatrix.MakeScale(scale, scale);
                    matrix.TransX = _padding * multiplier;
                    matrix.TransY = _padding * multiplier;

                    canvas.Clear(Color.Transparent.ToSKColor());
                    canvas.DrawPicture(svg.Picture, ref matrix, paint);
                }
            }
            catch (Exception ex)
            {
                Log.Instance.WriteLine($"Error drawing SvgImage w/ ImagePath {Source}: {ex}");
            }
        }
示例#19
0
        public void Render(SKImage [] images, ColorLinesNG skiaView, SKCanvas canvas)
        {
            var    destRect = CLReEntity.VirtualToSkiaCoords(this.Verticies, skiaView);
            SKRect?srcRect  = null;

//			if (this.textureCoords != null)
//				srcRect = CLReEntity.VirtualToSkiaCoords(this.Verticies, skiaView, this.textureCoords);

            if (this.Angle != 0.0f)
            {
                canvas.Save();
                canvas.RotateDegrees(this.Angle, destRect.MidX, destRect.MidY);
            }
            if (this.TextureId >= 0)
            {
                if (images[this.TextureId] == null)
                {
                    if (!loadingTextures.Contains(this.TextureId))
                    {
                        loadingTextures.Add(this.TextureId);
                        Task.Run(async() => {
                            images[this.TextureId] = await skiaView.LoadTexture(this.TextureId);
//							loadingTextures.Remove(this.TextureId);
                        });
                    }
                    if (this.Angle != 0.0f)
                    {
                        canvas.Restore();
                    }
                    return;
                }
                if (this.Grayscale)
                {
                    CLReEntity.CanvasDrawImage(canvas, images[this.TextureId], destRect, srcRect, paintGrayscale);
                }
                else if (this.Fill != null)
                {
                    using (texturePaint.ColorFilter = SKColorFilter.CreateBlendMode(CLReEntity.ColorToSKColor((Color)this.Fill), SKBlendMode.Modulate)) {
                        CLReEntity.CanvasDrawImage(canvas, images[this.TextureId], destRect, srcRect, texturePaint);
                    }
                }
                else
                {
                    CLReEntity.CanvasDrawImage(canvas, images[this.TextureId], destRect);
                }
            }
            else
            {
                rectPaint.Color = CLReEntity.ColorToSKColor((Color)this.Fill);
                canvas.DrawRect(destRect, rectPaint);
            }
            if (this.Angle != 0.0f)
            {
                canvas.Restore();
            }
        }
示例#20
0
 public override void PostProcess(SKCanvas canvas, SKRect bounds, SKPaint paint)
 {
     paint.ImageFilter = SKImageFilter.CreateColorFilter(SKColorFilter.CreateColorMatrix(new[]
     {
         0.21f, 0.72f, 0.07f, 0, 0,
         0.21f, 0.72f, 0.07f, 0, 0,
         0.21f, 0.72f, 0.07f, 0, 0,
         0, 0, 0, 1, 0
     }), paint.ImageFilter);
 }
        public override void Paint(PaintContext context)
        {
            var     color_filter = SKColorFilter.CreateBlendMode(color_, blend_mode_);
            SKPaint paint        = new SKPaint();

            paint.ColorFilter = color_filter;

            Layer.AutoSaveLayer save = Layer.AutoSaveLayer.Create(context, paint_bounds(), paint);
            PaintChildren(context);
        }
示例#22
0
        // Convert bitmap to grayscale and apply contrast to emphasize lines
        // For conversion SKColorFilter is used
        // However, SKColorFilter is usually set in SKPaint object and applied to canvas when drawn
        // To apply it to the bitmap, we have to convert bitmap to image because it's possible to apply filters to images
        SKBitmap ConvertBitmapToGray(SKBitmap bitmap, float contr = contrast)
        {
            SKImage       image       = SKImage.FromBitmap(bitmap);
            SKImageFilter imagefilter = SKImageFilter.CreateColorFilter(SKColorFilter.CreateHighContrast(true, SKHighContrastConfigInvertStyle.NoInvert, contrast));
            SKRectI       rectout     = new SKRectI();
            SKPoint       pointout    = new SKPoint();

            image = image.ApplyImageFilter(imagefilter, new SKRectI(0, 0, image.Width, image.Height), new SKRectI(0, 0, image.Width, image.Height), out rectout, out pointout);

            return(SKBitmap.FromImage(image));
        }
示例#23
0
        public void Draw(SKCanvas canvas, float x, float y, byte opacity)
        {
            if (!Visible)
            {
                return;
            }

            using var cf      = SKColorFilter.CreateBlendMode(SKColors.White.WithAlpha(opacity), SKBlendMode.DstIn);
            paint.ColorFilter = cf;
            Draw(canvas, x, y, paint);
            paint.ColorFilter = null;
        }
示例#24
0
 private static SKPaint ColoredFilter(SKColor Base, float r, float g, float b)
 {
     return(new SKPaint()
     {
         ColorFilter = SKColorFilter.CreateColorMatrix(new float[]
         { //84,47,78
             Base.Red / r, 0f, 0f, 0, 0,
             0f, Base.Green / g, 0f, 0, 0,
             0f, 0f, Base.Blue / b, 0, 0,
             0, 0, 0, 1, 0
         })
     });
 }
        //C++ TO C# CONVERTER WARNING: 'const' methods are not available in C#:
        //ORIGINAL LINE: void Paint(PaintContext& context) const override
        public override void Paint(PaintContext context)
        {
            //TRACE_EVENT0("flutter", "ColorFilterLayer::Paint");
            //FML_DCHECK(needs_painting());

            var     color_filter = SKColorFilter.CreateBlendMode(color_, blend_mode_);
            SKPaint paint        = new SKPaint();

            paint.ColorFilter = color_filter;

            Layer.AutoSaveLayer save = Layer.AutoSaveLayer.Create(context, paint_bounds(), paint);
            PaintChildren(context);
        }
示例#26
0
        private void SetGrayScale(SKSurface surface, SKBitmap bitmap)
        {
            // do not grayscale too much so that image can be split easily
            using (var cf = SKColorFilter.CreateHighContrast(true, SKHighContrastConfigInvertStyle.NoInvert, 0.5f))
                using (var paint = new SKPaint())
                {
                    paint.ColorFilter = cf;

                    var canvas = surface.Canvas;
                    canvas.Clear(SKColors.White);
                    canvas.DrawBitmap(bitmap, SKRect.Create(bitmap.Width, bitmap.Height), paint);
                }
        }
示例#27
0
        public async ValueTask <IActionResult> GetGrayscaleAsync(int size, string topic = null, float contrast = 0.06F)
        {
            int halfSize = size / 2;
            var rotate   = SKMatrix.MakeRotationDegrees(30, halfSize, halfSize);
            var scale    = SKMatrix.MakeScale(1.2F, 1.2F, halfSize, halfSize);

            using (var filter = SKColorFilter.CreateHighContrast(
                       true, SKHighContrastConfigInvertStyle.NoInvert, contrast))
            {
                var response = await GetWithFilterAsync(size, topic, colorFilter : filter);

                return(response);
            }
        }
示例#28
0
文件: SvgIcon.cs 项目: Nootus/Fabric
 private static SKPaint GetPaint(Color color)
 {
     if (!color.IsDefault)
     {
         return(new SKPaint()
         {
             ColorFilter = SKColorFilter.CreateBlendMode(color.ToSKColor(), SKBlendMode.SrcIn)
         });
     }
     else
     {
         return(null);
     }
 }
示例#29
0
        public static ProcessedImage GetImage(string imagePath)
        {
            using (var input = File.OpenRead(imagePath))
            {
                using (var image = SKBitmap.Decode(input))
                {
                    // RESIZE IMAGE
                    int width, height;
                    if (image.Width > image.Height)
                    {
                        width  = SIZE;
                        height = image.Height * SIZE / image.Width;
                    }
                    else
                    {
                        width  = image.Width * SIZE / image.Height;
                        height = SIZE;
                    }
                    var resizedImage = image.Resize(new SKImageInfo(width, height), SKFilterQuality.Medium);

                    // TRANSFORM TO GRAYSCALE
                    var surface = SKSurface.Create(resizedImage.Info);
                    var canvas  = surface.Canvas;
                    using (SKPaint paint = new SKPaint())
                    {
                        paint.ColorFilter =
                            SKColorFilter.CreateColorMatrix(new float[]
                        {
                            0.21f, 0.72f, 0.07f, 0, 0,
                            0.21f, 0.72f, 0.07f, 0, 0,
                            0.21f, 0.72f, 0.07f, 0, 0,
                            0, 0, 0, 1, 0
                        });

                        canvas.DrawBitmap(resizedImage, resizedImage.Info.Rect, paint);
                    }
                    var grayScaleImage = SKBitmap.Decode(surface.Snapshot().Encode(SKEncodedImageFormat.Jpeg, QUALITY)).Pixels;

                    // SET GRAYSCALE ARRAY
                    int[] grayScalePixels = new int[grayScaleImage.Length];
                    var   limit           = grayScaleImage.Length;
                    for (int i = 0; i < limit; i++)
                    {
                        var pixel = grayScaleImage[i];
                        grayScalePixels[i] = pixel.Red;
                    }
                    return(new ProcessedImage(grayScalePixels, resizedImage.Width, resizedImage.Height, Path.GetFileNameWithoutExtension(imagePath)));
                }
            }
        }
示例#30
0
        public static SKColorFilter GetBrightnessColorMatrix(float value)
        {
            value = 2.55f * value;
            value = Math.Clamp(value, -255, 255);

            float[] mat =
            {
                1, 0, 0, 0, value,
                0, 1, 0, 0, value,
                0, 0, 1, 0, value,
                0, 0, 0, 1, 0
            };
            return(SKColorFilter.CreateColorMatrix(mat));
        }