Return to Snippet

Revision: 65307
at November 16, 2013 14:16 by Rick


Initial Code
class RPGConsole
    {
        public static Dictionary<char, ConsoleColor> Colors;
        public static char SpecialFGChar { get; set; }
        public static char SpecialBGChar { get; set; }
        public static char ResetChar { get; set; }

        static RPGConsole()
        {
            Colors = new Dictionary<char, ConsoleColor>();

            // set some defaults
            SpecialFGChar = '#';
            SpecialBGChar = '$';
            ResetChar = 'x';

            // add some default colors
            Colors.Add('r', ConsoleColor.Red);
            Colors.Add('y', ConsoleColor.Yellow);
            Colors.Add('g', ConsoleColor.Green);
            Colors.Add('b', ConsoleColor.Blue);
            Colors.Add('w', ConsoleColor.White);
        }

        public static void WriteLine(string value)
        {
            Write(value);

            Console.Write(Environment.NewLine);
        }

        private static bool SetFGColor(char value)
        {
            // look at the next char and make sure we have the color code registered
            if (Colors.ContainsKey(value))
            {
                Console.ForegroundColor = Colors[value];

                return true;
            }
            else if (value == ResetChar)
            {
                Console.ResetColor();

                return true;
            }

            return false;
        }

        private static bool SetBGColor(char value)
        {
            // look at the next char and make sure we have the color code registered
            if (Colors.ContainsKey(value))
            {
                Console.BackgroundColor = Colors[value];

                return true;
            }
            else if (value == ResetChar)
            {
                Console.ResetColor();

                return true;
            }

            return false;
        }

        // we call foreground switching twice so that the order of defining bg & fg doesn't matter in the string
        public static void Write(string value)
        {
            char[] letters = value.ToCharArray();

            for (int i = 0; i < letters.Count(); i++)
            {
                // switch foreground color
                if (letters[i] == SpecialFGChar)
                {
                    if (SetFGColor(letters[i + 1]))
                        i += 2;
                }

                // switch background color
                if (letters[i] == SpecialBGChar)
                {
                    if (SetBGColor(letters[i + 1]))
                        i += 2;
                }

                // switch foreground color
                if (letters[i] == SpecialFGChar)
                {
                    if (SetFGColor(letters[i + 1]))
                        i += 2;
                }

                Console.Write(letters[i]);
            }
        }
    }

Initial URL


Initial Description
This is way to get different fg and bg colors inside 1 string. You provide escape like characters to define colors.

This is a fg of #rRed #xand now we're back to normal.
This is a bg of $bBlue#x and now we're back to normal.
I can combine fg & bg#r$bcolors #xalso.

Initial Title
Different colors in console string

Initial Tags
color

Initial Language
C#