找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 16225|回复: 0

[特效代码] 控制台虚拟终端序列 32 位真彩色显示,支持 Alpha 通道!(仅限 Win 10 / Win 11)

[复制链接]
发表于 2023-1-31 14:26:54 | 显示全部楼层 |阅读模式
本帖最后由 HOPE2021 于 2023-1-31 14:41 编辑

您没听错,Cmd支持 32 位真彩色!
我在去年发了一篇帖子(http://www.bathome.net/viewthread.php?tid=63749),简单实现了控制台的绘图。但我觉得,仅仅是24位真彩色还不够。所以我在今天简单对32位色进行了支持。
32位色说起来也不复杂,本质上还是24位色,只不过添加了一个 Alpha 通道,经过一定运算,将其用 24 位色的形式渲染出来。
下面是32位色转24位色的算法:
  1. 输出 = 前景色 * Alpha / 255 + 背景色 * (255 - Alpha) / 255
复制代码
下面就是我的实例:
按键说明:
  1. < / >        换档
  2. + / -        值增/减 1
  3. O / P        值增/减 8
  4. [ / ]        值增/减 16
复制代码
源码:
  1. @Mode Con: Cols=80 Lines=25 & @Echo Off & @Chcp 936 > Nul & @Title 32 位色测试
  2. For /L %%_ in (0,1,3) Do (
  3.         Set lngColor[0][%%_]=127
  4.         Set lngColor[1][%%_]=127
  5.         Set "cur[0][%%_]= "
  6.         Set "cur[1][%%_]= "
  7. )
  8. Set /A idx1=0
  9. Set /A idx2=0
  10. Set "cur[%idx1%][%idx2%]=#"
  11. @Call :LoadFunctions
  12. @Goto :Main
  13. %= VT100 虚拟终端序列 32 位色尝试 =%

  14. :Main
  15.         Set "ESC="
  16.         If Not Defined ESC (
  17.                 For /F "Delims=#" %%_ in ('Prompt #$E# ^& Echo On ^& For %%$ in ^(1^) Do DosKey') Do Set "ESC=%%_"
  18.         )
  19.         @Call :Draw
  20.         %_Getch:#ascCode#=ascKey%
  21.         If "%ascKey%"=="" @Goto :Main
  22.         Set "cur[%idx1%][%idx2%]= "

  23.         %==================================================================%

  24.         If "%ascKey%"==">" Set "ascKey=."
  25.         If "%ascKey%"=="<" Set "ascKey=,"
  26.         If "%ascKey%"=="." (       
  27.                 Set /A "idx2+=1"
  28.                 If %idx1% Equ 0 (If %idx2% Equ 2 (Set /A idx1=1,idx2=0))
  29.                 If %idx1% Equ 1 (If %idx2% Equ 3 (Set /A idx1=0,idx2=0))
  30.         )
  31.         If "%ascKey%"=="," (       
  32.                 Set /A "idx2-=1"
  33.                 If %idx1% Equ 0 (If %idx2% Equ 0 (Set /A idx1=1,idx2=3))
  34.                 If %idx1% Equ 1 (If %idx2% Equ 0 (Set /A idx1=0,idx2=2))
  35.         )

  36.         %==================================================================%

  37.         If "%ascKey%"=="_" Set "ascKey=-"
  38.         If "%ascKey%"=="=" Set "ascKey=+"

  39.         Set /A lngTemp=lngColor[%idx1%][%idx2%]

  40.         If "%ascKey%"=="-" (       
  41.                 If %lngTemp% Gtr 0 (Set /A lngColor[%idx1%][%idx2%]-=1)
  42.         )
  43.         If "%ascKey%"=="+" (       
  44.                 If %lngTemp% Lss 255 (Set /A lngColor[%idx1%][%idx2%]+=1)
  45.         )

  46.         %==================================================================%

  47.         If /I "%ascKey%"=="o" (       
  48.                 If %lngTemp% Gtr 7 (Set /A lngColor[%idx1%][%idx2%]-=8) Else (Set /A lngColor[%idx1%][%idx2%]=0)
  49.         )
  50.         If /I "%ascKey%"=="p" (       
  51.                 If %lngTemp% Lss 248 (Set /A lngColor[%idx1%][%idx2%]+=8) Else (Set /A lngColor[%idx1%][%idx2%]=255)
  52.         )

  53.         %==================================================================%

  54.         If "%ascKey%"=="{" Set "ascKey=["
  55.         If "%ascKey%"=="}" Set "ascKey=]"

  56.         If "%ascKey%"=="[" (       
  57.                 If %lngTemp% Gtr 15 (Set /A lngColor[%idx1%][%idx2%]-=16) Else (Set /A lngColor[%idx1%][%idx2%]=0)
  58.         )
  59.         If "%ascKey%"=="]" (       
  60.                 If %lngTemp% Lss 240 (Set /A lngColor[%idx1%][%idx2%]+=16) Else (Set /A lngColor[%idx1%][%idx2%]=255)
  61.         )

  62.         %==================================================================%

  63.         Set "cur[%idx1%][%idx2%]=#"
  64. @Goto :Main

  65. :LoadFunctions
  66.         Set _Getch=Set "#ascCode#=" ^& ( For /F "Usebackq Delims=" %%_ in (`XCopy /L /W "%~f0" "%~f0" 2^^^>Nul`) Do If Not Defined #ascCode# ( Set "#ascCode#=%%_") ) ^& Call Set "#ascCode#=%%#ascCode#:~-1%%"
  67. @Goto :Eof

  68. :Draw
  69.         @Call :GotoXY 3 2
  70.         Set /P strTemp=%ESC%[?25l+<Nul
  71.         Set "strColor[0]=%ESC%[38;2;%lngColor[0][0]%;%lngColor[0][1]%;%lngColor[0][2]%m%ESC%[48;2;%lngColor[0][0]%;%lngColor[0][1]%;%lngColor[0][2]%m"
  72.         Set "strColor[2]=%ESC%[38;2;%lngColor[1][0]%;%lngColor[1][1]%;%lngColor[1][2]%m%ESC%[48;2;%lngColor[1][0]%;%lngColor[1][1]%;%lngColor[1][2]%m"
  73.         Set /A "lngR=%lngColor[1][0]%*%lngColor[1][3]%/255+%lngColor[0][0]%*(255-%lngColor[1][3]%)/255"
  74.         Set /A "lngG=%lngColor[1][1]%*%lngColor[1][3]%/255+%lngColor[0][1]%*(255-%lngColor[1][3]%)/255"
  75.         Set /A "lngB=%lngColor[1][2]%*%lngColor[1][3]%/255+%lngColor[0][2]%*(255-%lngColor[1][3]%)/255"
  76.         Set "strColor[1]=%ESC%[38;2;%lngR%;%lngG%;%lngB%m%ESC%[48;2;%lngR%;%lngG%;%lngB%m"

  77.         For /L %%_ in (0,1,2) Do (
  78.                 For /L %%` in (0,1,23) Do Set /P strTemp=-<Nul
  79.                 Set /P strTemp=+<Nul
  80.         )
  81.         @Call :GotoXY 3 13
  82.         Set /P strTemp=+<Nul
  83.         For /L %%_ in (0,1,2) Do (
  84.                 For /L %%` in (0,1,23) Do Set /P strTemp=-<Nul
  85.                 Set /P strTemp=+<Nul
  86.         )
  87.         SetLocal EnableExtensions EnableDelayedExpansion
  88.         For /L %%_ in (3,1,12) Do (
  89.                 @Call :GotoXY 3 %%_
  90.                 Set /P strTemp=%ESC%[37m%ESC%[40m^|<Nul
  91.                 For /L %%` in (0,1,2) Do (
  92.                         Set /P strTemp=!strColor[%%`]!<Nul
  93.                         For /L %%[ in (0,1,23) Do Set /P strTemp=#<Nul
  94.                         Set /P strTemp=%ESC%[37m%ESC%[40m^|<Nul
  95.                 )
  96.         )
  97.         EndLocal

  98.         %==================================================================%

  99.         @Call :GotoXY 3 16
  100.         (Echo.背景色:[%cur[0][0]%]R:%lngColor[0][0]% [%cur[0][1]%]G:%lngColor[0][1]% [%cur[0][2]%]B:%lngColor[0][2]%        )
  101.         @Call :GotoXY 3 18
  102.         (Echo.前景色:[%cur[1][0]%]R:%lngColor[1][0]% [%cur[1][1]%]G:%lngColor[1][1]% [%cur[1][2]%]B:%lngColor[1][2]% [%cur[1][3]%]A:%lngColor[1][3]%        )
  103. @Goto :Eof

  104. :GotoXY
  105.         Set /P=%ESC%[%2;%1H< Nul
  106. @Goto :Eof
复制代码
效果图:



评分

参与人数 3PB +8 技术 +3 收起 理由
CrLf + 8 + 1 感谢分享
lancer + 1 牛掰。。。
老刘1号 + 1 感谢分享

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 10:17 , Processed in 0.010741 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表