Subject: Re: colors
To: Florian St?hr <florian.stoehr@login-solutions.de>
From: Julian Coleman <jdc@coris.demon.co.uk>
List: netbsd-help
Date: 01/15/2003 14:25:54
> VT220 supports colors.
> 
> NetBSD-terminal SEEM to support em, too - but only sometimes ?!?
> 
> IRC-client "Bitch-X" produces well-colored output. But my own (n)curses programs don't.
> 
> I know that the kernel prints its messages on color (and i always change the color to cyan
> in my own kernels).
> 
> How to make those curses programs work colored ?

Not sure if you mean "which terminal types support colour?", or "how do I use
colour inside a curses application?", so I'll try and answer both.

The terminals that support colour are those that have 'Co' (colours) and 'pa'
(colour pairs) capabilities in the termcap database, plus either the 'Ab' and
'Af' or the 'Sb' and 'Sf' capabilities, to set the background and foreground
colours, repectively.  Examples are 'xterm', 'rxvt' and 'wsvt25' (wscons).  I
don't think a real vt220 does colour (or maybe it has only 4 colours), but I
could be wrong.  Certainly, our termcap entry has no colour support for the
vt220.

In order to write curses applications that use colours :

  1) check to see if the terminal supports colours
  2) initialise colours
  3) set up the colour pairs
  4) set the colour pair attribute
  5) add the characters to the window

Curses applications use colour pairs - a foreground and background colour
pair for each character on the screen.  Set this colour pair as an attribute
using the COLOR_PAIR macro and your text will have these foreground and
background colours.

The manual pages covering this are curses_color(3), curses_attributes(3),
curses_background(3) and curses_default_colors(3).  Note that the routines
in curses_default_colors(3) and the full manual pages have only appeared
after the 1.6 release.  The manual pages are available online at :

  http://netbsd.gw.com/cgi-bin/man-cgi?curses_color+3
  http://netbsd.gw.com/cgi-bin/man-cgi?curses_attributes+3
  http://netbsd.gw.com/cgi-bin/man-cgi?curses_background+3
  http://netbsd.gw.com/cgi-bin/man-cgi?curses_default_colors+3

A trivial example using colours is :

#include "curses.h"

main() {
	initscr();
	move(1, 10);
	if (has_colors()) {
		start_color();
		init_pair(1, COLOR_WHITE, COLOR_RED);
		attrset(COLOR_PAIR(1));
		addstr("Has colours - using white on red");
	} else {
		attrset(A_REVERSE);
		addstr("No colours - using reverse video");
	}
	getch();
	endwin();
}

Run this in (say) an xterm and then set the terminal type to vt100 and run it
again.

J

-- 
                    My other computer also runs NetBSD
                          http://www.netbsd.org/