【问题标题】:how to set colors in terminal using terminfo如何使用 terminfo 在终端中设置颜色
【发布时间】:2017-08-01 17:52:09
【问题描述】:

我正在尝试使用 terminfo 数据库打印颜色,并已成功解析每个终端的终端数据库文件。就我而言,我有绝对支持颜色的 gnome 终端。

现在 terminfo 数据库中有几个命令,例如 -

set_foreground
set_background
set_a_foreground
set_a_background

因为我想设置前景色,所以我选择了set_a_foreground,它表示它与 ANSI seq 兼容。但我仍然不知道如何用它们中的任何一个实际打印颜色。

他们俩在描述中都说了这样的话-Set foreground color #1,而他们的实际字符串在我的终端上看起来像这样-ESC[3%p1%dm

所以我的问题是,我应该使用set_a_set_ 版本中的哪一个以及如何使用它们打印任何颜色。

【问题讨论】:

    标签: c colors terminal terminfo


    【解决方案1】:

    set_foregroundset_a_foreground 之间的区别(以及后台功能)在terminfo(5) 手册页中的Color Handling section 中。请记住,长名称很少使用,您应该寻找 setfsetaf

       The  setaf/setab  and setf/setb capabilities take a single
       numeric argument each.  Argument values 0-7 of setaf/setab
       are  portably defined as follows (the middle column is the
       symbolic #define available in the header for the curses or
       ncurses  libraries).  The terminal hardware is free to map
       these as it likes, but  the  RGB  values  indicate  normal
       locations in color space.
    
             Color       #define       Value       RGB
             black     COLOR_BLACK       0     0, 0, 0
             red       COLOR_RED         1     max,0,0
             green     COLOR_GREEN       2     0,max,0
             yellow    COLOR_YELLOW      3     max,max,0
             blue      COLOR_BLUE        4     0,0,max
             magenta   COLOR_MAGENTA     5     max,0,max
             cyan      COLOR_CYAN        6     0,max,max
             white     COLOR_WHITE       7     max,max,max
    
       The  argument  values of setf/setb historically correspond
       to a different mapping, i.e.,
    
             Color       #define       Value       RGB
             black     COLOR_BLACK       0     0, 0, 0
             blue      COLOR_BLUE        1     0,0,max
             green     COLOR_GREEN       2     0,max,0
             cyan      COLOR_CYAN        3     0,max,max
             red       COLOR_RED         4     max,0,0
             magenta   COLOR_MAGENTA     5     max,0,max
             yellow    COLOR_YELLOW      6     max,max,0
             white     COLOR_WHITE       7     max,max,max
    
       It is important to not confuse the two sets of color capa-
       bilities;  otherwise  red/blue will be interchanged on the
       display.
    

    大多数只使用terminfo(而不是curses)的应用程序使用tparm 函数来格式化字符串,替换一个(数字)参数,然后在结果字符串上使用tputs 来实际写入它。这两个原因是填充和延迟(通常在 color 功能中找不到,但通常在 terminfo 中)。

    ncurses-examples 程序dots 使用这些函数在屏幕上随机绘制彩色单元格。 (在示例中,tparm2tparm3 是提供 tparm 原型所需的额外参数的宏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 2012-11-26
      • 2017-10-04
      • 2013-08-18
      • 2012-06-27
      • 1970-01-01
      • 2021-11-18
      相关资源
      最近更新 更多