grass  7.8.6
About: GRASS (Geographic Resources Analysis Support System) is a raster- and vector-based GIS, image processing system, graphics production system and spatial modeling system.
  Fossies Dox: grass-7.8.6.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

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
13static struct color_rgb *colors;
14static int ncolors;
15static 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 that color.
23 * Otherwise (if the color is not standard) it sets the color of the supplied index to
24 * the specified color.
25 * Returns -1 if color is not known and 0 if the color is none.
26 *
27 * \param name_or_code
28 * \param suggested_color_index
29 * \return int
30 */
31
32static 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) {
54 nalloc = 2 * ncolors;
55 colors = G_malloc(nalloc * sizeof(struct color_rgb));
56 for (i = 0; i < ncolors; 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
101int 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/*!
115 * \brief color name to number
116 *
117 * Takes a
118 * color <b>name</b> in ascii and returns the color number for that color.
119 * Returns 0 if color is not known. The color number returned is for lines and
120 * text, not raster graphics.
121 *
122 * \param name
123 * \return int
124 */
125
126int D_translate_color(const char *str)
127{
128 return D_parse_color(str, 0);
129}
130
131
132/*!
133 * \brief draw with a color from D_parse_color
134 *
135 * Calls R_color or R_standard_color to use the color provided by
136 * D_parse_color. Returns 1 if color can be used to draw (is
137 * good and isn't none), 0 otherwise.
138 *
139 * \param color
140 * \return int
141 */
142
143int D_use_color(int color)
144{
145 if (color <= 0)
146 return 0;
147
148 if (color < G_num_standard_colors()) {
149 COM_Standard_color(color);
150 return 1;
151 }
152
153 if (color < ncolors) {
154 const struct color_rgb *c = &colors[color];
155
156 D_RGB_color(c->r, c->g, c->b);
157 return 1;
158 }
159
160 return 0;
161}
162
163
164/*!
165 * \brief get RGB values from color number
166 *
167 * Translates the color number provided by D_parse_color
168 * into 0-255 RGB values.
169 *
170 * Returns 1 if color can be used to draw (is good and
171 * isn't 'none'), 0 otherwise.
172 *
173 * \param color_number
174 * \param red
175 * \param green
176 * \param blue
177 *
178 * \return int
179 */
180int D_color_number_to_RGB(int color, int *r, int *g, int *b)
181{
182 const struct color_rgb *c;
183
184 if (color <= 0)
185 return 0;
186
187 if (color < G_num_standard_colors()) {
188 struct color_rgb col = G_standard_color_rgb(color);
189
190 if (r)
191 *r = col.r;
192 if (g)
193 *g = col.g;
194 if (b)
195 *b = col.b;
196
197 return 1;
198 }
199
200 if (color >= ncolors)
201 return 0;
202
203 c = &colors[color];
204 if (r)
205 *r = c->r;
206 if (g)
207 *g = c->g;
208 if (b)
209 *b = c->b;
210
211 return 1;
212}
213
214void D_RGB_color(int red, int grn, int blu)
215{
216 COM_Color_RGB(red, grn, blu);
217}
218
int G_num_standard_colors(void)
Get number of named colors (RGB triplets)
Definition: color_str.c:65
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_num_standard_color_names(void)
Get number of named colors (color names)
Definition: color_str.c:85
struct color_rgb G_standard_color_rgb(int n)
Get RGB triplet of given color.
Definition: color_str.c:75
const struct color_name * G_standard_color_name(int n)
Get color name.
Definition: color_str.c:95
#define MAX_COLOR_LEN
Definition: colors.h:31
void COM_Standard_color(int number)
Definition: color.c:11
void COM_Color_RGB(unsigned char r, unsigned char g, unsigned char b)
Definition: color.c:5
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: error.c:160
#define _(str)
Definition: glocale.h:13
char * G_tolcase(char *string)
convert string to lower case
Definition: mapcase.c:19
float g
Definition: named_colr.c:8
const char * name
Definition: named_colr.c:7
double b
Definition: r_raster.c:39
double r
Definition: r_raster.c:39
char * G_chop(char *line)
Chop leading and trailing white spaces.
Definition: strings.c:286
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:46
unsigned char g
Definition: colors.h:35
unsigned char b
Definition: colors.h:35
unsigned char r
Definition: colors.h:35
static int translate_or_add_color(const char *str)
color name and suggested number to actual number
Definition: tran_colr.c:32
int D_use_color(int color)
draw with a color from D_parse_color
Definition: tran_colr.c:143
void D_RGB_color(int red, int grn, int blu)
Definition: tran_colr.c:214
static struct color_rgb * colors
Definition: tran_colr.c:13
int D_color_number_to_RGB(int color, int *r, int *g, int *b)
get RGB values from color number
Definition: tran_colr.c:180
static int nalloc
Definition: tran_colr.c:15
int D_parse_color(const char *str, int none_acceptable)
color option text to usable color number
Definition: tran_colr.c:101
static int ncolors
Definition: tran_colr.c:14
int D_translate_color(const char *str)
color name to number
Definition: tran_colr.c:126
static unsigned int c
Definition: unfl.c:8