[GRASS5] Color prompt partially implemented, color confusion.

Cedric Shock cedricgrass at shockfamily.net
Sun Apr 16 22:27:36 EDT 2006


Does this (untested) snippet look useful for turning strings of colors into 
colors in the color table? We could get slightly more information about 
failure to find colors / use of none if it was safe to use a negative return 
value for a failure (there are no negative colors) or if the integer 
suggested color number is a pointer and is modified.

Thanks for the comments,


 * \brief color name and suggested number to actual number
 * Takes a color <b>name</b> or <b>red:green:blue</b> code in ascii
 * and a <b>suggested color index</b>.
 * If the color is a standard preallocated color it returns the color number 
for that color.
 * Otherwise (if the color is not standard) it sets the color of the supplied 
index to 
 * the specified color (see R_reset_color).
 * Returns 0 if color is not known or if the color is none.
 *  \param name_or_code
 *  \param suggested_color_index
 *  \return int

int D_translate_or_add_color (const char * str, int index)
	int redi, greeni, bluei;
	int preallocated, ret;

	char lowerstr[MAX_COLOR_LEN];

	/* Make the color string lowercase for display colors */
	G_strcpy (lowerstr, str );
	G_chop (lowerstr);
	G_tolcase (lowerstr);	

	preallocated = D_translate_color (lowerstr);
	if (preallocated != 0) {
		return preallocated;

	ret = G_str_to_color (str, &redi, &greeni, &bluei);

	if (ret == 2) {
		/* None color */
		return 0;
	} else if (ret == 1) {
		/* Add the specified color to the suggested index */
		R_reset_color ( (unsigned char) redi, (unsigned char) greeni, (unsigned 
char) bluei, index);
		return index;

	return 0;

More information about the grass-dev mailing list