예제 #1
0
        public void Test_GetColor()
        {
            var lib = new AnsiColors();

            output.WriteLine($"Color with Red: {lib.GetColor(AnsiColor.Red, "test")}");
        }
예제 #2
0
        public void Test_GetStyle()
        {
            var lib = new AnsiColors();

            output.WriteLine($"Style with Bold: {lib.GetStyle(AnsiStyle.Bold, "test")}");
        }
예제 #3
0
        public static unsafe int RenderAnsi <TColor>(Stream o, IntPtr pixels,
                                                     uint w, uint h,
                                                     uint reduceLineCount = 0, int maxLineCount           = -1, int maxWidth = -1,
                                                     bool borderless      = false, AnsiColors colors      = AnsiColors.TrueColor,
                                                     IDither?customDither = null, float customDitherScale = 1f
                                                     ) where TColor : unmanaged, IPixel <TColor>
        {
            GetConsoleSize(out var cw, out var ch);

            if (maxWidth >= 0)
            {
                cw = maxWidth;
            }

            if (maxLineCount >= 0)
            {
                ch = maxLineCount;
            }

            cw -= 1;
            ch -= (int)reduceLineCount;

            if (cw == 0 || ch == 0)
            {
                return(0);
            }

            var aw = cw;
            var ah = ch * 2;

            var pPixels = (byte *)pixels;
            var span    = new ReadOnlySpan <TColor>(pPixels, checked ((int)(w * h)));

            using var img = Image.LoadPixelData(span, (int)w, (int)h);
            img.Mutate(x => x
                       .Resize(aw, ah, LanczosResampler.Lanczos3)
                       //.Crop(aw, ah)
                       );

            IndexedImageFrame <TColor>?indexedImg = null;
            var isTrueColor = colors == AnsiColors.TrueColor;

            if (!isTrueColor)
            {
                switch (colors)
                {
                case AnsiColors.Palette16: {
                    var opts = AnsiPalette16.Options;
                    if (customDither != null)
                    {
                        opts.Dither      = customDither;
                        opts.DitherScale = customDitherScale;
                    }

                    indexedImg = AnsiPalette16
                                 .CreatePixelSpecificQuantizer <TColor>(Configuration.Default, opts)
                                 .QuantizeFrame(img.Frames[0], new Rectangle(0, 0, img.Width, img.Height));
                    break;
                }

                case AnsiColors.Palette256: {
                    var opts = AnsiPalette256.Options;
                    if (customDither != null)
                    {
                        opts.Dither      = customDither;
                        opts.DitherScale = customDitherScale;
                    }

                    indexedImg = AnsiPalette256
                                 .CreatePixelSpecificQuantizer <TColor>(Configuration.Default, opts)
                                 .QuantizeFrame(img.Frames[0], new Rectangle(0, 0, img.Width, img.Height));
                    break;
                }
                }
            }

            void WriteNumberTriplet(byte b)
            {
                var ones        = b % 10;
                var tens        = b / 10 % 10;
                var hundreds    = b / 100;
                var anyHundreds = hundreds > 0;

                if (anyHundreds)
                {
                    o !.WriteByte((byte)('0' + hundreds));
                }
                if (anyHundreds || tens > 0)
                {
                    o !.WriteByte((byte)('0' + tens));
                }
                o !.WriteByte((byte)('0' + ones));
            }

            // ╭
            void DrawTopLeftCorner()
            {
                o.WriteByte(0xE2);
                o.WriteByte(0x95);
                o.WriteByte(0xAD);
            }

            // ╮
            void DrawTopRightCorner()
            {
                o.WriteByte(0xE2);
                o.WriteByte(0x95);
                o.WriteByte(0xAE);
            }

            // ╰
            void DrawBottomLeftCorner()
            {
                o.WriteByte(0xE2);
                o.WriteByte(0x95);
                o.WriteByte(0xB0);
            }

            // ╯
            void DrawBottomRightCorner()
            {
                o.WriteByte(0xE2);
                o.WriteByte(0x95);
                o.WriteByte(0xAF);
            }

            // ─ x width
            void DrawHorizontalFrame(int width)
            {
                for (var i = 0; i < width; ++i)
                {
                    o.WriteByte(0xE2);
                    o.WriteByte(0x94);
                    o.WriteByte(0x80);
                }
            }

            // │
            void DrawVerticalFrame()
            {
                o.WriteByte(0xE2);
                o.WriteByte(0x94);
                o.WriteByte(0x82);
            }

            if (!borderless)
            {
                DrawTopLeftCorner();
                DrawHorizontalFrame(aw + 1);
                DrawTopRightCorner();
                o.WriteByte((byte)'\n');
            }

            var lastY = ah & ~1;

            for (var y = 0; y < lastY; y += 2)
            {
                if (!borderless)
                {
                    DrawVerticalFrame();
                }
                // write 2 lines at a time
                var haveL = y + 1 < ah;

                var u = isTrueColor
          ? img.GetPixelRowSpan(y)
          : default;
                var l = haveL && isTrueColor
          ? img.GetPixelRowSpan(y + 1)
          : default;

                var up = !isTrueColor
          ? indexedImg !.GetPixelRowSpan(y)
          : default;