コード例 #1
0
ファイル: SKTest.cs プロジェクト: silvrwolfboy/SkiaSharp
        protected static void ValidateTestPixmap(SKPixmap pix, byte alpha = 255)
        {
            Assert.NotNull(pix);
            Assert.Equal(40, pix.Width);
            Assert.Equal(40, pix.Height);

            Assert.Equal(SKColors.Red.WithAlpha(alpha), pix.GetPixelColor(10, 10));
            Assert.Equal(SKColors.Green.WithAlpha(alpha), pix.GetPixelColor(30, 10));
            Assert.Equal(SKColors.Blue.WithAlpha(alpha), pix.GetPixelColor(10, 30));
            Assert.Equal(SKColors.Yellow.WithAlpha(alpha), pix.GetPixelColor(30, 30));
        }
コード例 #2
0
        public string Print(SKPixmap b)
        {
            var colorMappings = new Dictionary <uint, char>
            {
                [0] = '.',
                [background.ToUint32()] = '#'
            };

            var sb = new StringBuilder();

            for (var y = 0; y < b.Height; y += 1)
            {
                for (var x = 0; x < b.Width; x += 1)
                {
                    var c = (uint)b.GetPixelColor(x, y);
                    if (colorMappings.TryGetValue(c, out var p))
                    {
                        sb.Append(p);
                    }
                    else
                    {
                        var px = (char)('a' + colorMappings.Count);
                        colorMappings[c] = px;
                        sb.Append(px);
                    }
                }

                sb.Append("\n");
            }

            return(sb.ToString());
        }
コード例 #3
0
        private void TakeSamples(IEnumerable <BrushRenderTarget> renderTargets)
        {
            int sampleSize  = _sampleSizeSetting.Value;
            int sampleDepth = Math.Sqrt(sampleSize).RoundToInt();

            int bitmapWidth  = Bitmap.Width;
            int bitmapHeight = Bitmap.Height;

            using SKPixmap pixmap = Bitmap.PeekPixels();
            foreach (BrushRenderTarget renderTarget in renderTargets)
            {
                // SKRect has all the good stuff we need
                int left   = (int)((renderTarget.Rectangle.Location.X + 4) * Scale.Horizontal);
                int top    = (int)((renderTarget.Rectangle.Location.Y + 4) * Scale.Vertical);
                int width  = (int)((renderTarget.Rectangle.Size.Width - 8) * Scale.Horizontal);
                int height = (int)((renderTarget.Rectangle.Size.Height - 8) * Scale.Vertical);

                int verticalSteps   = height / (sampleDepth - 1);
                int horizontalSteps = width / (sampleDepth - 1);

                int a = 0, r = 0, g = 0, b = 0;
                for (int horizontalStep = 0; horizontalStep < sampleDepth; horizontalStep++)
                {
                    for (int verticalStep = 0; verticalStep < sampleDepth; verticalStep++)
                    {
                        int x = left + horizontalSteps * horizontalStep;
                        int y = top + verticalSteps * verticalStep;
                        if (x < 0 || x >= bitmapWidth || y < 0 || y >= bitmapHeight)
                        {
                            continue;
                        }

                        SKColor color = pixmap.GetPixelColor(x, y);
                        a += color.Alpha;
                        r += color.Red;
                        g += color.Green;
                        b += color.Blue;

                        // Uncomment to view the sample pixels in the debugger, need a checkbox in the actual debugger but this was a quickie
                        // Bitmap.SetPixel(x, y, new SKColor(0, 255, 0));
                    }
                }

                RenderedTargets[renderTarget] = new Color(a / sampleSize, r / sampleSize, g / sampleSize, b / sampleSize);
            }
        }
コード例 #4
0
        public override SKColor GetColor(ArtemisLed led, SKPoint renderPoint)
        {
            const int sampleSize  = 9;
            const int sampleDepth = 3;

            var renderBounds = Layer.Bounds;
            var widthScale   = pixmap.Width / renderBounds.Width;
            var heightScale  = pixmap.Height / renderBounds.Height;

            lock (pixmapLock)
            {
                int x      = (int)(renderPoint.X * widthScale);
                int y      = (int)(renderPoint.Y * heightScale);
                int width  = (int)(led.Rectangle.Width * widthScale);
                int height = (int)(led.Rectangle.Height * heightScale);

                int verticalSteps   = height / (sampleDepth - 1);
                int horizontalSteps = width / (sampleDepth - 1);

                int a = 0, r = 0, g = 0, b = 0;
                for (int horizontalStep = 0; horizontalStep < sampleDepth; horizontalStep++)
                {
                    for (int verticalStep = 0; verticalStep < sampleDepth; verticalStep++)
                    {
                        var     bruhX = x + horizontalSteps * horizontalStep;
                        var     bruhY = y + verticalSteps * verticalStep;
                        SKColor color = pixmap.GetPixelColor(bruhX, bruhY);
                        r += color.Red;
                        g += color.Green;
                        b += color.Blue;
                        a += color.Alpha;
                    }
                }
                return(new SKColor((byte)(r / sampleSize), (byte)(g / sampleSize), (byte)(b / sampleSize)));
            }
        }