• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

color_str.c

Go to the documentation of this file.
00001 
00016 #include <string.h>
00017 
00018 #include <grass/gis.h>
00019 #include <grass/colors.h>
00020 
00021 /* The order in this table is important! It will be indexed by color number */
00022 static const struct color_rgb standard_colors_rgb[] = {
00023     {0, 0, 0},                  /* This is a dummy value to make lookup easier */
00024     {0, 0, 0},                  /* BLACK   */
00025     {255, 0, 0},                /* RED     */
00026     {0, 255, 0},                /* GREEN   */
00027     {0, 0, 255},                /* BLUE    */
00028     {255, 255, 0},              /* YELLOW  */
00029     {0, 255, 255},              /* CYAN    */
00030     {255, 0, 255},              /* MAGENTA */
00031     {255, 255, 255},            /* WHITE   */
00032     {128, 128, 128},            /* GRAY    */
00033     {255, 128, 0},              /* ORANGE  */
00034     {100, 128, 255},            /* AQUA    */
00035     {0, 128, 255},              /* INDIGO  */
00036     {128, 0, 255},              /* VIOLET  */
00037     {180, 77, 25}               /* BROWN   */
00038 };
00039 
00040 /* The order in this table has no meaning. */
00041 static const struct color_name standard_color_names[] = {
00042     {"black", BLACK},
00043     {"red", RED},
00044     {"green", GREEN},
00045     {"blue", BLUE},
00046     {"yellow", YELLOW},
00047     {"cyan", CYAN},
00048     {"magenta", MAGENTA},
00049     {"white", WHITE},
00050     {"grey", GREY},
00051     {"gray", GRAY},
00052     {"orange", ORANGE},
00053     {"aqua", AQUA},
00054     {"indigo", INDIGO},
00055     {"violet", VIOLET},
00056     {"purple", PURPLE},
00057     {"brown", BROWN}
00058 };
00059 
00065 int G_num_standard_colors(void)
00066 {
00067     return sizeof(standard_colors_rgb) / sizeof(standard_colors_rgb[0]);
00068 }
00069 
00075 struct color_rgb G_standard_color_rgb(int n)
00076 {
00077     return standard_colors_rgb[n];
00078 }
00079 
00085 int G_num_standard_color_names(void)
00086 {
00087     return sizeof(standard_color_names) / sizeof(standard_color_names[0]);
00088 }
00089 
00095 const struct color_name *G_standard_color_name(int n)
00096 {
00097     return &standard_color_names[n];
00098 }
00099 
00112 int G_str_to_color(const char *str, int *red, int *grn, int *blu)
00113 {
00114     char buf[100];
00115     int num_names = G_num_standard_color_names();
00116     int i;
00117 
00118     G_strcpy(buf, str);
00119     G_chop(buf);
00120 
00121     G_debug(3, "G_str_to_color(): str = '%s'", buf);
00122 
00123     if (G_strcasecmp(buf, "NONE") == 0)
00124         return 2;
00125 
00126     if (sscanf(buf, "%d%*[,:; ]%d%*[,:; ]%d", red, grn, blu) == 3) {
00127         if (*red < 0 || *red > 255 ||
00128             *grn < 0 || *grn > 255 || *blu < 0 || *blu > 255)
00129             return 0;
00130 
00131         return 1;
00132     }
00133 
00134     /* Look for this color in the standard (preallocated) colors */
00135     for (i = 0; i < num_names; i++) {
00136         const struct color_name *name = &standard_color_names[i];
00137 
00138         if (G_strcasecmp(buf, name->name) == 0) {
00139             struct color_rgb rgb = standard_colors_rgb[name->number];
00140 
00141             *red = (int)rgb.r;
00142             *grn = (int)rgb.g;
00143             *blu = (int)rgb.b;
00144 
00145             return 1;
00146         }
00147     }
00148 
00149     return 0;
00150 }

Generated on Thu Dec 9 2010 20:46:05 for GRASS Programmer's Manual by  doxygen 1.7.2