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