[GRASS5] all color in vectors and areas

Eric G. Miller egm2 at jps.net
Fri Mar 30 03:15:53 EST 2001


On Fri, Apr 06, 2001 at 01:53:06AM -0300, Fernando Pacheco wrote:
> Hello:
> I write a function D_translate_color_to_RGB(char *) in
> src/libes/display/tran_color.c to translate user input string like
> indigo or magenta or grey or 255,0,127 or (((255|0|||127)-- or
> somestring255somestring0somestring127somestring to typedef RGB defined
> in src/include/color.h then I can use all the colors in vectors
> (d.vect) and areas (d.area). I'm sending to you my 2 new files, and a
> d.area using the new library function. The function can substitute the
> D_translate_color.
> 
> I'm not a experience C program but my users like the new colors.
> Fernando Pacheco.

Thanks for your effort.  Couple things I noticed in your function:

Use of C++ style comments.  AFAIK, these are allowed in ISO C99, but
many older compilers will choke.  Second (and worse) you are returning a
locally scoped variable.  This may work on some systems, but on others
it will (and properly should) cause a segfault:

tran_colr.c: My comments delimited with <comment>...</comment>

/* Take a string in format R|G|B or (R,G,B) or somestringRsomestringGsomestringBsomestring or red or magenta or indigo...,
* and returns the colors numbers in format RGB as short. R=0-255,G=0-255,B=0-255.
*Fernando Pacheco 
* fpacheco at dinama.gub.uy
*/

RGB D_translate_color_to_RGB(char *str )
<comment> Make RGB * D_translate_color_to_RGB (char *str) </comment>
{
int ltotal,cont1,esdigito,color;
char buf[56];
char buftemp[]="...................";
<comment>       ^^^^^^^^^^^^^^^^^^^^^^^^ What is that about? </comment>
              
char one_char[2];

//typedef RGB is defined in color.h
//D_translate_color_to_RGB is declared in color.h
<comment> Don't use C++ style commenting </comment>

RGB RGB_ret;
<comment> Bad juju here.  This variable is local to this function:
Instead:

RGB *RGB_ret = (RGB *) G_malloc (sizeof(RGB));

and use RGB_ret->r (etc...)
</comment>

<comment> SNIP strcmp()'s </comment>

//not a named color then...
ltotal=strlen(str);
strcpy(buf,str);
esdigito=0;
color=0;
for (cont1=0;cont1<ltotal+1;cont1++)
{
	if ((buf[cont1]=='0') || (buf[cont1]=='1') || (buf[cont1]=='2') || (buf[cont1]=='3') || (buf[cont1]=='4') || (buf[cont1]=='5') || (buf[cont1]=='6') || (buf[cont1]=='7') || (buf[cont1]=='8') || (buf[cont1]=='9'))
	{
      one_char[0]=buf[cont1];
	  esdigito=esdigito+1;
	  buftemp[(esdigito-1)]=one_char[0];	
	} else {
	  	if (esdigito>0)
	  	{
	  		color=color+1;
	  		switch (color)
	  		{ 
	  		 case 1:
	  			RGB_ret.r=atoi(buftemp);
	  			if (RGB_ret.r>255)
	  				RGB_ret.r=255;
				  break;	  	 		
	  	 	case 2:
	  	 	   RGB_ret.g=atoi(buftemp);
	  			if (RGB_ret.g>255)
	  				RGB_ret.g=255;
				  break;	  	 		
	  	 	case 3:
	  	 	   RGB_ret.b=atoi(buftemp);		 
	  			if (RGB_ret.b>255)
	  				RGB_ret.b=255;
				  break;	  	 		
	  		}   	
	  	strcpy(buftemp,"...................");
	  	esdigito=0;
	     }	
	}
}
return(RGB_ret) ;
}

<comment>

Consider using strtol() and strtok() for your parsing or sscanf() even.

  {
    RGB *RGB_ret;
    int r,b,g;

    if (sscanf (str, "( %d , %d , %d )", &r, &g, &b) == 3)
    {
       /* success */
    }
    else if (sscanf (str, "%d | %d | %d", &r, &g, &b) == 3)
    {
       /* success */
    }
    else
    {
       /* failed */
       return NULL;
    }
    if (!(r >= 0 && r <= 255
         && g >= 0 && g <= 255
         && b >= 0 && b <= 255)
       )
    { /* error */
      return NULL;
    }

    RGB_ret = (RGB *) G_malloc (sizeof(RGB));
    if (RGB_ret == NULL)
    {
      return NULL;
    }
     
    RGB_ret->r = (unsigned short) r;
    RGB_ret->g = (unsigned short) g;
    RGB_ret->b = (unsigned short) b;

    return RGB_ret;
  }

</comment>

-- 
Eric G. Miller <egm2 at jps.net>

---------------------------------------- 
If you want to unsubscribe from GRASS Development Team mailing list write to:
minordomo at geog.uni-hannover.de with
subject 'unsubscribe grass5'



More information about the grass-dev mailing list