2017-10-06 85 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. –