ncurses color_content() 给我错误的值

ncurses color_content() giving me the wrong values

我正在使用 xterm-256color。这是我的短程序片段:

  mvwprintw(stdscr,1,1,"You have %d colors",COLORS);
  mvwprintw(stdscr,2,1,"You have %d color pairs",COLOR_PAIRS);
  wprintw(stdscr,"\n\n");
  for (i=1;i<10;i++)
  {
    short r,g,b;
    short thiscolor=i+70;
    init_pair(i,thiscolor,COLOR_BLACK);
    color_content(thiscolor,&r,&g,&b);
    wattron(stdscr,COLOR_PAIR(i));
    wprintw(stdscr,"This is color %d\t%d %d %d\n",thiscolor,r,g,b);
    wattroff(stdscr,COLOR_PAIR(i));
  }
  refresh();

它打印出 10 种不同的绿色阴影,但是 color_content 的输出与它打印的绿色不匹配:

 You have 256 colors
 You have 256 color pairs

This is color 71        1000 1000 1000
This is color 72        0 0 0
This is color 73        1000 0 0
This is color 74        0 1000 0
This is color 75        1000 1000 0
This is color 76        0 0 1000
This is color 77        1000 0 1000
This is color 78        0 1000 1000
This is color 79        1000 1000 1000

我本来希望看到中间值 (G) 总是一个相当高的数字。我会 而不是 期望看到 0.

我是不是做错了什么?还是我误解了 color_content 应该输出什么?

ncurses 无法预知给定终端仿真器使用的调色板。除非您初始化颜色 (init_color),否则它只有 built-in table。没有用于确定终端调色板的 portable 方法。

手册中关于 start_color 的部分说

  • If the terminal supports the initc (initialize_color) capability, start_color initializes its internal table representing the red, green and blue components of the color palette.

    The components depend on whether the terminal uses CGA (aka "ANSI") or HLS (i.e., the hls (hue_lightness_saturation) capability is set). The table is initialized first for eight basic colors (black, red, green, yellow, blue, magenta, cyan, and white), and after that (if the terminal supports more than eight colors) the components are initialized to 1000.

    start_color does not attempt to set the terminal's color palette to match its built-in table. An application may use init_color to alter the internal table along with the terminal's color.

"initialized to 1000" 可能更清楚。该库使用来自 8 种 ANSI 颜色的 red/green/blue 模式作为在前 8 种颜色之后重复重复的东西(使用 1000 作为 non-zero 值)(参见 source-code...)。

这是库中内置的默认值。如果你想要不同的东西,你必须告诉它那是什么,使用 init_color. The ncurses-examples 有样本调色板数据文件,一些程序(ncurses、picsmap、savescreen)可以使用这些文件来做到这一点。