GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tran_colr.c
Go to the documentation of this file.
1 /* Takes a color name in ascii, returns the color number for that color.
2  * returns 0 if color is not known.
3  */
4 
5 #include <string.h>
6 #include <grass/display.h>
7 #include <grass/colors.h>
8 #include <grass/raster.h>
9 #include <grass/glocale.h>
10 
11 static struct color_rgb *colors;
12 static int ncolors;
13 static int nalloc;
14 
27 int D_translate_color(const char *str)
28 {
29  int num_names = G_num_standard_color_names();
30  int i;
31 
32  for (i = 0; i < num_names; i++) {
33  const struct color_name *name = G_standard_color_name(i);
34 
35  if (G_strcasecmp(str, name->name) == 0)
36  return name->number;
37  }
38 
39  return 0;
40 }
41 
42 
58 static int translate_or_add_color(const char *str)
59 {
60  int index;
61  int red, grn, blu;
62  int i, preallocated, ret;
63  char lowerstr[MAX_COLOR_LEN];
64 
65  /* Make the color string lowercase for display colors */
66  G_strcpy(lowerstr, str);
67  G_chop(lowerstr);
68  G_tolcase(lowerstr);
69 
70  preallocated = D_translate_color(lowerstr);
71  if (preallocated)
72  return preallocated;
73 
74  if (!nalloc) {
75  ncolors = G_num_standard_colors();
76  nalloc = 2 * ncolors;
77  colors = G_malloc(nalloc * sizeof(struct color_rgb));
78  for (i = 0; i < ncolors; i++)
79  colors[i] = G_standard_color_rgb(i);
80  }
81 
82  ret = G_str_to_color(str, &red, &grn, &blu);
83 
84  /* None color */
85  if (ret == 2)
86  return 0;
87 
88  if (ret != 1)
89  return -1;
90 
91  for (i = 1; i < ncolors; i++)
92  if (colors[i].r == red && colors[i].g == grn && colors[i].b == blu)
93  return i;
94 
95  if (ncolors >= nalloc) {
96  nalloc *= 2;
97  colors = G_realloc(colors, nalloc * sizeof(struct color_rgb));
98  }
99 
100  index = ncolors++;
101 
102  colors[index].r = red;
103  colors[index].g = grn;
104  colors[index].b = blu;
105 
106  return index;
107 }
108 
123 int D_parse_color(const char *str, int none_acceptable)
124 {
125  int color;
126 
127  color = translate_or_add_color(str);
128  if (color == -1)
129  G_fatal_error(_("[%s]: No such color"), str);
130  if (color == 0 && !none_acceptable)
131  G_fatal_error(_("[%s]: No such color"), str);
132  return color;
133 }
134 
147 {
148  if (color <= 0)
149  return 0;
150 
151  if (color < G_num_standard_colors()) {
152  R_standard_color(color);
153  return 1;
154  }
155 
156  if (color < ncolors) {
157  const struct color_rgb *c = &colors[color];
158 
159  R_RGB_color(c->r, c->g, c->b);
160  return 1;
161  }
162 
163  return 0;
164 }
165 
166 
183 int D_color_number_to_RGB(int color, int *r, int *g, int *b)
184 {
185  const struct color_rgb *c;
186 
187  if (color <= 0)
188  return 0;
189 
190  if (color < G_num_standard_colors()) {
191  struct color_rgb col = G_standard_color_rgb(color);
192 
193  if (r)
194  *r = col.r;
195  if (g)
196  *g = col.g;
197  if (b)
198  *b = col.b;
199 
200  return 1;
201  }
202 
203  if (color >= ncolors)
204  return 0;
205 
206  c = &colors[color];
207  if (r)
208  *r = c->r;
209  if (g)
210  *g = c->g;
211  if (b)
212  *b = c->b;
213 
214  return 1;
215 }
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
float b
Definition: named_colr.c:8
string name
Definition: render.py:1314
float r
Definition: named_colr.c:8
void R_standard_color(int index)
select standard color
Definition: com_proto.c:90
char * G_chop(char *line)
Chop leading and trailing white spaces:
Definition: strings.c:418
char * G_tolcase(char *string)
convert string to lower case
Definition: mapcase.c:19
char * G_strcpy(char *T, const char *F)
Copies characters from the string F into the string T.
Definition: strings.c:46
int D_raster_use_color(int color)
draw with a color from D_parse_color
Definition: tran_colr.c:146
int G_num_standard_color_names(void)
Get number of named colors (color names)
Definition: color_str.c:85
tuple color
Definition: tools.py:1703
int D_color_number_to_RGB(int color, int *r, int *g, int *b)
get RGB values from color number
Definition: tran_colr.c:183
float g
Definition: named_colr.c:8
struct color_name * G_standard_color_name(int n)
Get color name.
Definition: color_str.c:95
int D_translate_color(const char *str)
color name to number
Definition: tran_colr.c:27
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 G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
void R_RGB_color(unsigned char red, unsigned char grn, unsigned char blu)
select color
Definition: com_proto.c:109
struct color_rgb G_standard_color_rgb(int n)
Get RGB triplet of given color.
Definition: color_str.c:75
int D_parse_color(const char *str, int none_acceptable)
color option text to usable color number
Definition: tran_colr.c:123