Skip to content

Console Application Color: Text and Background

Add color to your console application!

Color can help emphasize important content and create an engaging interface. C# console application color is limited, but it can still be used to improve your game or software interface.

Text and Background

Foreground and background color properties can be changed in C# console applications. The foreground is the color of the text, and the background is the space around what you are writing out to the console window.

Console Application Color Choices

There are several colors available by name, however not all the combinations of background and foreground will have enough contrast for legibility and readability.

Black text on a white background is considered the best choice if you have a lot of text for someone to read. While white text on a black background has a high contrast level (legible), it is considered less readable. Large areas of text with blues, or red and black together, can also be problematic (see Applying Color Theory to Digital Displays for a comprehensive guide to color and readability).

C# foreground and background console color list:

Console Application Color example
Color examples (code from MSDN)
  • Black
  • Blue
  • Cyan
  • DarkBlue
  • DarkCyan
  • DarkGray
  • DarkGreen
  • DarkMagenta
  • DarkRed
  • DarkYellow
  • Gray
  • Green
  • Magenta
  • Red
  • White
  • Yellow

Console Application Color Properties

Font color can be changed with the ForegroundColor property.

 Console.ForegroundColor = ConsoleColor.Blue;

The background can be changed with the BackgroundColor property.

Console.BackgroundColor = ConsoleColor.White;

Reset Colors

You can change the font and background colors back to the default with the ResetColor method.

Console.ResetColor();

Custom Functions

Console application color: text and background
Console application color: text and background

You can create custom functions to make it easier to swap between colors. For example, if you would like to have the text and background colors changed when something is updated in an application, you could have functions like the following:

static void Confirmation(string message)
{
    Console.BackgroundColor = ConsoleColor.White;
    Console.ForegroundColor = ConsoleColor.Blue;
    Console.WriteLine(message);
    Console.ResetColor();
}

static void Warning(string message)
{
    Console.BackgroundColor = ConsoleColor.Red;
    Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine(message);
    Console.ResetColor();
}

See full code

using System;

namespace ColorText
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Colored Text Example";
            string password = "";
            
            while (password != "password")
            {
                Console.Write("Enter the password: ");
                password = Console.ReadLine();
                if (password != "password")
                {
                    Warning("That was not the correct password!");
                    Console.WriteLine("Press enter to try again...");
                }
            }
            Confirmation("Password accepted.");
            Console.WriteLine("You have entered the correct password - congratulations!");
            Console.ReadKey();
        }

        static void Confirmation(string message)
        {
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine(message);
            Console.ResetColor();
        }
        static void Warning(string message)
        {
            Console.BackgroundColor = ConsoleColor.Red;
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(message);
            Console.ResetColor();
        }
    }
}

Console Clear

If you want to change the entire background you can use the same property: BackgroundColor. First you need to clear the current background color to see the full change, though. Otherwise only parts of the background will have updated.

Console Application Color: Example title screen for a game
Example title screen for a game
Console Application Color: White background, magenta text
Background color change without clearing the previous color first

The following two lines of code set the entire background to white:

Console.BackgroundColor = ConsoleColor.White;
Console.Clear();

See full code

using System;

namespace ColorText
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.White;
            Console.Clear();
            string TitleText = "";
            TitleText = @"


  .-_'''-.      ____    ,---.    ,---.    .-''-.   
 '_( )_   \   .'  __ `. |    \  /    |  .'_ _   \  
|(_ o _)|  ' /   '  \  \|  ,  \/  ,  | / ( ` )   ' 
. (_,_)/___| |___|  /  ||  |\_   /|  |. (_ o _)  | 
|  |  .-----.   _.-`   ||  _( )_/ |  ||  (_,_)___| 
'  \  '-   .'.'   _    || (_ o _) |  |'  \   .---. 
 \  `-'`   | |  _( )_  ||  (_,_)  |  | \  `-'    / 
  \        / \ (_ o _) /|  |      |  |  \       /  
   `'-...-'   '.(_,_).' '--'      '--'   `'-..-'   
                                                   
                  by Your Name                     
               (press enter to play)               
";
            Console.Title = "Colored Title Screen Example";
            Write(TitleText);

            Console.ReadKey();
            Console.Clear();
            Console.WriteLine("[Game would be here]");
            Console.ReadKey();
        }

        static void Write(string message)
        {
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine(message);
            Console.ResetColor();
        }
    }
}

You can also use the console's clear property to focus the user or player on important content. Clearing away what was printed to the screen before, and having only the newest information visible, can immediately direct the viewer's eyes to what has changed.

Interface Elements

Using the console's clear method can also provide the illusion of information persisting on the screen. You can even design areas of the screen for that information to reside in, like a Heads-up Display (HUD). Color can help you highlight different types of information within these areas.

Example of player data

Below is a code example that creates an area at the top of the application window for a simple display of player data.

using System;

namespace ColorText
{
    class Program
    {
        public static void Main(string[] args)
        {
            string PlayerName = "Test Player";
            string Location = "Starting Zone";
            int Turn = 0;
            int Gold = 0;

            Console.Title = "Simple Color Example";


            while (Turn < 20)
            {
                Turn++; Gold++;
                HUD(PlayerName, Location, Turn, Gold);
                Console.WriteLine("Press enter to simulate turns progressing... turn number " + Turn + " ... ");
                Console.ReadKey();
            }

            Console.WriteLine("Simulation of game ending");
            Console.ReadKey();

        }

        public static void HUD(string player, string location, int turn, int gold)
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("=============================================================");
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write("    " + player);
            Console.ResetColor();
            Console.Write(" :: ");
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write(location);
            Console.ResetColor();
            Console.Write(" :: ");
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("turn " + turn + " /20");
            Console.ResetColor();
            Console.Write(" :: ");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write(gold + " gold\n");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("=============================================================\n\n\n\n");
            Console.ResetColor();
        }
    }
}

Color Resources

Related Posts