简体   繁体   中英

How do I display the output of the color the user has selected while displaying the name of the color selected?

Recently I have asked about displaying the color code the user has chosen on the command prompt window itself called " Is it possible to output the current color code on the command prompt using batch programming? " I would like to take a moment and thank those who have responded to that. Thank you very much!

Now I have another question on the same topic. First I would like to say that I had found a really simple, straightforward .bat file that tell you the listing of colors you can choose from, and allows you to change the background and text to whatever color you want. Then once you have made your selection and hit "enter", the codes will say you choose color 0a, that's because I had picked the black background with the green text (0a) as my selection, and the code for that is echo: You choose %color% .

However, I would like to break that down more. I would like the file to breakdown the color selection the user has made and instead of outputting the actual color code, I would like it to output the name on top of the color code being output. I would like to be able to have something like this: "You chose color 0 = Black [Background] and A = [Text]. I do would like to have the square brackets in the echo command to have the user see the square brackets on the command prompt window.

Can this be done in batch code?

@ECHO OFF
MODE CON: cols=110 lines=40

REM - Required
SETLOCAL EnableDelayedExpansion

REM - Used in Colorize Function to Remove the Prompt.
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)

REM - Code selection Function. Choice is used to select Colorcodes Independantly for FG/BG

:ColorSelect
cls

CHOICE /N /C:abcdef0123456789 /M "Select Background Color: A B C D E F 0 1 2 3 4 5 6 7 8 9"
CALL :TESTCC !ERRORLEVEL! bgcode bgcolor

REM - Three parameters get sent to :TESTCC with Call. <errorlevel> <codeposition> <positioncolor>
REM - The first paramater has a value that gets tested, the other 2 parameters are Variables to be Defined
REM c- in :TESTCC using the result of If checks on Parameter 1.

CHOICE /N /C:abcdef0123456789 /M "Select Foreground Color: A B C D E F 0 1 2 3 4 5 6 7 8 9"
CALL :TESTCC !ERRORLEVEL! fgcode fgcolor

REM - Test that matching color codes have not been selected

IF "!bgcode!"=="!fgcode!" (
    ECHO Colors Cannot Match.
    Timeout 2 > nul
    GOTO ColorSelect
)
cls

  • When you echo something to the console standard output - stdout , you choose what to say. As long as any variables you expand are defined, you can display their information
  • It's really up to you to imagine what information you wish to display, and figuring out what input or variables you need to get or set to achieve the desired result.
REM - Display in standard Colors, for those times Colors are hard to distinguish
REM - Calls the Function to Print the message in the Selected colors.

ECHO Color !bgcode!!fgcode! Background Code [!bgcode!] Color [!bgcolor!] Foreground Code [!fgcode!] Color [!fgcolor!]
CALL :colorize "!bgcode!!fgcode!" "Color !bgcode!!fgcode! Background Code [!bgcode!] Color [!bgcolor!] Foreground Code [!fgcode!] Color [!fgcolor!]"
ECHO.
pause
GOTO ColorSelect

REM - Uses Parameters Sent by Call to Define Variables for Use and Display

:TESTCC
IF %~1==1 Set "%~2=a" && Set "%~3=Light Green"
IF %~1==2 Set "%~2=b" && Set "%~3=Light Aqua"
IF %~1==3 Set "%~2=c" && Set "%~3=Light Red"
IF %~1==4 Set "%~2=d" && Set "%~3=Light Purple"
IF %~1==5 Set "%~2=e" && Set "%~3=Light Yellow"
IF %~1==6 Set "%~2=f" && Set "%~3=Light White"
IF %~1==7 Set "%~2=0" && Set "%~3=Black"
IF %~1==8 Set "%~2=1" && Set "%~3=Blue"
IF %~1==9 Set "%~2=2" && Set "%~3=Green"
IF %~1==10 Set "%~2=3" && Set "%~3=Aqua"
IF %~1==11 Set "%~2=4" && Set "%~3=Red"
IF %~1==12 Set "%~2=5" && Set "%~3=Purple"
IF %~1==13 Set "%~2=6" && Set "%~3=Yellow"
IF %~1==14 Set "%~2=7" && Set "%~3=White"
IF %~1==15 Set "%~2=8" && Set "%~3=Grey"
IF %~1==16 Set "%~2=9" && Set "%~3=light Blue"
exit /b 0

REM - Colorize function. To use: 
REM - CALL :Colorize colorcodes "String in quotes"
REM - Function works by creating a temporary file named using the string and Printing it Colored with FINDSTR
REM - Note: Cannot use strings with characters reserved against use in filenames. Strips trailing Spaces.

Color Function Used from Jebs answer here . Other, Improved methods can be found there.

 :Colorize
IF NOT EXIST "%TEMP%\colorize" md "%TEMP%\colorize"
PUSHD %TEMP%\colorize
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
POPD
goto :eof

There's many, many different ways of displaying information, It's just a matter of choosing the means.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM