Click here to Skip to main content
14,422,431 members

How to Put Color in Windows

Rate this:
4.77 (5 votes)
Please Sign up or sign in to vote.
4.77 (5 votes)
13 Jan 2020CPOL
In this article, I will show you how to put color in C++, Python and batch on the Windows console.

Introduction

Windows has support for ANSI colors without any modification in the Prompt and yes in the ESC code.

The ESC code usually is \033 but Windows recognizes  (yes, a rectangle), but it is possible for Windows to recognize the ESC code \033 with a modification in the record (regedit.exe).

The Modification

For modification, you need to run the command below:

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

In case of error, here is the command to undo:

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

Colors

Color Foreground Background
Black <ESC>[1;30m <ESC>[1;40m
Red <ESC>[1;31m <ESC>[1;41m
Green <ESC>[1;32m <ESC>[1;42m
Yellow <ESC>[1;33m <ESC>[1;43m
Blue <ESC>[1;34m <ESC>[1;44m
Magenta <ESC>[1;35m <ESC>[1;45m
Cyan <ESC>[1;36m <ESC>[1;46m
Light gray <ESC>[1;37m <ESC>[1;47m
Dark gray <ESC>[1;90m <ESC>[1;100m
Light red <ESC>[1;91m <ESC>[1;101m
Light green <ESC>[1;92m <ESC>[1;102m
Light yellow <ESC>[1;93m <ESC>[1;103m
Light blue <ESC>[1;94m <ESC>[1;104m
Light magenta <ESC>[1;95m <ESC>[1;105m
Light cyan <ESC>[1;96m <ESC>[1;106m
White <ESC>[1;97m <ESC>[1;107m
Bold <ESC>[1m -
Reverse the foreground and background <ESC>[7m -
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[1;31mThis is red\033[0m")

In C++:

#include <iostream>

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

And in batch:

echo This is blue

Access this site for more information.

Where Does It Work?

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

Why Would I Use That?

With this method, you can compile without modification in C++ and Python!

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

History

  • 1st January, 2020: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

Comments and Discussions

 
PraiseMessage Closed Pin
40mins ago
MemberRipoff Removal Assistance40mins ago 
PraiseMy vote of 5 Pin
Michael Haephrati17hrs 12mins ago
professionalMichael Haephrati17hrs 12mins ago 
QuestionMessage Closed Pin
14-Jan-20 2:47
MemberClash with Warriors14-Jan-20 2:47 
GeneralMy vote of 4 Pin
schlebe2-Jan-20 10:03
Memberschlebe2-Jan-20 10:03 
GeneralRe: My vote of 4 Pin
Tiago Cavalcante Trindade13-Jan-20 9:41
MemberTiago Cavalcante Trindade13-Jan-20 9:41 
GeneralRe: My vote of 4 Pin
schlebe13-Jan-20 11:35
Memberschlebe13-Jan-20 11:35 
GeneralRe: My vote of 4 Pin
Tiago Cavalcante Trindade13-Jan-20 13:00
MemberTiago Cavalcante Trindade13-Jan-20 13:00 

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.

Tip/Trick
Posted 31 Dec 2019

Tagged as

Stats

5K views
4 bookmarked