views:

368

answers:

1

This is a tale of two scripts and is related to a previous question.

The two scripts are at http://gist.github.com/50692. The ansi.rb script displays all 256 colors on all 256 background colors. The ncurses.rb script displays all 256 foreground colors but the background displays the basic 16 and then seems to cycle through various attributes like blinking and reverse video.

So what gives? Is this the bug in ncurses that it uses a signed integer for color pairs? (ie 'tput colors' says 256 but 'tput pairs' says 32767 instead 65536) It seems like if that were the case the first half of the colors pairs would display properly but the second half would repeat or get into the attributes as the int wraps.

+1  A: 

I don't know Ruby at all, so can't provide a working example, but the ncurses.rb script should tell you something in that it's so much shorter than the ansi.rb script.

You're not setting up the colours, so it's just looping the default 16 colour palette with variations provided by the attributes such as blink underline, bold, etc.

You need to use int init_color(short color, short r, short g, short b) to initialize a color index with the RGB values (0 - 1000) and then set the colour pairs to use for display with int init_pair(short pair, short f, short b) before calling COLOR_PAIR(n).

For portability you should check bool has_colors(void) and bool can_change_color(void)

On my system the man pages for ncurses are invaluable.

James Morris