Can We Use More Colors In Batch Script?


Answer :

Can I use more the 16 colours in a Windows batch file?

No, as most most Windows apps only support 16 colours.

However, in the Windows 10 Insiders Build #14931 the Windows Console was updated to support 24-bit RGB true color.

Unfortunately as mentioned above most Windows apps cannot make use of this enhancement (yet).

However, using Windows Subsystem for Linux (WSL), Linux scripts and tools can use the Console's new 24-bit color support:

One of the most frequent requests we receive is to increase the number of colors that the Windows Console can support. We love nothing more than to deliver features you ask for!

But rather than just add a few more colors, or limit our console to a mere 256 colors, in Windows 10 Insiders Build #14931,

we’ve updated the Windows Console to support full, glorious 24-bit RGB true color!

This is actually a little tricky to demo since most Windows apps only support 16 colors at most whereas the Linux world has broadly supported 256 color terminals for a while now, and 24-bit color is becoming more established.

Thanks to our ability to run Linux apps and scripts using our new Bash on Ubuntu on Windows environment atop the Windows Subsystem for Linux (WSL), we can use some Linux scripts and tools to demonstrate the Console’s new 24-bit color support:

enter image description here

enter image description here

We’ve not yet started work on improving the console properties page to support the Console’s new color rendering capabilities, and we’ve not modified the default Windows color mappings; we’ll be making improvements here, and on the many, MANY other features queued-up in our backlog in future builds.

Source 24-bit Color in the Windows Console! | Windows Command Line Tools For Developers


Windows 10 conhost.exe does support ANSI escape codes including 24-bit color. See

  • 24-bit Color in the Windows Console!
  • Updating the Windows Console Colors

However currently you need to enable ANSI sequence support in by creating a DWORD named VirtualTerminalLevel in HKEY_CURRENT_USER\Console and set its value to 1. After that if 24-bit color is supported in bash then you can also use 24-bit ANSI color in any cmd or PowerShell sessions. Below is a cmd example

echo ESC[38;2;40;177;249m color: 40;177;249 ESC[0m echo ESC[38;2;255;255;100m color: 255;255;100 echo ESC[48;2;200;0;200m color: 200;0;200 ESC[0m 

Unfortunately I can't paste the raw escape character here, so you'll need to change ESC to the real escape character (ASCII 27/0x1B) locally. Here's the sample output:

cmd 24-bit color

In PowerShell it's easier to embed the escape character as [char]0x1B, [char]27, `u{001B} or `e without resolving to methods to paste the raw escape character

echo "$([char]0x1B)[38;2;40;177;249m color: 40;177;249 $([char]0x1B)[0m" echo "$([char]0x1B)[38;2;255;255;100m color: 255;255;100" echo "$([char]0x1B)[48;2;200;0;200m color: 200;0;200 $([char]0x1B)[0m" 

powershell 24-bit color

You can also check detect an ANSI-compatible console from Windows batch file and print 4-bit or 24-bit color depending on the current console. In PowerShell it's a lot simpler, just use $Host.UI.SupportsVirtualTerminal

See How to make win32 console recognize ANSI/VT100 escape sequences?


Comments

Popular posts from this blog

Android Manifest Permission Internet Code Example

Android SDK Location Should Not Contain Whitespace, As This Cause Problems With NDK Tools

Android How Can I Convert A String To A Editable