Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Put Color on Windows Console

0.00/5 (No votes)
19 Aug 2020 6  
How to put color in Python, C, C++, C#, Java and batch on the Windows console
In this article, you will see how to use ANSI colors in Windows and Linux without any modification on code, with Python, C, C++, C# and Java, and how to put color on batch (Command Prompt).

Introduction

Now Windows have support for ANSI colors!

On old builds (Windows 10 1909 or prior), Windows just recognizes the ESC code 1, but it is possible for Windows to recognize the ESC code \033 (or other depending on the language) with a modification in the record (regedit.exe).

The Modification

For modification, you need to run the command below (on CMD as administrator):

reg add HKEY_CURRENT_USER\Console /v VirtualTerminalLevel /t REG_DWORD /d 0x00000001 /f

In case of error (it'll have no error, but...), here is the command to undo: (on CMD as administrator)

reg add HKEY_CURRENT_USER\Console /v VirtualTerminalLevel /t REG_DWORD /d 0x00000000 /f

With this modification, you can compile the same program for Windows and Linux without changing anything; before to color the text of the console on Python for Linux, you used the ESC code, on Windows you used a library, so you had to write the same program twice!

Colors

Color Foreground Background
Black <ESC>[30m <ESC>[40m
Red <ESC>[31m <ESC>[41m
Green <ESC>[32m <ESC>[42m
Yellow <ESC>[33m <ESC>[43m
Blue <ESC>[34m <ESC>[44m
Magenta <ESC>[35m <ESC>[45m
Cyan <ESC>[36m <ESC>[46m
Light gray <ESC>[37m <ESC>[47m
Dark gray <ESC>[90m <ESC>[100m
Light red <ESC>[91m <ESC>[101m
Light green <ESC>[92m <ESC>[102m
Light yellow <ESC>[93m <ESC>[103m
Light blue <ESC>[94m <ESC>[104m
Light magenta <ESC>[95m <ESC>[105m
Light cyan <ESC>[96m <ESC>[106m
White <ESC>[97m <ESC>[107m
Bold <ESC>[1m -
Underline <ESC>[4m -
No underline <ESC>[24m -
Negative(reverse the foreground and background) <ESC>[7m -
Positive(no negative) <ESC>[27m -
Default <ESC>[0m -

To use a color, you need to write the ANSI color, your text and the ANSI color default.

Here goes an example of this in Python:

print("\033[31mThis is red\033[0m")

In C:

int main() {
    printf("\033[33mThis is yellow\033[0m");
    return 0;
}

In C++:

#include <iostream>

int main() {
    std::cout << "\033[32mThis is green\033[0m" << std::endl;
    return 0;
}

In C#:

class Program {
    static void Main() {
        System.Console.WriteLine("\u001b[35mThis is purple\u001b[0m");
    }
}

In Java:2

public class Color {
    public static void main(String[] args) {
        System.out.println("\033[36mThis is cyan\033[0m");
    }
}

And in batch:

echo This is blue

Access this site for more information and others ANSIs.

Where Does It Work?

This method of the modification works (that I tested) in Python, C, C++ and Java, but doesn't work in batch, there the <ESC> has to be , and in C#, there the <ESC> has to be \u001b.

Why Would I Use That?

With this method, you can compile for Linux without modification in Python, C, C++, C# and Java.

Notes

1 The character is a rectangle, if a question appears for you, just copy and paste it into Notepad and it will appear correctly.

2 If you click on jar file, nothing will happen, to run you have to call from the prompt, for that run the command lower in the folder that is jar file:

java -jar "program.jar"

where program.jar is the name of the jar file.

History

  • 1st January, 2020: Initial version
  • 21st February, 2020: C# update
  • 9th March, 2020: Java update
  • 18th August, 2020: colors' update

Read My Other Article

If you have any comments, please leave a note below.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here