如何使用ansii批量显示所有颜色

问题描述 投票:0回答:1

我的批处理脚本正在运行。我希望它每秒以不同的颜色回显单词,并且我对颜色使用 Ansii 转义码。最后我想出了这段代码:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
chcp 65001

REM Define ANSI escape codes
SET "ESC="

REM Set initial values for color variables
set /a col1=0
set /a col2=0
set /a col3=0

REM Loop through a range of iterations
for /L %%i in (1,1,25000) do (
    REM Print text with current color settings
    echo %ESC%[38;2;!col1!;!col2!;!col3!m"█▓▒▒░░░Text Editor░░░▒▒▓█"
    
    REM Pause
    timeout /t 1 /nobreak > nul
    
    REM Increment color variables
    if !col1! LSS 255 (
        set /a col1+=1
    ) else (
        if !col2! LSS 255 (
            set /a col2+=1
        ) else (
            if !col3! LSS 255 (
                set /a col3+=1
            ) else (
                echo No more color combinations
                pause
                exit /b
            )
        )
    )
)

这段代码有效,但颜色从黑色->红色->黄色->白色

如何显示每种颜色?而且很快?

batch-file vt100
1个回答
0
投票

由于 RGB 颜色的工作原理,您将看到许多与您已经见过的颜色相同的颜色。你必须相信我的话,它们是不同的,尽管几乎是难以察觉的。

您所要做的就是嵌套三个

for /L
循环,然后您根本不必跟踪任何计数器。

@echo off
setlocal


set "ESC="
for /L %%A in (0,1,255) do (
    for /L %%B in (0,1,255) do (
        for /L %%C in (0,1,255) do (
            echo %ESC%[38;2;%%A;%%B;%%Cm█▓▒▒░░░Text Editor░░░▒▒▓█
        )
    )
)

与您的代码一样,该代码在

set
语句中也有一个转义字符,该字符会被 Stack Overflow 吞没。

© www.soinside.com 2019 - 2024. All rights reserved.