22 #include <grass/gis.h>
24 static int write_new_colors(FILE *,
struct Colors *);
25 static int write_rules(FILE *,
struct _Color_Rule_ *, DCELL, DCELL);
26 static int write_old_colors(FILE *,
struct Colors *);
27 static int forced_write_old_colors(FILE *,
struct Colors *);
28 static int format_min(
char *,
double);
29 static int format_max(
char *,
double);
75 struct Colors *colors)
83 if (strcmp(xmapset, mapset) != 0)
92 sprintf(element,
"colr2/%s", mapset);
93 if (strcmp(mapset,
G_mapset()) == 0) {
95 strcpy(element,
"colr");
107 if (
getenv(
"FORCE_GRASS3_COLORS"))
108 return forced_write_old_colors(fd, colors);
109 else if (colors->version < 0)
110 return write_old_colors(fd, colors);
112 return write_new_colors(fd, colors);
115 static int write_new_colors(FILE *
fd,
struct Colors *colors)
117 char str1[100], str2[100];
119 format_min(str1, (
double)colors->cmin);
120 format_max(str2, (
double)colors->cmax);
121 fprintf(fd,
"%% %s %s\n", str1, str2);
124 sprintf(str2,
"%.15g", (
double)colors->shift);
126 fprintf(fd,
"shift:%s\n", str2);
129 fprintf(fd,
"invert\n");
131 if (colors->null_set) {
132 fprintf(fd,
"nv:%d", colors->null_red);
133 if (colors->null_red != colors->null_grn || colors->null_red
135 fprintf(fd,
":%d:%d", colors->null_grn, colors->null_blu);
138 if (colors->undef_set) {
139 fprintf(fd,
"*:%d", colors->undef_red);
140 if (colors->undef_red != colors->undef_grn || colors->undef_red
141 != colors->undef_blu)
142 fprintf(fd,
":%d:%d", colors->undef_grn, colors->undef_blu);
145 if (colors->modular.rules) {
146 fprintf(fd,
"%s\n",
"%%");
147 write_rules(fd, colors->modular.rules, colors->cmin, colors->cmax);
148 fprintf(fd,
"%s\n",
"%%");
150 if (colors->fixed.rules)
151 write_rules(fd, colors->fixed.rules, colors->cmin, colors->cmax);
156 static int write_rules(FILE * fd,
struct _Color_Rule_ *crules, DCELL
dmin, DCELL
dmax
159 struct _Color_Rule_ *
rule;
168 for (; rule; rule = rule->prev) {
169 if (rule->low.value == dmin)
170 format_min(str, (
double)rule->low.value);
172 sprintf(str,
"%.15g", (
double)rule->low.value);
175 fprintf(fd,
"%s:%d", str, (
int)rule->low.red);
176 if (rule->low.red != rule->low.grn || rule->low.red != rule->low.blu)
177 fprintf(fd,
":%d:%d", rule->low.grn, rule->low.blu);
179 if (rule->high.value == dmax || rule->low.value != rule->high.value) {
180 if (rule->high.value == dmax)
181 format_max(str, (
double)rule->high.value);
183 sprintf(str,
"%.15g", (
double)rule->high.value);
186 fprintf(fd,
" %s:%d", str, (
int)rule->high.red);
187 if (rule->high.red != rule->high.grn ||
188 rule->high.red != rule->high.blu)
189 fprintf(fd,
":%d:%d", rule->high.grn, rule->high.blu);
197 static int write_old_colors(FILE * fd,
struct Colors *colors)
201 fprintf(fd,
"#%ld first color\n", (
long)colors->fixed.min);
202 if (colors->null_set) {
203 fprintf(fd,
"%d %d %d\n",
204 (
int)colors->null_red,
205 (
int)colors->null_grn, (
int)colors->null_blu);
208 fprintf(fd,
"255 255 255\n");
210 n = colors->fixed.max - colors->fixed.min + 1;
212 for (i = 0; i <
n; i++) {
213 fprintf(fd,
"%d", (
int)colors->fixed.lookup.red[i]);
214 if (colors->fixed.lookup.red[i] != colors->fixed.lookup.grn[i]
215 || colors->fixed.lookup.red[i] != colors->fixed.lookup.blu[i])
216 fprintf(fd,
" %d %d",
217 (
int)colors->fixed.lookup.grn[i],
218 (
int)colors->fixed.lookup.blu[i]);
225 static int forced_write_old_colors(FILE * fd,
struct Colors *colors)
230 fprintf(fd,
"#%ld first color\n", (
long)colors->cmin);
232 fprintf(fd,
"%d %d %d\n", red, grn, blu);
234 for (cat = colors->cmin; cat <= colors->cmax; cat++) {
236 fprintf(fd,
"%d", red);
237 if (red != grn || red != blu)
238 fprintf(fd,
" %d %d", grn, blu);
245 static int format_min(
char *str,
double dval)
252 sscanf(str,
"%lf", &dtmp);
256 sprintf(str,
"%.15g", dval * (1 - GRASS_EPSILON));
258 sprintf(str,
"%.15g", dval * (1 + GRASS_EPSILON));
264 static int format_max(
char *str,
double dval)
271 sscanf(str,
"%lf", &dtmp);
275 sprintf(str,
"%.15g", dval * (1 + GRASS_EPSILON));
277 sprintf(str,
"%.15g", dval * (1 - GRASS_EPSILON));
char * G_mapset(void)
current mapset name
int G_write_colors(const char *name, const char *mapset, struct Colors *colors)
write map layer color table
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G_trim_decimal(char *buf)
Removes trailing zeros from decimal number.
int G_get_color(CELL n, int *red, int *grn, int *blu, struct Colors *colors)
Get a category color.
int G_remove(const char *element, const char *name)
Remove a database file.
FILE * G_fopen_new(const char *element, const char *name)
Open a new database file.
int G__write_colors(FILE *fd, struct Colors *colors)
int G__name_is_fully_qualified(const char *fullname, char *name, char *mapset)
Check if map name is fully qualified (map @ mapset)