[GRASS-dev] lib fn to convert int color number to RGB values?

Glynn Clements glynn at gclements.plus.com
Tue Jan 29 06:54:22 EST 2008


Hamish wrote:

> I am looking for a library function to get the R,G,B values (0-255) for
> an integer color number which was created with D_parse_color(). I don't
> want to set the color for drawing, just get the RGB component values so
> I can populate a RGBA_Color struct. Reparsing the original color string
> isn't an option without a lot of re-fn prototyping as I'm operating in
> a subordinate function with only the int color number available.
> 
> I've searched through the color functions in the display, raster, and
> gis libraries without much luck. The closest I found was
> D_raster_use_color().
> 
> any ideas?

That information isn't available at present.

Also, the concept of colour numbers is likely to go away entirely in
7.x. With the display drivers and raster library using R/G/B colours
exclusively, the only reason that colour numbers remain was to avoid
having to re-write all of the modules which parse a colour string to
an integer.

> Is a new D_number_to_RGB(int color, unsigned char &r,&g,&b) fn needed
> in lib/display/tran_colr.c?

Yes. E.g.:

int D_color_number_to_RGB(int color, int &r, int &g, int &b)
{
    const struct color_rgb *c;

    if (color <= 0)
        return 0;

    if (color >= ncolors)
        return 0;

    c = &colors[color];
    r = c->r;
    g = c->g;
    b = c->b;

    return 1;
}

> Or some place better?

The array which holds the information isn't exported from
tran_color.c, so the code can't go anywhere else.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list