GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
color_str.c
Go to the documentation of this file.
1 
16 #include <string.h>
17 
18 #include <grass/gis.h>
19 #include <grass/colors.h>
20 
21 /* The order in this table is important! It will be indexed by color number */
22 static const struct color_rgb standard_colors_rgb[] = {
23  {0, 0, 0}, /* This is a dummy value to make lookup easier */
24  {0, 0, 0}, /* BLACK */
25  {255, 0, 0}, /* RED */
26  {0, 255, 0}, /* GREEN */
27  {0, 0, 255}, /* BLUE */
28  {255, 255, 0}, /* YELLOW */
29  {0, 255, 255}, /* CYAN */
30  {255, 0, 255}, /* MAGENTA */
31  {255, 255, 255}, /* WHITE */
32  {128, 128, 128}, /* GRAY */
33  {255, 128, 0}, /* ORANGE */
34  {100, 128, 255}, /* AQUA */
35  {0, 128, 255}, /* INDIGO */
36  {128, 0, 255}, /* VIOLET */
37  {180, 77, 25} /* BROWN */
38 };
39 
40 /* The order in this table has no meaning. */
41 static const struct color_name standard_color_names[] = {
42  {"black", BLACK},
43  {"red", RED},
44  {"green", GREEN},
45  {"blue", BLUE},
46  {"yellow", YELLOW},
47  {"cyan", CYAN},
48  {"magenta", MAGENTA},
49  {"white", WHITE},
50  {"grey", GREY},
51  {"gray", GRAY},
52  {"orange", ORANGE},
53  {"aqua", AQUA},
54  {"indigo", INDIGO},
55  {"violet", VIOLET},
56  {"purple", PURPLE},
57  {"brown", BROWN}
58 };
59 
66 {
67  return sizeof(standard_colors_rgb) / sizeof(standard_colors_rgb[0]);
68 }
69 
75 struct color_rgb G_standard_color_rgb(int n)
76 {
77  return standard_colors_rgb[n];
78 }
79 
86 {
87  return sizeof(standard_color_names) / sizeof(standard_color_names[0]);
88 }
89 
95 const struct color_name *G_standard_color_name(int n)
96 {
97  return &standard_color_names[n];
98 }
99 
112 int G_str_to_color(const char *str, int *red, int *grn, int *blu)
113 {
114  char buf[100];
115  int num_names = G_num_standard_color_names();
116  int i;
117 
118  G_strcpy(buf, str);
119  G_chop(buf);
120 
121  G_debug(3, "G_str_to_color(): str = '%s'", buf);
122 
123  if (G_strcasecmp(buf, "NONE") == 0)
124  return 2;
125 
126  if (sscanf(buf, "%d%*[,:; ]%d%*[,:; ]%d", red, grn, blu) == 3) {
127  if (*red < 0 || *red > 255 ||
128  *grn < 0 || *grn > 255 || *blu < 0 || *blu > 255)
129  return 0;
130 
131  return 1;
132  }
133 
134  /* Look for this color in the standard (preallocated) colors */
135  for (i = 0; i < num_names; i++) {
136  const struct color_name *name = &standard_color_names[i];
137 
138  if (G_strcasecmp(buf, name->name) == 0) {
139  struct color_rgb rgb = standard_colors_rgb[name->number];
140 
141  *red = (int)rgb.r;
142  *grn = (int)rgb.g;
143  *blu = (int)rgb.b;
144 
145  return 1;
146  }
147  }
148 
149  return 0;
150 }
int G_num_standard_colors(void)
Get number of named colors (RGB triplets)
Definition: color_str.c:65
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:192
string name
Definition: render.py:1314
char * G_chop(char *line)
Chop leading and trailing white spaces:
Definition: strings.c:418
char * G_strcpy(char *T, const char *F)
Copies characters from the string F into the string T.
Definition: strings.c:46
int G_num_standard_color_names(void)
Get number of named colors (color names)
Definition: color_str.c:85
int
Definition: g3dcolor.c:48
struct color_name * G_standard_color_name(int n)
Get color name.
Definition: color_str.c:95
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
int G_str_to_color(const char *str, int *red, int *grn, int *blu)
Parse color string and set red,green,blue.
Definition: color_str.c:112
int n
Definition: dataquad.c:291
struct color_rgb G_standard_color_rgb(int n)
Get RGB triplet of given color.
Definition: color_str.c:75