显示用户选择的颜色输出以及所选颜色的名称应该如何实现?

huangapple go评论64阅读模式
英文:

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

问题

最近我问过如何在命令提示符窗口中显示用户选择的颜色代码,称为"是否可以使用批处理编程在命令提示符中输出当前颜色代码?"。我想要花一点时间感谢那些回答了我的人。非常感谢!

现在我有一个关于同一主题的另一个问题。首先,我想说我找到了一个非常简单、直接的 .bat 文件,可以告诉你可以选择的颜色列表,并允许你将背景和文本更改为任何颜色。然后,一旦你做出选择并按下"Enter"键,代码将会告诉你选择了颜色 0a,这是因为我选择了黑色背景和绿色文本 (0a) 作为我的选择,代码如下:echo: You choose %color%

但是,我想更详细地解释一下。我想让文件分解用户所做的颜色选择,而不是输出实际的颜色代码,我希望它在输出颜色代码的顶部输出颜色名称。我想要像这样的东西:"你选择了颜色 0 = 黑色 [背景] 和 A = [文本]。我确实希望在echo命令中使用方括号,以便用户在命令提示符窗口上看到方括号。

这能在批处理代码中完成吗?

英文:

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?

答案1

得分: 1

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

REM - 必需
SETLOCAL EnableDelayedExpansion

REM - 用于颜色功能中删除提示符。
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)

REM - 代码选择功能。 Choice 用于独立选择前景/背景的颜色代码

:ColorSelect
cls

CHOICE /N /C:abcdef0123456789 /M "选择背景颜色:A B C D E F 0 1 2 3 4 5 6 7 8 9"
CALL :TESTCC !ERRORLEVEL! bgcode bgcolor

REM - 使用Call发送三个参数到:TESTCC。 <errorlevel> <codeposition> <positioncolor>
REM - 第一个参数具有需要测试的值,其他两个参数是要定义的变量,以在:TESTCC中使用。

CHOICE /N /C:abcdef0123456789 /M "选择前景颜色:A B C D E F 0 1 2 3 4 5 6 7 8 9"
CALL :TESTCC !ERRORLEVEL! fgcode fgcolor

REM - 检查是否选择了匹配的颜色代码

IF "!bgcode!"=="!fgcode!" (
    ECHO 颜色不能匹配。
    Timeout 2 > nul
    GOTO ColorSelect
)
cls

REM - 在标准颜色中显示,用于在难以区分颜色的情况下显示消息
REM - 调用函数以使用所选颜色打印消息。

ECHO 颜色 !bgcode!!fgcode! 背景代码 [!bgcode!] 颜色 [!bgcolor!] 前景代码 [!fgcode!] 颜色 [!fgcolor!]
CALL :colorize "!bgcode!!fgcode!" "颜色 !bgcode!!fgcode! 背景代码 [!bgcode!] 颜色 [!bgcolor!] 前景代码 [!fgcode!] 颜色 [!fgcolor!]"
ECHO.
pause
GOTO ColorSelect

REM - 使用Call发送的参数定义变量以供使用和显示

:TESTCC
IF %~1==1 Set "%~2=a" && Set "%~3=亮绿"
IF %~1==2 Set "%~2=b" && Set "%~3=亮青"
IF %~1==3 Set "%~2=c" && Set "%~3=亮红"
IF %~1==4 Set "%~2=d" && Set "%~3=亮紫"
IF %~1==5 Set "%~2=e" && Set "%~3=亮黄"
IF %~1==6 Set "%~2=f" && Set "%~3=亮白"
IF %~1==7 Set "%~2=0" && Set "%~3=黑色"
IF %~1==8 Set "%~2=1" && Set "%~3=蓝色"
IF %~1==9 Set "%~2=2" && Set "%~3=绿色"
IF %~1==10 Set "%~2=3" && Set "%~3=青色"
IF %~1==11 Set "%~2=4" && Set "%~3=红色"
IF %~1==12 Set "%~2=5" && Set "%~3=紫色"
IF %~1==13 Set "%~2=6" && Set "%~3=黄色"
IF %~1==14 Set "%~2=7" && Set "%~3=白色"
IF %~1==15 Set "%~2=8" && Set "%~3=灰色"
IF %~1==16 Set "%~2=9" && Set "%~3=亮蓝"
exit /b 0

REM - 颜色化功能。使用方法:
REM - CALL :Colorize colorcodes "带引号的字符串"
REM - 该函数通过创建一个以字符串命名的临时文件并以指定颜色打印它来工作。
REM - 注意:不能使用文件名中保留用于文件名的字符。删除尾随空格。

: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

存在许多不同的显示信息的方法,只是选择适当的方式。
英文:
@ECHO OFF
MODE CON: cols=110 lines=40

REM - Required
SETLOCAL EnableDelayedExpansion

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

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

:ColorSelect
cls

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

REM - Three parameters get sent to :TESTCC with Call. &lt;errorlevel&gt; &lt;codeposition&gt; &lt;positioncolor&gt;
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 &quot;Select Foreground Color: A B C D E F 0 1 2 3 4 5 6 7 8 9&quot;
CALL :TESTCC !ERRORLEVEL! fgcode fgcolor

REM - Test that matching color codes have not been selected

IF &quot;!bgcode!&quot;==&quot;!fgcode!&quot; (
	ECHO Colors Cannot Match.
	Timeout 2 &gt; 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 &quot;!bgcode!!fgcode!&quot; &quot;Color !bgcode!!fgcode! Background Code [!bgcode!] Color [!bgcolor!] Foreground Code [!fgcode!] Color [!fgcolor!]&quot;
ECHO.
pause
GOTO ColorSelect

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

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

REM - Colorize function. To use: 
REM - CALL :Colorize colorcodes &quot;String in quotes&quot;
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 &quot;%TEMP%\colorize&quot; md &quot;%TEMP%\colorize&quot;
PUSHD %TEMP%\colorize
&lt;nul set /p &quot;.=%DEL%&quot; &gt; &quot;%~2&quot;
findstr /v /a:%1 /R &quot;^$&quot; &quot;%~2&quot; nul
del &quot;%~2&quot; &gt; nul 2&gt;&amp;1
POPD
goto :eof

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

huangapple
  • 本文由 发表于 2020年1月3日 16:52:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定