color_str.c

Go to the documentation of this file.
00001 #include "string.h"
00002 #include "gis.h"
00003 
00004 #define NUM_COLORS      15 
00005 
00006 static struct {
00007     char *name;
00008     int r, g, b;
00009 } _colors[NUM_COLORS] =
00010 {
00011     {"white",   255, 255, 255},
00012     {"black",     0,   0,   0},
00013     {"red",     255,   0,   0},
00014     {"green",     0, 255,   0},
00015     {"blue",      0,   0, 255},
00016     {"yellow",  255, 255,   0},
00017     {"magenta", 255,   0, 255},
00018     {"cyan",      0, 255, 255},
00019     {"aqua",    100, 127, 255},
00020     {"grey",    127, 127, 127},
00021     {"gray",    127, 127, 127},
00022     {"orange",  255, 127,   0},
00023     {"brown",   180,  75,  25},
00024     {"violet",  255,   0, 255},
00025     {"indigo",    0, 127, 255}
00026 };
00027 
00028 /* 
00029 *  Parses color string and sets red,green,blue
00030 * 
00031 *  Returns: 1 - OK
00032 *           2 - NONE 
00033 *           0 - Error 
00034 * 
00035 */
00036 int G_str_to_color (char *str, int *red, int *green, int *blue)
00037 {
00038     int i, ret;
00039     char buf[100], temp[10]; 
00040 
00041     strcpy (buf, str );
00042     G_chop (buf);
00043     
00044     G_debug (3, "G_str_to_color(): str = '%s'", buf );
00045 
00046     if ( G_strcasecmp ( buf, "NONE" ) == 0 ) return 2;
00047     
00048     ret = sscanf (buf, "%d%[,:; ]%d%[,:; ]%d", red, temp, green, temp, blue);
00049    
00050     if ( ret == 5 ) { 
00051         if ( *red < 0 || *red > 255 || *green < 0 || *green > 255 ||
00052              *blue < 0 || *blue > 255 ) 
00053         { 
00054             return 0; 
00055         }
00056         return 1;
00057     } else {
00058         for (i = 0; i < NUM_COLORS; i++) {
00059             if ( G_strcasecmp(buf, _colors[i].name) == 0) {
00060                 *red   = _colors[i].r;
00061                 *green = _colors[i].g;
00062                 *blue  = _colors[i].b;
00063                 return 1;
00064             }
00065         }
00066         return 0;
00067     }
00068         
00069     return 0;
00070 }
00071 

Generated on Mon Jan 1 19:49:25 2007 for GRASS by  doxygen 1.5.1