GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
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 
7 #include <grass/colors.h>
8 #include <grass/display.h>
9 #include <grass/gis.h>
10 #include <grass/glocale.h>
11 #include <grass/raster.h>
12 
13 #include "driver.h"
14 
15 static struct color_rgb *colors;
16 static int ncolors;
17 static int nalloc;
18 
19 /*!
20  * \brief color name and suggested number to actual number
21  *
22  * Takes a color <b>name</b> or <b>red:green:blue</b> code in ascii
23  * and a <b>suggested color index</b>.
24  * If the color is a standard preallocated color it returns the color number for
25  * that color. Otherwise (if the color is not standard) it sets the color of the
26  * supplied index to the specified color. Returns -1 if color is not known and 0
27  * if the color is none.
28  *
29  * \param name_or_code
30  * \param suggested_color_index
31  * \return int
32  */
33 static int translate_or_add_color(const char *str)
34 {
35  int num_names = G_num_standard_color_names();
36  int index;
37  int red, grn, blu;
38  int i, ret;
39  char lowerstr[MAX_COLOR_LEN];
40 
41  /* Make the color string lowercase for display colors */
42  if (G_strlcpy(lowerstr, str, sizeof(lowerstr)) >= sizeof(lowerstr)) {
43  G_fatal_error(_("String <%s> is too long"), str);
44  }
45  G_chop(lowerstr);
46  G_tolcase(lowerstr);
47 
48  for (i = 0; i < num_names; i++) {
49  const struct color_name *name = G_standard_color_name(i);
50 
51  if (G_strcasecmp(str, name->name) == 0)
52  return name->number;
53  }
54 
55  if (!nalloc) {
56  ncolors = G_num_standard_colors();
57  nalloc = 2 * ncolors;
58  colors = G_malloc(nalloc * sizeof(struct color_rgb));
59  for (i = 0; i < ncolors; i++)
60  colors[i] = G_standard_color_rgb(i);
61  }
62 
63  ret = G_str_to_color(str, &red, &grn, &blu);
64 
65  /* None color */
66  if (ret == 2)
67  return 0;
68 
69  if (ret != 1)
70  return -1;
71 
72  for (i = 1; i < ncolors; i++)
73  if (colors[i].r == red && colors[i].g == grn && colors[i].b == blu)
74  return i;
75 
76  if (ncolors >= nalloc) {
77  nalloc *= 2;
78  colors = G_realloc(colors, nalloc * sizeof(struct color_rgb));
79  }
80 
81  index = ncolors++;
82 
83  colors[index].r = red;
84  colors[index].g = grn;
85  colors[index].b = blu;
86 
87  return index;
88 }
89 
90 /*!
91  * \brief color option text to usable color number
92  *
93  * Converts or looks up the color provided in the string.
94  * Returns a color number usable by D_use_color.
95  * If the color does not exist exits with a fatal error and message.
96  * If the color is none and none_acceptable is not true exits with
97  * a fatal error and message.
98  *
99  * \param str Color name or color code, as a string
100  * \param none_acceptable
101  * \return int Returns a color number usable by D_use_color.
102  */
103 int D_parse_color(const char *str, int none_acceptable)
104 {
105  int color;
106 
107  color = translate_or_add_color(str);
108  if (color == -1)
109  G_fatal_error(_("[%s]: No such color"), str);
110  if (color == 0 && !none_acceptable)
111  G_fatal_error(_("[%s]: No such color"), str);
112  return color;
113 }
114 
115 /*!
116  * \brief color name to number
117  *
118  * Takes a
119  * color <b>name</b> in ascii and returns the color number for that color.
120  * Returns 0 if color is not known. The color number returned is for lines and
121  * text, not raster graphics.
122  *
123  * \param str Color name as an ASCII string
124  * \return int
125  */
126 int D_translate_color(const char *str)
127 {
128  return D_parse_color(str, 0);
129 }
130 
131 /*!
132  * \brief draw with a color from D_parse_color
133  *
134  * Calls R_color or R_standard_color to use the color provided by
135  * D_parse_color. Returns 1 if color can be used to draw (is
136  * good and isn't none), 0 otherwise.
137  *
138  * \param color
139  * \return int
140  */
141 int D_use_color(int color)
142 {
143  if (color <= 0)
144  return 0;
145 
146  if (color < G_num_standard_colors()) {
147  COM_Standard_color(color);
148  return 1;
149  }
150 
151  if (color < ncolors) {
152  const struct color_rgb *c = &colors[color];
153 
154  D_RGB_color(c->r, c->g, c->b);
155  return 1;
156  }
157 
158  return 0;
159 }
160 
161 /*!
162  * \brief get RGB values from color number
163  *
164  * Translates the color number provided by D_parse_color
165  * into 0-255 RGB values.
166  *
167  * Returns 1 if color can be used to draw (is good and
168  * isn't 'none'), 0 otherwise.
169  *
170  * \param color The color number provided by D_parse_color to convert
171  * into 0-255 RGB values.
172  * \param r Pointer to an integer where the red component will be stored.
173  * \param g Pointer to an integer where the green component will be stored.
174  * \param b Pointer to an integer where the blue component will be stored.
175  *
176  * \return Returns 1 if color can be used to draw (is good and
177  * isn't 'none'), 0 otherwise.
178  */
179 int D_color_number_to_RGB(int color, int *r, int *g, int *b)
180 {
181  const struct color_rgb *c;
182 
183  if (color <= 0)
184  return 0;
185 
186  if (color < G_num_standard_colors()) {
187  struct color_rgb col = G_standard_color_rgb(color);
188 
189  if (r)
190  *r = col.r;
191  if (g)
192  *g = col.g;
193  if (b)
194  *b = col.b;
195 
196  return 1;
197  }
198 
199  if (color >= ncolors)
200  return 0;
201 
202  c = &colors[color];
203  if (r)
204  *r = c->r;
205  if (g)
206  *g = c->g;
207  if (b)
208  *b = c->b;
209 
210  return 1;
211 }
212 
213 void D_RGB_color(int red, int grn, int blu)
214 {
215  COM_Color_RGB(red, grn, blu);
216 }
#define MAX_COLOR_LEN
Definition: colors.h:37
int G_num_standard_colors(void)
Get number of named colors (RGB triplets)
Definition: color_str.c:56
int G_str_to_color(const char *, int *, int *, int *)
Parse color string and set red,green,blue.
Definition: color_str.c:103
int G_num_standard_color_names(void)
Get number of named colors (color names)
Definition: color_str.c:76
struct color_rgb G_standard_color_rgb(int)
Get RGB triplet of given color.
Definition: color_str.c:66
const struct color_name * G_standard_color_name(int)
Get color name.
Definition: color_str.c:86
char * G_tolcase(char *)
convert string to lower case
Definition: mapcase.c:17
#define G_realloc(p, n)
Definition: defs/gis.h:96
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition: defs/gis.h:94
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition: strings.c:47
char * G_chop(char *)
Chop leading and trailing white spaces.
Definition: strings.c:332
size_t G_strlcpy(char *, const char *, size_t)
Safe string copy function.
Definition: strlcpy.c:52
void COM_Standard_color(int number)
Definition: driver/color.c:11
void COM_Color_RGB(unsigned char r, unsigned char g, unsigned char b)
Definition: driver/color.c:5
#define _(str)
Definition: glocale.h:10
float g
Definition: named_colr.c:7
const char * name
Definition: named_colr.c:6
double b
Definition: r_raster.c:39
double r
Definition: r_raster.c:39
unsigned char g
Definition: colors.h:40
unsigned char b
Definition: colors.h:40
unsigned char r
Definition: colors.h:40
int D_use_color(int color)
draw with a color from D_parse_color
Definition: tran_colr.c:141
void D_RGB_color(int red, int grn, int blu)
Definition: tran_colr.c:213
int D_color_number_to_RGB(int color, int *r, int *g, int *b)
get RGB values from color number
Definition: tran_colr.c:179
int D_parse_color(const char *str, int none_acceptable)
color option text to usable color number
Definition: tran_colr.c:103
int D_translate_color(const char *str)
color name to number
Definition: tran_colr.c:126