Search code examples
batch-filewindows-10-desktop

How can I apply multiple colors to "Lines" in Windows Command Prompt?


I am a newbie here on this website and I would like to ask you how to apply multiple colors by line (or column maybe?) in CMD. Feel free to edit the codes below, I would also like the color of the words to be similar like the classic Google Chrome colors.

cls
@echo off
color 0a
echo.
echo.
echo.
echo                                                                                                      ,,
echo                                                         .g8"""bgd                                  `7MM
echo                                                        .dP'    `M                                    MM
echo                                                        dm'      `    ,pW"Wq.    ,pW"Wq.    .P"Ybmmm  MM   .gP"Ya
echo                                                        MM           6W'   `Wb  6W'   `Wb  :MI  I8    MM  ,M'   Yb
echo                                                        MM.   `7MMF' 8M     MB  8M     MB   Wmmmp"    MM  8M""""""
echo                                                        `Mb     MM   YA.   ,A9  YA.   ,A9  8M         MM  YM.    .
echo                                                         `"bmmmdPY    `Ybmd9'    `Ybmd9'    YMMMMMb .JMML. `Mbmmd'
echo                                                                                           6'    dP
echo                                                                                            Ybmmmd'
echo.
echo.
echo.
pause>nul

Solution

  • Here's a somewhat neater looking version of the ANSI escape code method, already suggested by Gerhard.

    @Echo Off
    SetLocal EnableExtensions
    For /F %%G In ('Echo Prompt $E ^| "%__AppDir__%cmd.exe"') Do Set "ESC=%%G"
    ClS
    Echo=
    Echo=
    Echo=
    Echo                             %ESC%[92m                                                ,,
    Echo                             %ESC%[94m  .g8"""bgd  %ESC%[92m                                 `7MM
    Echo                             %ESC%[94m .dP'    `M  %ESC%[92m                                   MM
    Echo                             %ESC%[94m dm'      `  %ESC%[91m  ,pW"Wq.  %ESC%[93m  ,pW"Wq. %ESC%[94m  .P"Ybmmm %ESC%[92m   MM  %ESC%[91m  .gP"Ya
    Echo                             %ESC%[94m MM          %ESC%[91m 6W'   `Wb %ESC%[93m 6W'   `Wb%ESC%[94m :MI  I8   %ESC%[92m   MM  %ESC%[91m ,M'   Yb
    Echo                             %ESC%[94m MM.   `7MMF'%ESC%[91m 8M     MB %ESC%[93m 8M     MB%ESC%[94m  Wmmmp"   %ESC%[92m   MM  %ESC%[91m 8M""""""
    Echo                             %ESC%[94m `Mb     MM  %ESC%[91m YA.   ,A9 %ESC%[93m YA.   ,A9%ESC%[94m 8M        %ESC%[92m   MM  %ESC%[91m YM.    .
    Echo                             %ESC%[94m  `"bmmmdPY  %ESC%[91m  `Ybmd9'  %ESC%[93m  `Ybmd9' %ESC%[94m  YMMMMMb  %ESC%[92m .JMML.%ESC%[91m  `Mbmmd'
    Echo                             %ESC%[94m                                   6'    dP
    Echo                                                                 Ybmmmd' %ESC%[0m
    Echo=
    Echo=
    Echo=
    Pause 1> NUL
    

    You could even mess around with it to put it into a bright white box, for instance:

    @Echo Off
    SetLocal EnableExtensions
    For %%# In (BG FG)Do Set "%%#="
    For /F %%# In ('%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoP ^
        "$Console=(Get-Host).UI.RawUI;Switch($Console.BackgroundColor," ^
        "$Console.ForegroundColor){'Black'{40}'DarkBlue'{44}'DarkGreen'{42}" ^
        "'DarkCyan'{46}'DarkRed'{41}'DarkMagenta'{45}'DarkYellow'{43}'Gray'{47}" ^
        "'DarkGray'{100}'Blue'{104}'Green'{102}'Cyan'{106}'Red'{101}" ^
        "'Magenta'{105}'Yellow'{103}'White'{107}}" 2^>NUL')Do If Not Defined BG (
        Set "BG=%%#")Else Set "FG=%%#"
    For /F %%G In ('Echo Prompt $E ^| "%__AppDir__%cmd.exe"') Do Set "ESC=%%G"
    ClS
    Echo=
    Echo=
    Echo=
    Echo                             %ESC%[92;107m                                                ,,            %ESC%[%FG%;%BG%m
    Echo                             %ESC%[94;107m  .g8"""bgd  %ESC%[92m                                 `7MM            %ESC%[%FG%;%BG%m
    Echo                             %ESC%[94;107m .dP'    `M  %ESC%[92m                                   MM            %ESC%[%FG%;%BG%m
    Echo                             %ESC%[94;107m dm'      `  %ESC%[91m  ,pW"Wq.  %ESC%[93m  ,pW"Wq. %ESC%[94m  .P"Ybmmm %ESC%[92m   MM  %ESC%[91m  .gP"Ya  %ESC%[%FG%;%BG%m
    Echo                             %ESC%[94;107m MM          %ESC%[91m 6W'   `Wb %ESC%[93m 6W'   `Wb%ESC%[94m :MI  I8   %ESC%[92m   MM  %ESC%[91m ,M'   Yb %ESC%[%FG%;%BG%m
    Echo                             %ESC%[94;107m MM.   `7MMF'%ESC%[91m 8M     MB %ESC%[93m 8M     MB%ESC%[94m  Wmmmp"   %ESC%[92m   MM  %ESC%[91m 8M"""""" %ESC%[%FG%;%BG%m
    Echo                             %ESC%[94;107m `Mb     MM  %ESC%[91m YA.   ,A9 %ESC%[93m YA.   ,A9%ESC%[94m 8M        %ESC%[92m   MM  %ESC%[91m YM.    . %ESC%[%FG%;%BG%m
    Echo                             %ESC%[94;107m  `"bmmmdPY  %ESC%[91m  `Ybmd9'  %ESC%[93m  `Ybmd9' %ESC%[94m  YMMMMMb  %ESC%[92m .JMML.%ESC%[91m  `Mbmmd' %ESC%[%FG%;%BG%m
    Echo                             %ESC%[94;107m                                   6'    dP                   %ESC%[%FG%;%BG%m
    Echo                             %ESC%[94;107m                                    Ybmmmd'                   %ESC%[%FG%;%BG%m
    Echo=
    Echo=
    Echo=
    Pause 1> NUL
    GoTo :EOF
    

    This above method leverages to determine, and create variables using the current console foreground and background colors. This ensures that they are reset correctly after the color changed image lines have been processed. (Of course the script writer could just define known colors, right from the outset)

    I will add however, that the command line is supposed to be for typing commands and reading text output, not for displaying unnecessary childish images and colors. If you want a GUI, build a GUI program, not a console one.