Click here to Skip to main content
15,355,133 members
Articles / Programming Languages / Python
Tip/Trick
Posted 31 Dec 2019

Stats

115.4K views
820 downloads
40 bookmarked

How to Put Color on Windows Console

Rate me:
Please Sign up or sign in to vote.
4.81/5 (26 votes)
19 Aug 2020CPOL2 min read
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):

BAT
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)

BAT
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:

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

In C:

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

In C++:

C++
#include <iostream>

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

In C#:

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

In Java:2

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

And in batch:

BAT
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:

BAT
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

I'm a student of programming and math, a chess player and a person who by nature loves to solve problems

Comments and Discussions

 
QuestionDoesn't seem to work in java. Pin
Zsombor Kászonyi24-Mar-22 5:04
MemberZsombor Kászonyi24-Mar-22 5:04 
GeneralMy vote of 5 Pin
mktschudi19-Mar-21 7:01
professionalmktschudi19-Mar-21 7:01 
GeneralRe: My vote of 5 Pin
Tiago Cavalcante Trindade9-May-21 10:45
MemberTiago Cavalcante Trindade9-May-21 10:45 
QuestionNice for private software Pin
wvd_vegt19-Aug-20 23:32
professionalwvd_vegt19-Aug-20 23:32 
AnswerRe: Nice for private software Pin
Tiago Cavalcante Trindade20-Aug-20 1:23
MemberTiago Cavalcante Trindade20-Aug-20 1:23 
GeneralRe: Nice for private software Pin
wvd_vegt20-Aug-20 1:55
professionalwvd_vegt20-Aug-20 1:55 
GeneralRe: Nice for private software Pin
Tiago Cavalcante Trindade20-Aug-20 2:44
MemberTiago Cavalcante Trindade20-Aug-20 2:44 
GeneralRe: Nice for private software Pin
wvd_vegt20-Aug-20 3:00
professionalwvd_vegt20-Aug-20 3:00 
QuestionNice! What is old is new again. Pin
davercadman19-Aug-20 6:28
Memberdavercadman19-Aug-20 6:28 
AnswerRe: Nice! What is old is new again. Pin
Tiago Cavalcante Trindade19-Aug-20 8:08
MemberTiago Cavalcante Trindade19-Aug-20 8:08 
QuestionSouvenir ... Pin
Gluups19-Aug-20 3:11
MemberGluups19-Aug-20 3:11 
AnswerRe: Souvenir ... Pin
Tiago Cavalcante Trindade19-Aug-20 7:31
MemberTiago Cavalcante Trindade19-Aug-20 7:31 
Questionwill chr(27) still work after registry change? Pin
Jan Heckman19-Aug-20 2:22
professionalJan Heckman19-Aug-20 2:22 
AnswerRe: will chr(27) still work after registry change? Pin
Tiago Cavalcante Trindade19-Aug-20 7:26
MemberTiago Cavalcante Trindade19-Aug-20 7:26 
AnswerMessage Closed Pin
4-Sep-20 2:15
MemberMember 149306624-Sep-20 2:15 
QuestionNote sure, don't seems to work Pin
Punprom19-Aug-20 2:00
MemberPunprom19-Aug-20 2:00 
AnswerRe: Note sure, don't seems to work Pin
Tiago Cavalcante Trindade19-Aug-20 8:07
MemberTiago Cavalcante Trindade19-Aug-20 8:07 
QuestionYou made my day Pin
YDaoust19-Aug-20 0:13
MemberYDaoust19-Aug-20 0:13 
AnswerRe: You made my day Pin
Tiago Cavalcante Trindade19-Aug-20 7:20
MemberTiago Cavalcante Trindade19-Aug-20 7:20 
Questiona screenshot will say more for your tips Pin
Southmountain18-Aug-20 6:39
MemberSouthmountain18-Aug-20 6:39 
AnswerRe: a screenshot will say more for your tips Pin
Tiago Cavalcante Trindade19-Aug-20 7:22
MemberTiago Cavalcante Trindade19-Aug-20 7:22 
GeneralMy vote of 5 Pin
tal_segal11-Mar-20 5:41
Membertal_segal11-Mar-20 5:41 
GeneralRe: My vote of 5 Pin
Tiago Cavalcante Trindade31-Mar-20 6:02
MemberTiago Cavalcante Trindade31-Mar-20 6:02 
QuestionHigh Color on WSL console Pin
Member 146691693-Feb-20 8:05
MemberMember 146691693-Feb-20 8:05 
QuestionHow to Put Color in Windows Pin
MARK ody23-Jan-20 21:17
MemberMARK ody23-Jan-20 21:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.