GRASS Programmer's Manual  6.4.2(2012)
tran_colr.c
Go to the documentation of this file.
00001 /* Takes a color name in ascii, returns the color number for that color.
00002  *    returns 0 if color is not known.
00003  */
00004 
00005 #include <string.h>
00006 #include <grass/display.h>
00007 #include <grass/colors.h>
00008 #include <grass/raster.h>
00009 #include <grass/glocale.h>
00010 
00011 static struct color_rgb *colors;
00012 static int ncolors;
00013 static int nalloc;
00014 
00027 int D_translate_color(const char *str)
00028 {
00029     int num_names = G_num_standard_color_names();
00030     int i;
00031 
00032     for (i = 0; i < num_names; i++) {
00033         const struct color_name *name = G_standard_color_name(i);
00034 
00035         if (G_strcasecmp(str, name->name) == 0)
00036             return name->number;
00037     }
00038 
00039     return 0;
00040 }
00041 
00042 
00058 static int translate_or_add_color(const char *str)
00059 {
00060     int index;
00061     int red, grn, blu;
00062     int i, preallocated, ret;
00063     char lowerstr[MAX_COLOR_LEN];
00064 
00065     /* Make the color string lowercase for display colors */
00066     G_strcpy(lowerstr, str);
00067     G_chop(lowerstr);
00068     G_tolcase(lowerstr);
00069 
00070     preallocated = D_translate_color(lowerstr);
00071     if (preallocated)
00072         return preallocated;
00073 
00074     if (!nalloc) {
00075         ncolors = G_num_standard_colors();
00076         nalloc = 2 * ncolors;
00077         colors = G_malloc(nalloc * sizeof(struct color_rgb));
00078         for (i = 0; i < ncolors; i++)
00079             colors[i] = G_standard_color_rgb(i);
00080     }
00081 
00082     ret = G_str_to_color(str, &red, &grn, &blu);
00083 
00084     /* None color */
00085     if (ret == 2)
00086         return 0;
00087 
00088     if (ret != 1)
00089         return -1;
00090 
00091     for (i = 1; i < ncolors; i++)
00092         if (colors[i].r == red && colors[i].g == grn && colors[i].b == blu)
00093             return i;
00094 
00095     if (ncolors >= nalloc) {
00096         nalloc *= 2;
00097         colors = G_realloc(colors, nalloc * sizeof(struct color_rgb));
00098     }
00099 
00100     index = ncolors++;
00101 
00102     colors[index].r = red;
00103     colors[index].g = grn;
00104     colors[index].b = blu;
00105 
00106     return index;
00107 }
00108 
00123 int D_parse_color(const char *str, int none_acceptable)
00124 {
00125     int color;
00126 
00127     color = translate_or_add_color(str);
00128     if (color == -1)
00129         G_fatal_error(_("[%s]: No such color"), str);
00130     if (color == 0 && !none_acceptable)
00131         G_fatal_error(_("[%s]: No such color"), str);
00132     return color;
00133 }
00134 
00146 int D_raster_use_color(int color)
00147 {
00148     if (color <= 0)
00149         return 0;
00150 
00151     if (color < G_num_standard_colors()) {
00152         R_standard_color(color);
00153         return 1;
00154     }
00155 
00156     if (color < ncolors) {
00157         const struct color_rgb *c = &colors[color];
00158 
00159         R_RGB_color(c->r, c->g, c->b);
00160         return 1;
00161     }
00162 
00163     return 0;
00164 }
00165 
00166 
00183 int D_color_number_to_RGB(int color, int *r, int *g, int *b)
00184 {
00185     const struct color_rgb *c;
00186 
00187     if (color <= 0)
00188         return 0;
00189 
00190     if (color < G_num_standard_colors()) {
00191         struct color_rgb col = G_standard_color_rgb(color);
00192 
00193         if (r)
00194             *r = col.r;
00195         if (g)
00196             *g = col.g;
00197         if (b)
00198             *b = col.b;
00199 
00200         return 1;
00201     }
00202 
00203     if (color >= ncolors)
00204         return 0;
00205 
00206     c = &colors[color];
00207     if (r)
00208         *r = c->r;
00209     if (g)
00210         *g = c->g;
00211     if (b)
00212         *b = c->b;
00213 
00214     return 1;
00215 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines