Ansi扩展颜色索引(17-255)中颜色的RGB值 [英] RGB values of the colors in the Ansi extended colors index (17-255)

查看:193
本文介绍了Ansi扩展颜色索引(17-255)中颜色的RGB值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是使用ansi颜色进行一般的外壳脚本编写,但作为参考,我正在使用Apple Mac OS X 10.9 Mavericks.我使用"iTerm"终端应用程序作为我的默认终端,但也使用内置的终端"应用程序进行了检查.我使用ZSH(5.0.7)作为默认外壳程序,但也签入了BASH(3.2.51).

My question is in general shell scripting with ansi colors but for reference I am using an Apple Mac OS X 10.9 Mavericks. I use "iTerm" terminal app as my default terminal but also checked with the built in "terminal" app as well. I use ZSH (5.0.7) as my default shell but also checked in BASH (3.2.51).

我一直在尝试找出是否存在使用esc [38; 5; xm和esc [48; 5; xm其中x是从0到255的数字.我发现一些脚本将颜色打印为块(使用索引),但是我想知道索引中每种颜色的rgb值.

I have been trying to find out if there is a list of the RGB values for the 256 color indexed extended fore/background Ansi escape codes that are available using esc[38;5;xm and esc[48;5;xm where x is a number from 0 to 255. I have found some scripts that print out the colors as blocks (using the index) but I want to know the rgb values of each of the colors in the indexes.

以下是正在使用的ansi代码的示例:

Here is an example of the ansi codes in use:

printf '\e[38;5;222;48;5;238m  Hi  \e[m\n'

(\ e可以替换为\ 033或\ x1b)

(\e can be replaced by \033 or \x1b)

所以基本上我想知道是否有这些索引颜色的列表或商定的设置?例如232-255似乎总是灰色渐变.我发现的每个引用扩展颜色的网站都只是在说如何使用扩展颜色,而未列出任何特定颜色.

So basically I am wondering if there is a list or agreed upon settings for these indexed colors? For example 232-255 seem to always be a gray gradient. Every site that I have found that references the extended colors pretty much just says how to use it and doesn't list any specific colors.

我找到了一些对X11的引用和一个rgb.txt文件,这些文件起初似乎是我在寻找的东西,但它们似乎与索引号不匹配.我找到的文件中有752种颜色(大多数是重复的,所以说376种,仍然比256种还多).此外,还有50种灰色阴影(如果您计算重复数,则为100种阴影),但是Ansi索引的颜色只有23种,因此看起来并不相同.如果这些在某种程度上确实包含了ansi扩展颜色索引中的颜色,那么有人会列出哪个名称属于哪个索引吗?

I found some references to X11 and an rgb.txt file which at first seemed like it was what I was looking for but they don't seem to match up to the index numbers. There are 752 colors in the file I found (most are duplicates so say 376 which is still more the 256). Also there are 50 shades of gray (100 if you count duplicates) but the Ansi indexed colors have 23 so it doesn't seem to be the same. If these in some way do contain the colors in the ansi extended color index does anyone have a list of which names are in which index?

PS.我知道esc [38; 2; r; g:bm可以使用rgb值设置颜色,但我无法在Mac上使用它,并且我对索引颜色的默认值更感兴趣.

PS. I know esc[38;2;r;g:bm can supposedly set a color using rgb values but I can't get it to work on my Mac and I am more interested in the default values for the indexed colors.

某些网站:(由于回复率低,只能发布2个?但是我检查了很多网站)

Some sites: (can only post 2 due to low rep? But I checked a lot of sites)

此颜色具有标准颜色的rgb,但没有扩展颜色的rgb.

推荐答案

256色表及其分区

256色终端的颜色范围包括4个部分,通常是5个部分,在这种情况下,您实际上可以得到258种颜色:

The 256 color table and its partitioning

The color range of a 256 color terminal consists of 4 parts, often 5, in which case you actually get 258 colors:

  1. 颜色编号0到7是默认的端子颜色,其实际RGB值尚未标准化,可以经常配置.

  1. Color numbers 0 to 7 are the default terminal colors, the actual RGB value of which is not standardized and can often be configured.

颜色数字8到15是明亮"颜色.在大多数情况下,这些是带有指数-8的浅色阴影.它们也不是标准化的,通常可以进行配置.根据终端和外壳的不同,它们通常代替粗体或与粗体一起使用.

Color numbers 8 to 15 are the "bright" colors. Most of the time these are a lighter shade of the color with index - 8. They are also not standardized and can often be configured. Depending on terminal and shell, they are often used instead of or in conjunction with bold font faces.

颜色编号16到231是RGB颜色.这216种颜色由三个RGB轴上每个轴上的6个值定义.也就是说,每种颜色都只能使用0-5范围的值,而不是0-255的值.

Color numbers 16 to 231 are RGB colors. These 216 colors are defined by 6 values on each of the three RGB axes. That is, instead of values 0 - 255, each color only ranges from 0 - 5.

然后按以下方式计算色号:

The color number is then calculated like this:

number = 16 + 36 * r + 6 * g + b

rgb的范围为0-5.

with r, g and b in the range 0 - 5.

232到255的颜色数字是灰度的,从暗到亮都有24种灰度.

The color numbers 232 to 255 are grayscale with 24 shades of gray from dark to light.

前景色和背景色的默认颜色.在许多终端中,它们可以独立于256种索引颜色进行配置,从而提供另外两种可配置颜色.当您未设置任何其他颜色或禁用其他颜色(即print '\e[m')时,就会得到它们.

The default colors for foreground and background. In many terminals they can be configured independently from the 256 indexed colors, giving an additional two configurable colors . You get them when not setting any other color or disabling other colors (i.e. print '\e[m').

某些来源:

  • urxvt联机帮助页:

除了默认的前景色和背景色之外,urxvt最多可以显示88/256色:8种ANSI颜色以及相同的高强度(可能是粗体/闪烁)版本,以及72种(或在256色模式下为240种) )以4x4x4(或6x6x6)彩色RGB多维数据集加上8(24)彩色灰度渐变排列.

In addition to the default foreground and background colours, urxvt can display up to 88/256 colours: 8 ANSI colours plus high-intensity (potentially bold/blink) versions of the same, and 72 (or 240 in 256 colour mode) colours arranged in an 4x4x4 (or 6x6x6) colour RGB cube plus a 8 (24) colour greyscale ramp.

  • xterm联机帮助页:

  • xterm manpage:

    这些指定256色扩展名的颜色.默认资源值是将颜色16到231设置为6x6x6彩色立方体,将颜色232到255设置为灰度渐变.

    These specify the colors for the 256-color extension. The default resource values are for colors 16 through 231 to make a 6x6x6 color cube, and colors 232 through 255 to make a grayscale ramp.

  • 有关ANSI转义码的Wikipedia文章(反过来,该文章本身也缺少引文主题)

  • Wikipedia article on ANSI escape codes (which in turn itself is lacking a citation on the topic)

    理论上,为了获得均匀分布的颜色范围,可以按以下方式计算范围为16-231的颜色的RGB值:

    Theoretically, in order to get an equally distributed range of colors, the RGB values for the colors in the range 16 - 231 could be calculated like this:

    # example in Python: // is integer divison, % is modulo
    rgb_R = ((number - 16) // 36) * 51
    rgb_G = (((number - 16) % 36) // 6) * 51
    rgb_B = ((number - 16) % 6) * 51
    

    但是似乎实际方法有所不同:

    But it seems that the actual method is different:

    我测试过的任何终端仿真器似乎都遵循XTerm,并将红色,绿色和蓝色的值[0, 1, 2, 3, 4, 5]映射到RGB颜色轴上的值[0, 95, 135, 175, 215, 255]. (我用XTerm(297)URxvt(v9.19),ROXTerm(2.8.1),gnome-terminal(3.6.2)和xfce4-terminal(0.6.3)进行了测试)

    Any terminal emulators I tested seems to follow XTerm and map the values [0, 1, 2, 3, 4, 5] for red, green and blue to the values [0, 95, 135, 175, 215, 255] on the RGB color axes. (I tested with XTerm (297) URxvt (v9.19), ROXTerm (2.8.1), gnome-terminal (3.6.2) and xfce4-terminal (0.6.3))

    可以使用以下算法计算给定索引的RGB值:

    The RGB values for a given index can be calculated with this algorithm:

    # example in Python: 'a = b if c else d' is 'a = (c) ? b : d` in C, Perl, etc.
    index_R = ((number - 16) // 36)
    rgb_R = 55 + index_R * 40 if index_R > 0 else 0
    index_G = (((number - 16) % 36) // 6)
    rgb_G = 55 + index_G * 40 if index_G > 0 else 0
    index_B = ((number - 16) % 6)
    rgb_B = 55 + index_B * 40 if index_B > 0 else 0
    

    灰度似乎遵循以下简单公式:

    The grayscale seems to follow this simple formula:

    rgb_R = rgb_G = rgb_B = (number - 232) * 10 + 8
    

    XTerm源(版本313)的根中的

    256colres.pl 使用类似的算法来生成256colres.h,其中包含256色模式的颜色定义:

    256colres.pl in the root of the XTerm sources (version 313) uses a similar algorithm to generate 256colres.h, which contains the color definitions for 256 color mode:

    $line1="COLOR_RES(\"%d\",";
    $line2="\tscreen.Acolors[%d],";
    $line3="\tDFT_COLOR(\"rgb:%2.2x/%2.2x/%2.2x\")),\n";
    
    # colors 16-231 are a 6x6x6 color cube
    for ($red = 0; $red < 6; $red++) {
        for ($green = 0; $green < 6; $green++) {
        for ($blue = 0; $blue < 6; $blue++) {
            $code = 16 + ($red * 36) + ($green * 6) + $blue;
            printf($line1, $code);
            printf($line2, $code);
            printf($line3,
               ($red ? ($red * 40 + 55) : 0),
               ($green ? ($green * 40 + 55) : 0),
               ($blue ? ($blue * 40 + 55) : 0));
        }
        }
    }
    
    # colors 232-255 are a grayscale ramp, intentionally leaving out
    # black and white
    $code=232;
    for ($gray = 0; $gray < 24; $gray++) {
        $level = ($gray * 10) + 8;
        $code = 232 + $gray;
        printf($line1, $code);
        printf($line2, $code);
        printf($line3,
           $level, $level, $level);
    }
    


    在终端中显示可用的颜色

    这是zsh函数,可在256色终端上打印所有颜色(如果TERM设置为256色值):


    Showing available colors in a terminal

    Here is a zsh function that prints all colors on a 256 color terminal (if TERM is set to a 256 color value):

    function termcolors () 
    {
        print TERM
        print -P "Foreground: >█<"
        print -P "Background: >%S█%s<\n"
    
        print "      0 1 2 3 4 5 6 7" 
        for b (0 1)
        do
            printf "%d %2d " $b $(( 8 * b ))
            for r (0 1 2 3 4 5 6 7)
            do
                c=$(( 8 * b + r ))
                print -nP "%K{$c}  %k"
            done
            printf " %2d\n" $(( 8 * b + 7 ))
        done
    
        print
    
        print RGB
        for r (0 1 2 3 4 5)
        do 
            print "$r $(( 16 + 36 * r )) - $(( 16 + 36 * r + 35 ))\n       0 1 2 3 4 5"
            for g (0 1 2 3 4 5)
            do
                printf "%d %3d " $g $(( 16 + 36 * r + 6 * g ))
                for b (0 1 2 3 4 5)
                do
                    c=$(( 16 + 36 * r + 6 * g + b ))
                    print -nP "%K{$c}  %k"
                done
                printf " %3d\n" $(( 16 + 36 * r + 6 * g + 5))
            done
            print
        done
    
        print
    
        print GRAY
        for g in $(seq 0 23)
        do
            c=$(( 232 + g ))
            printf "%2d %3d " $g $c
            print -P "%K{$c}  %k"
        done
    }
    


    在运行时更改RGB值

    在某些终端(至少为xtermgnome-terminaltermiteurxvt)中,可以通过发送以下


    Changing RGB values during runtime

    In some terminals (at least xterm, gnome-terminal, termite and urxvt) all those colors can be changed during runtime by sending one of the following XTerm Control Sequences:

    OSC 4; c ; spec BEL
    OSC 4; c ; spec ST
    

    其中:

    • OSC是转义字符(\e\033),后跟]
    • c是色号(0-255)
    • spec是一种颜色规范(例如red#ff0000rgb:ff/00/00rgbi:1/0/0-实际起作用的内容可能取决于终端)
    • BEL是铃声字符(\a\007)
    • ST是字符串终止符\e\\\033\\
    • OSC is the escape character (\e or \033) followed by ]
    • c is the color number (0 - 255)
    • spec is a color specification (e.g. red, #ff0000, rgb:ff/00/00, rgbi:1/0/0 - what actually works might depend on the terminal)
    • BEL is the bell character (\a or \007)
    • ST is the string terminator \e\\ or \033\\

    这些控制序列可以通过简单地用echo打印它们来发送:

    These control sequences can be sent by simply printing them with echo:

    echo -en "\e]4;COLOR;SPEC\a"
    echo -en "\e]4;COLOR;SPEC\a"
    

    例如,为了将颜色数字5(通常为洋红色阴影)设置为红色,以下两种方法均应起作用:

    For example, in order to set color number 5 (usually some shade of magenta) to red, either of these should work:

    echo -en "\e]4;5;red\a"
    echo -en "\e]4;5;#ff0000\e\\"
    echo -en "\033]4;5;rgb:ff/00/00\007"
    

    可以使用控制序列之一将这些颜色重置为其(配置的)默认值

    Those colors can be reset to their (configured) default with one of the control sequences

    OSC 104 ; c BEL
    OSC 104 ; c ST
    

    因此,以下循环会将所有颜色从0重置为255为其默认值:

    So the following loop will reset all colors from 0 to 255 to their configured or default value:

    for c in {0..255}; do
      echo -en "\e]104;$c\a"
    done
    

    对于默认的前景色和背景色,控制顺序分别为OSC 10 ; spec BELOSC 11 ; spec BEL.例如:

    For the default foreground and background colors the control sequences are OSC 10 ; spec BEL and OSC 11 ; spec BEL, respectively. For example:

    echo -en "\e]10;red\a"
    echo -en "\e]11;green\a"
    

    可以分别用OSC 110 BELOSC 111 BEL重置它们:

    Those can be reset with OSC 110 BEL and OSC 111 BEL respectively:

    echo -en "\e]110\a"
    echo -en "\e]111\a"
    

    这篇关于Ansi扩展颜色索引(17-255)中颜色的RGB值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆