Hello, world! — with colour
This program extends "Hello, world!" by outputting the phrase a number of times, with different foreground and background colors.
Programming Issues
We must call start_color()
to initialze colour
operations in ncurses, before using any of the colour handling
library functions. After doing this, we can call has_color()
to find out whether the terminal in use supports colour at all.
To change color, we need to call color_set()
and
specify a "color pair" to use. A color pair simply consists
of a foreground and background color. ncurses has a limited number
of color pairs available (you can find out how many by checking the
value of the constant COLOR_PAIRS
).
In this program, we set up a number of color pairs using the
init_pair()
function together with the predefined
color constants ncurses supplies (e.g. COLOR_BLACK, COLOR_GREEN).
Once we have defined these, we loop through them, using
color_set()
to switch between pairs, printing the
"Hello, world!" phrase in each one as we go.
Usage
Link with the ncurses library. The ncurses "Hello, World!" example includes details on how to do this.