color_str.c

Go to the documentation of this file.
00001 #include "string.h"
00002 #include <grass/gis.h>
00003 #include <grass/colors.h>
00004 
00005 /* The order in this table is important! It will be indexed by color number */
00006 const struct color_rgb standard_colors_rgb [MAX_COLOR_NUM + 1] =
00007 {
00008   {  0,  0,  0}, /* This is a dummy value to make lookup easier */
00009   {  0,  0,  0}, /* BLACK   */
00010   {255,  0,  0}, /* RED     */
00011   {  0,255,  0}, /* GREEN   */
00012   {  0,  0,255}, /* BLUE    */
00013   {255,255,  0}, /* YELLOW  */
00014   {  0,255,255}, /* CYAN    */
00015   {255,  0,255}, /* MAGENTA */
00016   {255,255,255}, /* WHITE   */
00017   {128,128,128}, /* GRAY    */
00018   {255,128,  0}, /* ORANGE  */
00019   {100,128,255}, /* AQUA    */
00020   {  0,128,255}, /* INDIGO  */
00021   {128,  0,255}, /* VIOLET  */
00022   {180, 77, 25}  /* BROWN   */
00023 };
00024 
00025 /* The order in this table has no meaning. */
00026 const struct color_name standard_color_names[MAX_COLOR_NAMES] =
00027 {
00028     {"black",   BLACK},
00029     {"red",     RED},
00030     {"green",   GREEN},
00031     {"blue",    BLUE},
00032     {"yellow",  YELLOW},
00033     {"cyan",    CYAN},
00034     {"magenta", MAGENTA},
00035     {"white",   WHITE},
00036     {"grey",    GREY},
00037     {"gray",    GRAY},
00038     {"orange",  ORANGE},
00039     {"aqua",    AQUA},
00040     {"indigo",  INDIGO},
00041     {"violet",  VIOLET},
00042     {"purple",  PURPLE},
00043     {"brown",   BROWN}
00044 };
00045 
00046 /* 
00047 *  Parses color string and sets red,green,blue
00048 * 
00049 *  Returns: 1 - OK
00050 *           2 - NONE 
00051 *           0 - Error 
00052 * 
00053 */
00054 int G_str_to_color (const char *str, int *red, int *green, int *blue)
00055 {
00056     int i, ret, n;
00057     char buf[100], temp[10]; 
00058 
00059     G_strcpy (buf, str );
00060     G_chop (buf);
00061     
00062     G_debug (3, "G_str_to_color(): str = '%s'", buf );
00063 
00064     if ( G_strcasecmp ( buf, "NONE" ) == 0 ) return 2;
00065     
00066     ret = sscanf (buf, "%d%[,:; ]%d%[,:; ]%d", red, temp, green, temp, blue);
00067    
00068     if ( ret == 5 ) { 
00069         if ( *red < 0 || *red > 255 || *green < 0 || *green > 255 ||
00070              *blue < 0 || *blue > 255 ) 
00071         { 
00072             return 0; 
00073         }
00074         return 1;
00075     } else {
00076         /* Look for this color in the standard (preallocated) colors */
00077         for (i = 0; i < MAX_COLOR_NAMES; i++) {
00078             if ( G_strcasecmp(buf, standard_color_names[i].name) == 0) {
00079                 n = standard_color_names[i].number;
00080                 *red   = (int) standard_colors_rgb[n].r;
00081                 *green = (int) standard_colors_rgb[n].g;
00082                 *blue  = (int) standard_colors_rgb[n].b;
00083                 return 1;
00084             }
00085         }
00086 
00087         return 0;
00088     }
00089         
00090     return 0;
00091 }
00092 

Generated on Fri Nov 21 11:02:17 2008 for GRASS by  doxygen 1.5.1