2
\file lib/raster/color_read.c
4
\brief Raster Library - Read and parse color rules file
6
(C) 2007 by the GRASS Development Team
8
This program is free software under the GNU General Public
9
License (>=v2). Read the file COPYING that comes with GRASS
12
\author Glynn Clements
17
#include <grass/gis.h>
18
#include <grass/raster.h>
19
#include <grass/glocale.h>
39
\brief Read color rule
41
\param min, max min & max values (used only when color rules are in percentage)
44
\param[out] r,g,b color values
52
int Rast_parse_color_rule(DCELL min, DCELL max, const char *buf,
53
DCELL * val, int *r, int *g, int *b,
54
int *norm, int *nval, int *dflt)
56
char value[80], color[80];
60
*norm = *nval = *dflt = 0;
62
if (sscanf(buf, "%s %[^\n]", value, color) != 2)
63
return CR_ERROR_SYNTAX;
67
if (sscanf(color, "%d:%d:%d", r, g, b) == 3 ||
68
sscanf(color, "%d %d %d", r, g, b) == 3) {
69
if (*r < 0 || *r > 255 || *g < 0 || *g > 255 || *b < 0 || *b > 255)
75
if (G_color_values(color, &fr, &fg, &fb) < 0)
76
return CR_ERROR_COLOR;
78
*r = (int)(fr * 255.99);
79
*g = (int)(fg * 255.99);
80
*b = (int)(fb * 255.99);
85
if (G_strcasecmp(value, "default") == 0) {
90
if (G_strcasecmp(value, "nv") == 0) {
95
if (sscanf(value, "%lf%c", &x, &c) == 2 && c == '%') {
97
return CR_ERROR_PERCENT;
99
*val = min + (max - min) * (x / 100);
104
if (sscanf(value, "%lf", val) == 1) {
109
return CR_ERROR_VALUE;
113
\brief Parse color rule
117
\return pointer to buffer with error message
119
const char *Rast_parse_color_rule_error(int code)
124
case CR_ERROR_SYNTAX:
125
return _("syntax error");
127
return _("R/G/B not in range 0-255");
129
return _("invalid color name");
130
case CR_ERROR_PERCENT:
131
return _("percentage not in range 0-100");
133
return _("invalid value");
135
return _("unknown error");
140
\brief Read color rule
143
\param min, max min & max values (used only when color rules are in percentage)
145
\param[out] r,g,b color values
153
int Rast_read_color_rule(void *closure, DCELL min, DCELL max,
154
DCELL * val, int *r, int *g, int *b,
155
int *norm, int *nval, int *dflt)
161
*norm = *nval = *dflt = 0;
164
if (!G_getl2(buf, sizeof(buf), fp))
168
G_debug(5, "color buf = [%s]", buf);
176
Rast_parse_color_rule(min, max, buf, val, r, g, b, norm, nval,
181
G_fatal_error(_("bad rule (%s): [%s]"),
182
Rast_parse_color_rule_error(ret), buf);
189
\brief Read color rules from file
191
\param[out] colors pointer to Colors structure
192
\param min, max min & max values (used only when color rules are in percentage)
193
\param read_rule pointer to read_rule_fn structure
199
int Rast_read_color_rules(struct Colors *colors, DCELL min, DCELL max,
200
read_rule_fn * read_rule, void *closure)
202
struct rule *rule = NULL;
204
struct rule dflt, null;
205
int set, is_null, is_dflt, r, g, b;
210
read_rule = Rast_read_color_rule;
212
Rast_init_colors(colors);
215
dflt.r = dflt.g = dflt.b = dflt.set = 0;
216
null.r = null.g = null.b = null.set = 0;
219
(closure, min, max, &val, &r, &g, &b, &set, &is_null, &is_dflt)) {
224
rule = G_realloc(rule, nrules * sizeof(struct rule));
243
const struct rule *p = &rule[0];
245
Rast_set_d_color(p->val, p->r, p->g, p->b, colors);
248
for (n = 1; n < nrules; n++) {
249
struct rule *lo = &rule[n - 1];
250
struct rule *hi = &rule[n];
252
Rast_add_d_color_rule(&lo->val, lo->r, lo->g, lo->b,
253
&hi->val, hi->r, hi->g, hi->b, colors);
258
/* null value and default color set up, if rules are set up by user */
260
Rast_set_null_value_color(null.r, null.g, null.b, colors);
263
Rast_set_default_color(dflt.r, dflt.g, dflt.b, colors);
268
static int load_rules_file(struct Colors *colors, const char *path, DCELL min,
274
fp = fopen(path, "r");
280
Rast_read_color_rules(colors, min, max, Rast_read_color_rule,
289
\brief Load color rules from file
291
\param[out] colors pointer to Colors structure
292
\param path path to the color rules file
293
\param min, max min & max values (used only when color rules are in percentage)
298
int Rast_load_colors(struct Colors *colors, const char *path, CELL min,
301
return load_rules_file(colors, path, (DCELL) min, (DCELL) max);
305
\brief Load color floating-point rules from file
307
\param[out] colors pointer to Colors structure
308
\param path path to the color rules file
309
\param min, max min & max values (used only when color rules are in percentage)
314
int Rast_load_fp_colors(struct Colors *colors, const char *path, DCELL min,
317
return load_rules_file(colors, path, min, max);
320
static void load_rules_name(struct Colors *colors, const char *name,
321
DCELL min, DCELL max)
323
char path[GPATH_MAX];
325
sprintf(path, "%s/etc/colors/%s", G_gisbase(), name);
327
if (!load_rules_file(colors, path, min, max))
328
G_fatal_error(_("Unable to load color rules <%s>"), name);
332
\brief Load color rules from predefined color table
334
\param[out] colors pointer to Colors structure
335
\param name name of color table to load
336
\param min, max min & max values (used only when color rules are in percentage)
338
void Rast_make_colors(struct Colors *colors, const char *name, CELL min,
341
return load_rules_name(colors, name, (DCELL) min, (DCELL) max);
345
\brief Load color rules from predefined floating-point color table
347
\param[out] colors pointer to Colors structure
348
\param name name of color table to load
349
\param min, max min & max values (used only when color rules are in percentage)
351
void Rast_make_fp_colors(struct Colors *colors, const char *name, DCELL min,
354
return load_rules_name(colors, name, min, max);