Unix Programming - Another ncurses question

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > November 2005 > Another ncurses question





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Another ncurses question
Simon Elliott

2005-11-09, 5:53 pm

Looking at the man pages and curses.h, it seems that there are only 8
colours available:

#define COLOR_BLACK 0
#define COLOR_RED 1
#define COLOR_GREEN 2
#define COLOR_YELLOW 3
#define COLOR_BLUE 4
#define COLOR_MAGENTA 5
#define COLOR_CYAN 6
#define COLOR_WHITE 7

Is there any way to display any other colours?

--
Simon Elliott http://www.ctsn.co.uk
Jordan Abel

2005-11-09, 5:53 pm

On 2005-11-09, Simon Elliott <Simon> wrote:
> Looking at the man pages and curses.h, it seems that there are only 8
> colours available:
>
> #define COLOR_BLACK 0
> #define COLOR_RED 1
> #define COLOR_GREEN 2
> #define COLOR_YELLOW 3
> #define COLOR_BLUE 4
> #define COLOR_MAGENTA 5
> #define COLOR_CYAN 6
> #define COLOR_WHITE 7
>
> Is there any way to display any other colours?


bold for foreground, you're SOL for background. [try blink, if you
dare.]

ncurses provides extensions that allow for more colors.
Simon Elliott

2005-11-09, 5:53 pm

On 09/11/2005, Jordan Abel wrote:

>
> bold for foreground, you're SOL for background. [try blink, if you
> dare.]
>
> ncurses provides extensions that allow for more colors.


Thanks. Looking at the man pages again, I now see that I can change the
RGB With init_color(), but still seem to be stuck with only 8 colours.

--
Simon Elliott http://www.ctsn.co.uk
Thomas Dickey

2005-11-10, 7:51 am

Simon Elliott <Simon at ctsn.co.uk> wrote:
> On 09/11/2005, Jordan Abel wrote:


[vbcol=seagreen]
> Thanks. Looking at the man pages again, I now see that I can change the
> RGB With init_color(), but still seem to be stuck with only 8 colours.


no - only certain terminals support the feature used in init_color().
That doesn't define _more_ colors, but allows changing the values for
a palette.

ncurses normally provides coding for 16 colors, and can be compiled to support
256 colors. Only certain terminals (such as xterm) support the larger
numbers of colors.

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
Simon Elliott

2005-11-10, 6:04 pm

On 10/11/2005, Thomas Dickey wrote:

>
> no - only certain terminals support the feature used in init_color().
> That doesn't define more colors, but allows changing the values for
> a palette.


OK

> ncurses normally provides coding for 16 colors, and can be compiled
> to support 256 colors. Only certain terminals (such as xterm)
> support the larger numbers of colors.


I'm probably missing something blindingly obvious here, but I can't see
how to get 16 colours when there seems to be only these colours
availabe in my curses.h:

#define COLOR_BLACK 0
#define COLOR_RED 1
#define COLOR_GREEN 2
#define COLOR_YELLOW 3
#define COLOR_BLUE 4
#define COLOR_MAGENTA 5
#define COLOR_CYAN 6
#define COLOR_WHITE 7


--
Simon Elliott http://www.ctsn.co.uk
Thomas Dickey

2005-11-10, 6:04 pm

Simon Elliott <Simon at ctsn.co.uk> wrote:

> I'm probably missing something blindingly obvious here, but I can't see
> how to get 16 colours when there seems to be only these colours
> availabe in my curses.h:


....

Those are numbers 0 to 7, for the "ANSI" (actually ISO-6429) colors.
The start_color() manpage notes that the number of colors which the
terminfo claims that the terminal supports is given in the variable
COLORS. It's ok to use values up to (not including) that.

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
Brian Raiter

2005-11-10, 6:04 pm

>> ncurses normally provides coding for 16 colors, and can be compiled
>
> I'm probably missing something blindingly obvious here, but I can't
> see how to get 16 colours when there seems to be only these colours
> availabe in my curses.h:


Those numbers are just indexes into the default color palette --
i.e. the palette that ncurses sets up when you call start_color(). If
you define a different palette, using init_color(), then you'll be
using your own indexes instead of (or in addition to) the eight that
are predefined.

Don't forget to call can_change_color() before doing any of this.
Plenty of terminals exist that don't support arbitrary color
assignment. (I presume you're already calling has_colors() before
that.)

If you haven't already, man curs_color.

b
Simon Elliott

2005-11-14, 5:56 pm

On 10/11/2005, Thomas Dickey wrote:

> Those are numbers 0 to 7, for the "ANSI" (actually ISO-6429) colors.
> The start_color() manpage notes that the number of colors which the
> terminfo claims that the terminal supports is given in the variable
> COLORS. It's ok to use values up to (not including) that.


Thanks. My xterm does this:

has_colors() : TRUE
can_change_color() : FALSE
COLORS : 8
COLOR_PAIRS : 64

so looks like on that terminal at any rate I'm out of luck!

--
Simon Elliott http://www.ctsn.co.uk
Thomas Dickey

2005-11-15, 7:55 am

Simon Elliott <Simon at ctsn.co.uk> wrote:

> so looks like on that terminal at any rate I'm out of luck!


ncurses doesn't _know_ what the terminal does.
You tell ncurses what it does based on the terminfo description.
(I don't recall which terminal emulator you're using).

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
Jordan Abel

2005-11-15, 7:55 am

On 2005-11-15, Thomas Dickey <dickey@saltmine.radix.net> wrote:
> Simon Elliott <Simon at ctsn.co.uk> wrote:
>
>
> ncurses doesn't _know_ what the terminal does.
> You tell ncurses what it does based on the terminfo description.
> (I don't recall which terminal emulator you're using).


Also, on many terminals, setting the bold attribute picks a different
color than without it [i.e. dark grey instead of black, yellow instead
of brown, etc]
Simon Elliott

2005-11-15, 5:59 pm

On 15/11/2005, Jordan Abel wrote:

> On 2005-11-15, Thomas Dickey <dickey@saltmine.radix.net> wrote:
>
> Also, on many terminals, setting the bold attribute picks a different
> color than without it [i.e. dark grey instead of black, yellow instead
> of brown, etc]


Thanks - I'll play with that on a few terminals and see what it looks
like.

One of the apps I want to use (n)curses in keeps wanting to change to
different sets of colours. Would it be reasonable to call
init_pair(1, ...)
each time round rather than have lots of pairs set up at the beginning?

--
Simon Elliott http://www.ctsn.co.uk
Thomas Dickey

2005-11-15, 5:59 pm

Simon Elliott <Simon at ctsn.co.uk> wrote:

> One of the apps I want to use (n)curses in keeps wanting to change to
> different sets of colours. Would it be reasonable to call
> init_pair(1, ...)
> each time round rather than have lots of pairs set up at the beginning?


It doesn't make much difference, since it's an in-memory table.
(If you redefine a color pair, ncurses will modify the display as
needed to reflect the new mapping).

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
Simon Elliott

2005-11-15, 5:59 pm

On 15/11/2005, Thomas Dickey wrote:

> Simon Elliott <Simon at ctsn.co.uk> wrote:
>
>
> It doesn't make much difference, since it's an in-memory table.
> (If you redefine a color pair, ncurses will modify the display as
> needed to reflect the new mapping).


I see what you mean!

init_pair(1, COLOR_GREEN, COLOR_BLACK);
wattrset(stdscr, COLOR_PAIR(1));
printw("GREEN/BLACK\n"); /* Displays red/black actually! */
init_pair(1, COLOR_RED, COLOR_BLACK);
wattrset(stdscr, COLOR_PAIR(1));
printw("RED/BLACK\n");
refresh();

Both the lines displayed with printw() are actually displayed
red/black. This isn't what I expected to see at all.

It seems that with curses it helps a lot to know in advance what
colours an application is planning on using. I can't even keep adding
new colour pairs as it seems that I've only got 64 of them to play
with. Is there any sensible way for the app to "make up its colours as
it goes along"?

--
Simon Elliott http://www.ctsn.co.uk
Jordan Abel

2005-11-15, 5:59 pm

On 2005-11-15, Simon Elliott <Simon> wrote:
> On 15/11/2005, Thomas Dickey wrote:
>
>
> I see what you mean!
>
> init_pair(1, COLOR_GREEN, COLOR_BLACK);
> wattrset(stdscr, COLOR_PAIR(1));
> printw("GREEN/BLACK\n"); /* Displays red/black actually! */
> init_pair(1, COLOR_RED, COLOR_BLACK);
> wattrset(stdscr, COLOR_PAIR(1));
> printw("RED/BLACK\n");
> refresh();
>
> Both the lines displayed with printw() are actually displayed
> red/black. This isn't what I expected to see at all.
>
> It seems that with curses it helps a lot to know in advance what
> colours an application is planning on using. I can't even keep adding
> new colour pairs as it seems that I've only got 64 of them to play
> with. Is there any sensible way for the app to "make up its colours as
> it goes along"?


How many color pairs do you plan on using? I have 10 on the screen in
irssi, six now [across the whole buffer] in vim.
Simon Elliott

2005-11-17, 6:13 pm

On 15/11/2005, Jordan Abel wrote:

>
> How many color pairs do you plan on using? I have 10 on the screen in
> irssi, six now [across the whole buffer] in vim.


'Plan' is the operative word here: I don't want to have to 'plan' on
using any given colour pairs at the start of the app. I want to use
them on demand.

This is because I want the display part of my app to be completely
separate from the functional part. The functional part knows nothing of
curses (or whatever is being used to draw stuff on the screen). The
display part knows nothing of the functional part. I don't want the
functional part to be constrained to know about its colours at the
beginning, even if these are defined in the spec.

For one thing, the functional part may be designed and coded by another
developer. For another thing, some of the functional modules have been
ported from MSDOS, where the developer expects to be able to place
different attributes into the video buffer for each character whiuch
appears on the screen, without setting anything up at the start of the
app.

The only sensible way I can see of implementing this is for the display
module to cache all the colours it's been asked to display. If it's
been asked to display that colour before, it uses the COLOR_PAIR index.
If it's never been asked to display that colour before, it calls
init_pair().

--
Simon Elliott http://www.ctsn.co.uk
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com