GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-83537f72e2
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/display.h>
8 #include <grass/colors.h>
9 #include <grass/raster.h>
10 #include <grass/glocale.h>
11 #include "driver.h"
12 
13 static struct color_rgb *colors;
14 static int ncolors;
15 static int nalloc;
16 
17 /*!
18  * \brief color name and suggested number to actual number
19  *
20  * Takes a color <b>name</b> or <b>red:green:blue</b> code in ascii
21  * and a <b>suggested color index</b>.
22  * If the color is a standard preallocated color it returns the color number for
23  * that color. Otherwise (if the color is not standard) it sets the color of the
24  * supplied index to the specified color. Returns -1 if color is not known and 0
25  * if the color is none.
26  *
27  * \param name_or_code
28  * \param suggested_color_index
29  * \return int
30  */
31 
32 static int translate_or_add_color(const char *str)
33 {
34  int num_names = G_num_standard_color_names();
35  int index;
36  int red, grn, blu;
37  int i, ret;
38  char lowerstr[MAX_COLOR_LEN];
39 
40  /* Make the color string lowercase for display colors */
41  strcpy(lowerstr, str);
42  G_chop(lowerstr);
43  G_tolcase(lowerstr);
44 
45  for (i = 0; i < num_names; i++) {
46  const struct color_name *name = G_standard_color_name(i);
47 
48  if (G_strcasecmp(str, name->name) == 0)
49  return name->number;
50  }
51 
52  if (!nalloc) {
53  ncolors = G_num_standard_colors();
54  nalloc = 2 * ncolors;
55  colors = G_malloc(nalloc * sizeof(struct color_rgb));
56  for (i = 0; i < ncolors; i++)
57  colors[i] = G_standard_color_rgb(i);
58  }
59 
60  ret = G_str_to_color(str, &red, &grn, &blu);
61 
62  /* None color */
63  if (ret == 2)
64  return 0;
65 
66  if (ret != 1)
67  return -1;
68 
69  for (i = 1; i < ncolors; i++)
70  if (colors[i].r == red && colors[i].g == grn && colors[i].b == blu)
71  return i;
72 
73  if (ncolors >= nalloc) {
74  nalloc *= 2;
75  colors = G_realloc(colors, nalloc * sizeof(struct color_rgb));
76  }
77 
78  index = ncolors++;
79 
80  colors[index].r = red;
81  colors[index].g = grn;
82  colors[index].b = blu;
83 
84  return index;
85 }
86 
87 /*!
88  * \brief color option text to usable color number
89  *
90  * Converts or looks up the color provided in the string.
91  * Returns a color number usable by D_use_color.
92  * If the color does not exist exits with a fatal error and message.
93  * If the color is none and none_acceptable is not true exits with
94  * a fatal error and message.
95  *
96  * \param name_or_code
97  * \param none_acceptable
98  * \return int
99  */
100 
101 int D_parse_color(const char *str, int none_acceptable)
102 {
103  int color;
104 
105  color = translate_or_add_color(str);
106  if (color == -1)
107  G_fatal_error(_("[%s]: No such color"), str);
108  if (color == 0 && !none_acceptable)
109  G_fatal_error(_("[%s]: No such color"), str);
110  return color;
111 }
112 
113 /*!
114  * \brief color name to number
115  *
116  * Takes a
117  * color <b>name</b> in ascii and returns the color number for that color.
118  * Returns 0 if color is not known. The color number returned is for lines and
119  * text, not raster graphics.
120  *
121  * \param name
122  * \return int
123  */
124 
125 int D_translate_color(const char *str)
126 {
127  return D_parse_color(str, 0);
128 }
129 
130 /*!
131  * \brief draw with a color from D_parse_color
132  *
133  * Calls R_color or R_standard_color to use the color provided by
134  * D_parse_color. Returns 1 if color can be used to draw (is
135  * good and isn't none), 0 otherwise.
136  *
137  * \param color
138  * \return int
139  */
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_number
171  * \param red
172  * \param green
173  * \param blue
174  *
175  * \return int
176  */
177 int D_color_number_to_RGB(int color, int *r, int *g, int *b)
178 {
179  const struct color_rgb *c;
180 
181  if (color <= 0)
182  return 0;
183 
184  if (color < G_num_standard_colors()) {
185  struct color_rgb col = G_standard_color_rgb(color);
186 
187  if (r)
188  *r = col.r;
189  if (g)
190  *g = col.g;
191  if (b)
192  *b = col.b;
193 
194  return 1;
195  }
196 
197  if (color >= ncolors)
198  return 0;
199 
200  c = &colors[color];
201  if (r)
202  *r = c->r;
203  if (g)
204  *g = c->g;
205  if (b)
206  *b = c->b;
207 
208  return 1;
209 }
210 
211 void D_RGB_color(int red, int grn, int blu)
212 {
213  COM_Color_RGB(red, grn, blu);
214 }
#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
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
#define strcpy
Definition: parson.c:62
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:141
void D_RGB_color(int red, int grn, int blu)
Definition: tran_colr.c:211
int D_color_number_to_RGB(int color, int *r, int *g, int *b)
get RGB values from color number
Definition: tran_colr.c:177
int D_parse_color(const char *str, int none_acceptable)
color option text to usable color number
Definition: tran_colr.c:101
int D_translate_color(const char *str)
color name to number
Definition: tran_colr.c:125