[GRASS-dev] lib fn to convert int color number to RGB values?
Ivan Shmakov
ivan at theory.asu.ru
Wed Jan 30 01:15:58 EST 2008
>>>>> Glynn Clements <glynn at gclements.plus.com> writes:
[...]
>> 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)
> {
That looks a lot like C++. Surely it will work with C89?
> const struct color_rgb *c;
> if (color <= 0)
> return 0;
BTW, is it a GRASS convention to use 0 to indicate failure?
Standard C library functions tend to use -1 for failure, while
using non-negative values to indicate success.
Also, it seems feasible to set `errno' in the library functions
like this. Do I understand correctly that it isn't done (with
any regularity) in GRASS?
> if (color >= ncolors)
> return 0;
> c = &colors[color];
> r = c->r;
> g = c->g;
> b = c->b;
> return 1;
> }
[...]
I'd suggest the following, but I don't know (yet) the
appropriate conventions for the D_* functions.
int
D_color_number_to_RGB (int color, int *r, int *g, int *b)
{
const struct color_rgb *c;
if (color <= 0 || color >= ncolors) {
errno = EINVAL;
return -1;
}
c = &colors[color];
if (r != 0) *r = c->r;
if (g != 0) *g = c->g;
if (b != 0) *b = c->b;
return 0;
}
With the `if (X != 0)' guards, the function may be used to query
whether the given color index exists, while not asking for its
components to be returned, like:
if (D_color_number_to_RGB (INDEX, 0, 0, 0) < 0) {
/* color INDEX doesn't exist */
}
If `struct color_rgb' is a ``public'' data type, then it might
be appropriate to code this function as:
int
D_color_number_to_RGB (int color, struct color_rgb *components)
{
...
}
instead, and use a guarded `memmove' to copy `colors[color]' to
`*components'.
More information about the grass-dev
mailing list