Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Don't forget background colors #18

Open
JohnnyErnest opened this issue Jun 3, 2020 · 1 comment
Open

[feature] Don't forget background colors #18

JohnnyErnest opened this issue Jun 3, 2020 · 1 comment

Comments

@JohnnyErnest
Copy link

Use esc[48;2;R;G;Bm to set background colors, maybe the object should be Output.Background.FromRGB(r,g,b)?

Observe the following:

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

namespace ConsoleColor
{
    public class App
    {
        private const int STD_OUTPUT_HANDLE = -11;
        private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
        private const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008;

        [DllImport("kernel32.dll")]
        private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);

        [DllImport("kernel32.dll")]
        private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern IntPtr GetStdHandle(int nStdHandle);

        [DllImport("kernel32.dll")]
        public static extern uint GetLastError();

        //int[] colorMeta1 = { 0, 85, 170, 255 };
        int[] colorMeta2 = { 0, 43, 85, 127, 170, 213, 255 };

        public class Point
        {
            public int X { get; set; }
            public int Y { get; set; }
        }

        public Point SaveCursor()
        {
            return new Point()
            {
                X = Console.CursorLeft,
                Y = Console.CursorTop
            };
        }

        public void SetCursor(Point point)
        {
            Console.CursorTop = point.Y;
            Console.CursorLeft = point.X;
        }

        public void ConsoleInit()
        {
            var iStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
            if (!GetConsoleMode(iStdOut, out uint outConsoleMode))
            {
                Console.WriteLine("failed to get output console mode");
                Console.ReadKey();
                return;
            }

            outConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;
            if (!SetConsoleMode(iStdOut, outConsoleMode))
            {
                Console.WriteLine($"failed to set output console mode, error code: {GetLastError()}");
                Console.ReadKey();
                return;
            }
        }

        public App()
        {
            ConsoleInit();

            int length = colorMeta2.Length;
            int index = 0;
            Encoding defaultEncoder = Console.OutputEncoding;
            Console.OutputEncoding = Encoding.UTF8;

            StringBuilder sb = new StringBuilder();
            Console.WriteLine();

            for (int r=0;r<length;r++)
            {
                for(int g=0;g<length;g++)
                {
                    for(int b=0;b<length;b++)
                    {
                        int r2 = colorMeta2[r];
                        int g2 = colorMeta2[g];
                        int b2 = colorMeta2[b];
                        int foregroundColor = (r2 == 255 || g2 == 255 || b2 == 255) ? 0 : 255;
                        string formatted = $"({r2.ToString().PadLeft(3, ' ')},{g2.ToString().PadLeft(3, ' ')},{b2.ToString().PadLeft(3, ' ')})";

                        sb.Append($"\u001b[38;2;{r2};{g2};{b2}m█");

                        sb.Append($"\u001b[38;2;{foregroundColor};{foregroundColor};{foregroundColor}m");
                        sb.Append($"\u001b[48;2;{r2};{g2};{b2}m{formatted}");
                        sb.Append($"\u001b[38;2;{r2};{g2};{b2}m█");

                        sb.Append($"\u001b[38;2;{170};{170};{170}m");
                        sb.Append($"\u001b[48;2;{0};{0};{0}m");
                        index++;
                    }
                    sb.AppendLine();
                }
            }
            sb.AppendLine($"{index} total colors.");
            for (int a = 0x2580; a <= 0x259F; a++)
                sb.Append($"{(char)a}");
            for (int a = 0x25E2; a <= 0x25E5; a++)
                sb.Append($"{(char)a}");
            sb.AppendLine();

            Console.WriteLine(sb.ToString());
            Console.WriteLine(defaultEncoder.EncodingName);

            Console.OutputEncoding = defaultEncoder;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
        }
    }
}

@riezebosch
Copy link
Owner

It looks beautiful, but I've no idea what I'm looking at 🤷🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants