Next: Mouse Handling, Previous: General Remarks, Up: Part IV Drawing Objects [Contents][Index]

### 28.2 Color Handling

As mentioned earlier, Forms Library keeps an internal colormap, initialized to predefined colors. The predefined colors do not correspond to pixel values the server understands but are indexes into the colormap. Therefore, they can’t be used in any of the `GC` altering or Xlib routines. To get the actual pixel value the X server understands, use the following routine

    unsigned long fl_get_pixel(FL_COLOR col);

To e.g., get the pixel value of the red color, use

    unsigned long red_pixel;
    red_pixel = fl_get_pixel(FL_RED);

To change the foreground color in the Forms Library’s default `GC` (`gc[0]`) use

    void fl_color(FL_COLOR col);

To set the background color in the default `GC` use instead

    void fl_bk_color(FL_COLOR col);

To set foreground or background in `GC`s other than the Forms Library’s default, the following functions exist:

    void fl_set_foreground(GC gc, FL_COLOR col);
    void fl_set_background(GC gc, FL_COLOR col);

which is equivalent to the following Xlib calls

    XSetForeground(fl_get_display(), gc, fl_get_pixel(color));
    XSetBackground(fl_get_display(), gc, fl_get_pixel(color));

To free allocated colors from the default colormap, use the following routine

    void fl_free_colors(FL_COLOR *cols, int n);

This function frees the `n` colors stored in the array of colormap indices `cols`. You shouldn’t do that for the reserved colors, i.e., colors with indices below `FL_FREE_COL1`.

In case the pixel values (instead of the index into the colormap) are known, the following routine can be used to free the colors from the default colormap

    void fl_free_pixels(unsigned long *pixels, int n);

Note that the internal colormap maintained by the Forms Library is not updated. This is in general harmless.

To modify or query the internal colormap, use the following routines:

    unsigned long fl_mapcolor(FL_COLOR col, int red, int green, int blue)
    long fl_mapcolorname(FL_COLOR col, const char *name);
    unsigned long fl_getmcolor(FL_COLOR col,
                               int *red, int *green, int *blue);

The first function, `fl_mapcolor()` sets a the color indexed by `color` to the color given by the `red`, `green` and `blue`, returning the colors pixel value.

The second function, `fl_mapcolorname()`, sets the color in the colormap indexed by `color` to the color named `name`, where `name` must be a valid name from the system’s color database file `rgb.txt`. It also returns the colors pixel value or -1 on failure.

The last function, `fl_getmcolor()`, returns the RGB values of the color indexed by `color` in the second to third argument pointers and the pixel value as the return value (or -1, cast to `unsigned long`, on failure).

Next: Mouse Handling, Previous: General Remarks, Up: Part IV Drawing Objects [Contents][Index]