2017-10-06 68 views
2

如何在ncurses上获得带有黑色文字的明亮白色背景,与nano中的标题栏类似?尽管出现the advice in another question(这与在黑色背景上获得明亮的白色文字有关,与我想达到的目的相反),但我仍然可以实现这一目标,这是一种丑陋的米色背景。如何使用ncurses获得明亮的白色背景色?

图片:

GNU nano的标题栏,我想要的。

enter image description here

我得到下面的程序是什么。 (与gcc -lncursesw -I/usr/include minimal_example.c建设)

enter image description here

#include <locale.h> 
#include <ncurses.h> 

int main() { 
    setlocale(LC_ALL, ""); 
    // Initialize curses library 
    initscr(); 
    // Enable colors 
    start_color(); 
    // Attempt recommendation in https://stackoverflow.com/questions/1896162/how-to-get-a-brightwhite-color-in-ncurses and other places on the web 
    use_default_colors(); 
    // Make the COLOR_PAIR 0xFF refer to a white foreground, black background window. 
    // Using -1 will not work in my case, because I want the opposite of the default (black text on white bg), not the default (white text on black bg). 
    init_pair(0xFF, COLOR_BLACK, COLOR_WHITE); 
    refresh(); 
    // Get our term height and width. 
    int x; 
    int y; 
    // & not required because this is a macro 
    getmaxyx(stdscr, y, x); 
    // Create a new window. 
    // TODO: Resize the window when the term resizes. 
    WINDOW *window = newwin(y,x,0,0); 
    // Try some other attributes recommended online, no dice. Putting this after the call to wbkgd() just makes the text look strange, does not change the background. 
    wattron(window,A_BOLD|A_STANDOUT); 
    // Set window color. 
    wbkgd(window, COLOR_PAIR(0xff)); 
    // Draw a nice box around the window. 
    box(window, 0, 0); 
    // Write some text. 
    mvwprintw(window, 1, 1, "背景:不白"); 
    wrefresh(window); 

    // Wait for keypress to exit. 
    getch(); 
    // De-initialize ncurses. 
    endwin(); 
    return 0; 
} 

我想也许有什么错我的终端配置(termite),但是我能够重现xfce4-terminalxterm的问题,无论是使用默认配置。解决这个问题的唯一方法是将我的color7color15设置为与​​相同的颜色,显然我不想这样做,因为这是非标准的,我想分发使用此代码的较大应用程序。

(xfce4-终端有bug) enter image description here

+0

对于几乎所有终端(包括您在屏幕截图中显示的那个终端),您引用的答案都不正确。 –

回答

1

我的建议是定义明亮的颜色(9至15),如果至少有16种颜色,can_change_color()返回true。否则回落到非鲜艳的颜色:

#include <stdlib.h> 
#include <locale.h> 
#include <ncurses.h> 

#define PAIR_BW  1 
#define BRIGHT_WHITE 15 

int main(void) { 
    int rows, cols; 

    setlocale(LC_ALL, ""); 
    initscr(); 

    start_color(); 
    use_default_colors(); 
    if (can_change_color() && COLORS >= 16) 
     init_color(BRIGHT_WHITE, 1000,1000,1000); 

    if (COLORS >= 16) { 
     init_pair(PAIR_BW, COLOR_BLACK, BRIGHT_WHITE); 
    } else { 
     init_pair(PAIR_BW, COLOR_BLACK, COLOR_WHITE); 
    } 

    refresh(); 
    getmaxyx(stdscr, rows, cols); 
    WINDOW *window = newwin(rows,cols,0,0); 
    wbkgd(window, COLOR_PAIR(PAIR_BW)); 
    box(window, 0, 0); 
    mvwprintw(window, 1, 1, "背景:不白"); 
    wrefresh(window); 

    getch(); 
    endwin(); 
    return EXIT_SUCCESS; 
} 

这是测试在Gnome终端3.18.3和的XTerm 322的工作,它应该如使用ncursesw(在所有颜色能力的终端工作,虽然有些怪异的人您仍可能会看到非亮白色的背景)。

+0

您好, 您的答案对我来说似乎最好,我只有一个问题。为什么在同一个语句中'can_change_color()'和'COLORS> = 16'?在我看来,最好是有一个if/else if/else,所以'if(can_change_color()){do this; } else if(COLORS> = 15){使用BRIGHT_WHITE而不是COLOR_WHITE} else {使用COLOR_WHITE}'。没有?这看起来像它会支持我想要的大多数终端,也就是说,明亮的白色,即使是不能改变颜色,但在15有明亮白色的那些。 –

+0

@FredrickBrennan:实际上,我认为:这样做更有意义。它也使颜色在大多数终端中正常工作,包括那些不支持颜色变化的终端。 (尽管诅咒不会通过命名来标准化颜色8-16,但实际的颜色几乎是标准的;参见例如[颜色支持部分](https://en.wikipedia.org/wiki/ANSI_colors#Colors)维基百科关于ANSI转义序列的文章,在我看来,实用性在这里最重要。)谢谢,这真是一个很好的建议! –

-2

终端模拟器的颜色有点乱。你的问题是,根据你的终端,灰色背景真的是“白色”!看看这张表Wikipedia。看看在所有不同的终端仿真器上,“白色”行是多么的灰色?你真正想要的是“亮白”,超过了8种初始颜色。问题是,根据curses

一些终端支持超过八(8)“ANSI”的颜色。 对于这些附加颜色没有标准名称。

所以,你只需要使用它们的数量,并希望每个人的终端符合维基百科上的表格(我认为大部分都是这样)。

init_pair(0xFF, COLOR_BLACK, COLORS > 15 ? 15 : COLOR_WHITE); 

这就是你所需要的,这样你就可以摆脱所有其他use_default_colorsA_BOLD东西。


老答案:

curs_color手册说

注意,通过颜色对设置一个隐含的背景颜色影响到一个字符写入操作明确只接触字符元。

...

A_BLINK属性理论上应该使背景变亮。

事实上,如果你只需要改变

wbkgd(window, COLOR_PAIR(0xff) | A_BLINK); 

你得到一个明亮的白色背景,但只有所在的地区文本绘制(包括窗口边框)。我不知道如何在整个窗口背景中获得相同的效果,但希望这可以让你开始。

+0

-2并且没有评论?这个答案正确地标识了(非标准的)亮白色,并且链接到维基百科关于扩展终端颜色的文章。 – Max

0

这种变化(假设TERM=xterm-256color)做什么有人问:

> diff -u foo.c foo2.c 
--- foo.c  2017-10-06 15:59:56.000000000 -0400 
+++ foo2.c  2017-10-06 16:10:11.893529758 -0400 
@@ -7,10 +7,9 @@ 
    initscr(); 
    // Enable colors 
    start_color(); 
- // Attempt recommendation in https://stackoverflow.com/questions/1896162/how-to-get-a-brightwhite-color-in-ncurses and other places on the web 
- use_default_colors(); 
- // Make the COLOR_PAIR 0xFF refer to a white foreground, black background window. 
- // Using -1 will not work in my case, because I want the opposite of the default (black text on white bg), not the default (white text on black bg). 
+ // redefine colors, using the initc capability in TERM=xterm-256color 
+ init_color(COLOR_BLACK, 0, 0, 0); 
+ init_color(COLOR_WHITE, 999, 999, 999); 
    init_pair(0xFF, COLOR_BLACK, COLOR_WHITE); 
    refresh(); 
    // Get our term height and width. 

nano没有做到这一点。您可能会发现阅读其source-code很有帮助,以便通过使用终端的默认颜色来解决问题。

+0

我的答案是非常相同的路径,除了可以检查终端是否能够使用'can_change_color()'重新定义颜色,并且最大颜色分量是1000而不是999. –