old-www/HOWTO/NCURSES-Programming-HOWTO/color.html

343 lines
6.3 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Colors</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE=" NCURSES Programming HOWTO "
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="Windows"
HREF="windows.html"><LINK
REL="NEXT"
TITLE="Interfacing with the key board"
HREF="keys.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>NCURSES Programming HOWTO</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="windows.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="keys.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="COLOR"
></A
>10. Colors</H1
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="COLORBASICS"
></A
>10.1. The basics</H2
><P
>Life seems dull with no colors. Curses has a nice mechanism to handle colors.
Let's get into the thick of the things with a small program.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="BSICO"
></A
><P
><B
>Example 9. A Simple Color example </B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
><SPAN
CLASS="INLINEMEDIAOBJECT"
>#include &#60;ncurses.h&#62;
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string);
int main(int argc, char *argv[])
{ initscr(); /* Start curses mode */
if(has_colors() == FALSE)
{ endwin();
printf("Your terminal does not support color\n");
exit(1);
}
start_color(); /* Start color */
init_pair(1, COLOR_RED, COLOR_BLACK);
attron(COLOR_PAIR(1));
print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ...");
attroff(COLOR_PAIR(1));
getch();
endwin();
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
mvwprintw(win, y, x, "%s", string);
refresh();
}
</SPAN
></PRE
></FONT
></TD
></TR
></TABLE
></DIV
><P
>As you can see, to start using color, you should first call the function
<TT
CLASS="LITERAL"
> start_color()</TT
>. After that, you can use color
capabilities of your terminals using various functions. To find out whether a
terminal has color capabilities or not, you can use
<TT
CLASS="LITERAL"
>has_colors()</TT
> function, which returns FALSE if
the terminal does not support color. </P
><P
>Curses initializes all the colors supported by terminal when start_color() is
called. These can be accessed by the define constants like
<TT
CLASS="LITERAL"
>COLOR_BLACK </TT
> etc. Now to actually start using
colors, you have to define pairs. Colors are always used in pairs. That means
you have to use the function <TT
CLASS="LITERAL"
>init_pair() </TT
> to
define the foreground and background for the pair number you give. After that
that pair number can be used as a normal attribute with <TT
CLASS="LITERAL"
>COLOR_PAIR()</TT
>function. This may seem to be cumbersome at first.
But this elegant solution allows us to manage color pairs very easily. To
appreciate it, you have to look into the the source code of "dialog", a utility
for displaying dialog boxes from shell scripts. The developers have defined
foreground and background combinations for all the colors they might need and
initialized at the beginning. This makes it very easy to set attributes just by
accessing a pair which we already have defined as a constant.</P
><P
>The following colors are defined in <TT
CLASS="LITERAL"
>curses.h</TT
>.
You can use these as parameters for various color functions.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
> COLOR_BLACK 0
COLOR_RED 1
COLOR_GREEN 2
COLOR_YELLOW 3
COLOR_BLUE 4
COLOR_MAGENTA 5
COLOR_CYAN 6
COLOR_WHITE 7</PRE
></FONT
></TD
></TR
></TABLE
></P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="CHANGECOLORDEFS"
></A
>10.2. Changing Color Definitions</H2
><P
>The function <TT
CLASS="LITERAL"
>init_color()</TT
>can be used to change
the rgb values for the colors defined by curses initially. Say you wanted to
lighten the intensity of red color by a minuscule. Then you can use this
function as</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
> init_color(COLOR_RED, 700, 0, 0);
/* param 1 : color name
* param 2, 3, 4 : rgb content min = 0, max = 1000 */</PRE
></FONT
></TD
></TR
></TABLE
><P
>If your terminal cannot change the color definitions, the function returns ERR.
The function <TT
CLASS="LITERAL"
>can_change_color()</TT
> can be used to
find out whether the terminal has the capability of changing color content or
not. The rgb content is scaled from 0 to 1000. Initially RED color is defined
with content 1000(r), 0(g), 0(b). </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="COLORCONTENT"
></A
>10.3. Color Content</H2
><P
>The functions <TT
CLASS="LITERAL"
>color_content()</TT
> and
<TT
CLASS="LITERAL"
>pair_content()</TT
> can be used to find the color
content and foreground, background combination for the pair. </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="windows.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="keys.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Windows</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Interfacing with the key board</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>