8

In this simple program (written in C)

#include <ncurses.h>
#include <string.h>

int main()
{
 initscr();
 printw("line 1\n");
 printw("line 2\n");
 start_color();
 init_pair(1, COLOR_RED, COLOR_BLACK);
 printw("line 3");
 getch();
 endwin();

 return 0;
}

a red text is printed on the screen over a black background. But when I run the program, the background is slightly brighter than the black background of the terminal, in Linux (Gnome terminal).

I don't want to set a background color over the default, black color of the terminal: I would like to keep the terminal background and to actually set the ncurses background as transparent.

Is there a way to do this?

Note: I tried to put the function use_default_colors(); after start_color(); as suggested in this question, but it was not useful.

Community
  • 1
  • 1
BowPark
  • 1,340
  • 2
  • 21
  • 31
  • 2
    Of course there is, at least as far as the terminal emulation is concerned. You can find the corresponding ANSI-sequence here: https://en.wikipedia.org/wiki/ANSI_escape_code . You just need to set no background color at all / reset it. However, I don't know the corresponding macro name in ncurses, so that's not an answer to your question. – cmaster - reinstate monica Aug 17 '15 at 20:19

2 Answers2

7

From man init_pair:

As an extension, ncurses allows you to set color pair 0 via the assume_default_colors routine, or to specify the use of default colors (color number -1) if you first invoke the use_default_colors routine.

So, generally, if you want to use "default" color, use -1 for color value, but make sure you've called use_default_colors() first.

#include <ncurses.h>
#include <string.h>

int main()
{
 initscr();
 use_default_colors();
 printw("line 1\n");
 printw("line 2\n");
 start_color();
 init_pair(1, COLOR_RED, -1);
 printw("line 3");
 getch();
 endwin();

 return 0;
}
Pawel Veselov
  • 3,996
  • 7
  • 44
  • 62
  • 2
    As a rule, you should also first check if the terminal supports colors using [`has_colors`](http://invisible-island.net/ncurses/man/curs_color.3x.html). – Thomas Dickey Aug 17 '15 at 20:47
  • 1
    @ThomasDickey correct, but I think this was more of an SSCCE app. It just showcases the fact that the terminal background changes, it doesn't even utilize the colorpair initialized. But that's fine :) – Pawel Veselov Aug 17 '15 at 20:56
  • 1
    Also, the program as written does not show red on the last line (it lacks, for instance, `attrset`). – Thomas Dickey Aug 17 '15 at 20:59
5

For what it's worth, here is a corrected example starting with @Pawel Veselov's suggestion:

#include <ncurses.h>
#include <string.h>

int main(void)
{
 initscr();
 if (has_colors()) {
  use_default_colors();
  start_color();
  init_pair(1, COLOR_RED, -1);
 }
 printw("line 1\n");
 printw("line 2\n");
 attrset(COLOR_PAIR(1));
 printw("line 3");
 getch();
 endwin();

 return 0;
}

The last line should appear (for cooperating terminals) with red text on the terminal's default background color. (To be pedantic, one could do the attrset only when has_colors is true...).

Running in white-on-black:

enter image description here

or in black-on-white:

enter image description here

uses the terminal's default background. Without use_default_colors, ncurses assumes the terminal displays white-on-black (but then you can change that assumption using assume_default_colors).

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • 1
    thank you, it was very useful anyway. What could be the behaviour of the program if `attrset` is used in a terminal which does not support colors? – BowPark Sep 03 '15 at 19:08
  • 3
    In that case, no coloring (or adjustment for default colors) is done. – Thomas Dickey Sep 03 '15 at 22:41