Exemple #1
0
        /// <inheritdoc />
        public void SetPixel(Point position, Color color)
        {
            if (position.X < 0 || position.Y < 0 || position.X >= _texture.Size.X || position.Y >= _texture.Size.Y)
            {
                throw new IndexOutOfRangeException("Pixel position is out of range.");
            }

            _texture.Update(new[] { color.R, color.G, color.B, color.A }, 1, 1, (uint)position.X, (uint)position.Y);
        }
Exemple #2
0
        /// <inheritdoc />
        public Color[] GetPixels()
        {
            var colors = new Color[(int)_texture.Size.X * (int)_texture.Size.Y];

            using Image image = _texture.CopyToImage();
            byte[] pixels = image.Pixels;

            int colorIndex = 0;

            for (int i = 0; i < pixels.Length; i += 4)
            {
                colors[colorIndex++] = new Color(pixels[i], pixels[i + 1], pixels[i + 2], pixels[i + 3]);
            }

            return(colors);
        }
Exemple #3
0
        private static void Draw(Console console, SadRogue.Primitives.Color foreground, int glyphId)
        {
            var coloredString = new ColoredString(new ColoredGlyph(foreground, Color.Black, glyphId));

            console.Cursor.Print(coloredString);
        }
Exemple #4
0
 /// <inheritdoc />
 public void SetPixel(int index, Color color) =>
 SetPixel(Point.FromIndex(index, (int)_texture.Size.X), color);
Exemple #5
0
        /// <inheritdoc />
        public ICellSurface ToSurface(TextureConvertMode mode, int surfaceWidth, int surfaceHeight, TextureConvertBackgroundStyle backgroundStyle = TextureConvertBackgroundStyle.Pixel, TextureConvertForegroundStyle foregroundStyle = TextureConvertForegroundStyle.Block, Color[] cachedColorArray = null, ICellSurface cachedSurface = null)
        {
            if (surfaceWidth <= 0 || surfaceHeight <= 0 || surfaceWidth > _texture.Size.X || surfaceHeight > _texture.Size.Y)
            {
                throw new ArgumentOutOfRangeException("The size of the surface must be equal to or smaller than the texture.");
            }

            ICellSurface surface = cachedSurface ?? new CellSurface(surfaceWidth, surfaceHeight);

            // Background mode with simple resizing.
            if (mode == TextureConvertMode.Background && backgroundStyle == TextureConvertBackgroundStyle.Pixel)
            {
                using var resizer = GetResizedTexture(surface.Width, surface.Height);

                var colors = new Color[(int)resizer.Size.X * (int)resizer.Size.Y];
                using Image image = resizer.Texture.CopyToImage();
                byte[] imagePixels = image.Pixels;

                int colorIndex = 0;
                for (int i = 0; i < imagePixels.Length; i += 4)
                {
                    surface[colorIndex++].Background = new Color(imagePixels[i], imagePixels[i + 1], imagePixels[i + 2], imagePixels[i + 3]);
                }

                return(surface);
            }

            // Calculating color based on surrounding pixels
            Color[] pixels = GetPixels();

            int fontSizeX = (int)_texture.Size.X / surfaceWidth;
            int fontSizeY = (int)_texture.Size.Y / surfaceHeight;

            global::System.Threading.Tasks.Parallel.For(0, (int)_texture.Size.Y / fontSizeY, (h) =>
                                                        //for (int h = 0; h < imageHeight / fontSizeY; h++)
            {
                int startY = h * fontSizeY;
                //System.Threading.Tasks.Parallel.For(0, imageWidth / fontSizeX, (w) =>
                for (int w = 0; w < _texture.Size.X / fontSizeX; w++)
                {
                    int startX = w * fontSizeX;

                    float allR = 0;
                    float allG = 0;
                    float allB = 0;

                    for (int y = 0; y < fontSizeY; y++)
                    {
                        for (int x = 0; x < fontSizeX; x++)
                        {
                            int cY = y + startY;
                            int cX = x + startX;

                            Color color = pixels[cY * _texture.Size.X + cX];

                            allR += color.R;
                            allG += color.G;
                            allB += color.B;
                        }
                    }

                    byte sr = (byte)(allR / (fontSizeX * fontSizeY));
                    byte sg = (byte)(allG / (fontSizeX * fontSizeY));
                    byte sb = (byte)(allB / (fontSizeX * fontSizeY));

                    var newColor = new SadRogue.Primitives.Color(sr, sg, sb);

                    if (mode == TextureConvertMode.Background)
                    {
                        surface.SetBackground(w, h, newColor);
                    }

                    else if (foregroundStyle == TextureConvertForegroundStyle.Block)
                    {
                        float sbri = newColor.GetBrightness() * 255;

                        if (sbri > 204)
                        {
                            surface.SetGlyph(w, h, 219, newColor); //█
                        }
                        else if (sbri > 152)
                        {
                            surface.SetGlyph(w, h, 178, newColor); //▓
                        }
                        else if (sbri > 100)
                        {
                            surface.SetGlyph(w, h, 177, newColor); //▒
                        }
                        else if (sbri > 48)
                        {
                            surface.SetGlyph(w, h, 176, newColor); //░
                        }
                    }
                    else //else if (foregroundStyle == TextureConvertForegroundStyle.AsciiSymbol)
                    {
                        float sbri = newColor.GetBrightness() * 255;

                        if (sbri > 230)
                        {
                            surface.SetGlyph(w, h, '#', newColor);
                        }
                        else if (sbri > 207)
                        {
                            surface.SetGlyph(w, h, '&', newColor);
                        }
                        else if (sbri > 184)
                        {
                            surface.SetGlyph(w, h, '$', newColor);
                        }
                        else if (sbri > 161)
                        {
                            surface.SetGlyph(w, h, 'X', newColor);
                        }
                        else if (sbri > 138)
                        {
                            surface.SetGlyph(w, h, 'x', newColor);
                        }
                        else if (sbri > 115)
                        {
                            surface.SetGlyph(w, h, '=', newColor);
                        }
                        else if (sbri > 92)
                        {
                            surface.SetGlyph(w, h, '+', newColor);
                        }
                        else if (sbri > 69)
                        {
                            surface.SetGlyph(w, h, ';', newColor);
                        }
                        else if (sbri > 46)
                        {
                            surface.SetGlyph(w, h, ':', newColor);
                        }
                        else if (sbri > 23)
                        {
                            surface.SetGlyph(w, h, '.', newColor);
                        }
                    }
                }
            }
                                                        );

            return(surface);
        }