【问题标题】:Having trouble with defining color values using init_color(); in ncurses in c使用 init_color() 定义颜色值时遇到问题;在 c 中的 ncurses
【发布时间】:2019-09-02 16:57:58
【问题描述】:

我在 C 的 ncurses 中使用 init_color(); 来尝试定义新的 RGB 颜色值。但是,init_color(); 在我运行程序后不会生效并更改默认颜色。

我尝试在init_pair();start_color(); 前后移动init_color(); 语句,但没有运气。我还尝试使用不同的值(ASCII 和其他来源的数字)代替 ex。 COLOR_MAGENTA,对于 init_color(); 语句之一中的第一个参数,但也没有运气。我的start_color();init_color();init_pair(); 语句都在程序其余部分之前的主函数中。我的终端(使用 cloud9/cs50)支持 256 色(使用终端命令检查)。此外,所有颜色定义都在函数“main”之上。

int main(int argc, char *argv[])
{
    // ensure that number of arguments is as expected
    if (argc != 1)
    {
      fprintf(stderr, "Usage: ./lemonade\n");
      return 1;
    }

// start up ncurses
if (!startup())
{
    fprintf(stderr, "Error starting up ncurses\n");
    return 2;
}

// initialize colors
start_color();

// re-asign specific RGB value to colors
init_color(COLOR_MAGENTA, 254, 160, 207);
init_color(COLOR_GREEN, 37, 244, 82);
init_color(COLOR_BLUE, 96, 82, 186);

// used cyan for a different greeen
init_color(COLOR_CYAN, 46, 243, 74);

// used yellow for a grey
init_color(COLOR_YELLOW, 156, 156, 156);

// used red for a purple
init_color(COLOR_RED, 208, 196, 253);

// initilaize color pairs
init_pair(LOGO_PAIR, COLOR_MAGENTA, COLOR_GREEN);
init_pair(DRAWBORDERSSPECIAL_PAIR, COLOR_BLACK, COLOR_GREEN);
init_pair(BORDERS_PAIR, COLOR_WHITE, COLOR_BLACK);
init_pair(SPECIALNEXT_PAIR, COLOR_BLACK, COLOR_GREEN);
init_pair(SUNNYBLUE_PAIR, COLOR_WHITE, COLOR_BLUE);
init_pair(WEATHERGREEN_PAIR, COLOR_WHITE, COLOR_CYAN);
init_pair(CLOUDYGREY_PAIR, COLOR_WHITE, COLOR_YELLOW);
init_pair(HOTPURPLE_PAIR, COLOR_WHITE, COLOR_RED);

// clean
clean();            // clean includes (refresh(); and clear();)

// draw borders
drawborders();

// run screen 1
screenone();





// support color test                           
mvprintw(6, 50, "My terminal supports %d colors.", COLORS);

// has_color(); test    
if (has_colors() == FALSE)
{
    mvprintw(7, 50, "Your terminal does not support color \n");
}

// can_change_color(); test     
if (can_change_color() == FALSE)
{
    mvprintw(8, 50, "Can_change_color is false \n");
}

我预计 init_color(); 语句会生效并将默认颜色(例如洋红色、黑色等)更改为新分配的特定 RGB 值,但一旦程序运行它们就会保持不变。

我添加了检查支持的颜色数量has_colors();can_change_color();。支持的颜色数返回8,has_colors();返回true,最后can_change_color();返回false。感谢您建议使用 has_colors();can_change_color(); 虽然这似乎是我不知道从这里去哪里的问题?

【问题讨论】:

标签: colors ncurses cs50 cc


【解决方案1】:

嗯,我看不到你对has_colors()can_change_color() 的调用,应该使用它们来检测你是否允许在你的系统上这样做?

这是您应该检查的第一件事。您的环境可能不允许允许颜色更改。

【讨论】:

  • 目前can_change_color();返回true,`has_color();. I used the terminal command "echo $TERM" and received 'xterm-256color'. I also used "tput colors" and received '256'. I'm not sure why the init_color();```语句不会生效,即使终端支持256色?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2012-06-04
  • 2012-03-27
  • 2020-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多