22 #include <grass/gis.h>
24 #define THRESHOLD .0000000000000000000000000000005
27 static int write_new_colors(FILE *,
struct Colors *);
28 static int write_rules(FILE *,
struct _Color_Rule_ *, DCELL, DCELL);
29 static int write_old_colors(FILE *,
struct Colors *);
30 static int forced_write_old_colors(FILE *,
struct Colors *);
31 static int format_min(
char *,
double);
32 static int format_max(
char *,
double);
78 struct Colors *colors)
81 char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
86 if (strcmp(xmapset, mapset) != 0)
95 sprintf(element,
"colr2/%s", mapset);
96 if (strcmp(mapset,
G_mapset()) == 0) {
98 strcpy(element,
"colr");
110 if (
getenv(
"FORCE_GRASS3_COLORS"))
111 return forced_write_old_colors(fd, colors);
112 else if (colors->version < 0)
113 return write_old_colors(fd, colors);
115 return write_new_colors(fd, colors);
118 static int write_new_colors(FILE * fd,
struct Colors *colors)
120 char str1[100], str2[100];
122 format_min(str1, (
double)colors->cmin);
123 format_max(str2, (
double)colors->cmax);
124 fprintf(fd,
"%% %s %s\n", str1, str2);
127 sprintf(str2,
"%.10f", (
double)colors->shift);
129 fprintf(fd,
"shift:%s\n", str2);
132 fprintf(fd,
"invert\n");
134 if (colors->null_set) {
135 fprintf(fd,
"nv:%d", colors->null_red);
136 if (colors->null_red != colors->null_grn || colors->null_red
138 fprintf(fd,
":%d:%d", colors->null_grn, colors->null_blu);
141 if (colors->undef_set) {
142 fprintf(fd,
"*:%d", colors->undef_red);
143 if (colors->undef_red != colors->undef_grn || colors->undef_red
144 != colors->undef_blu)
145 fprintf(fd,
":%d:%d", colors->undef_grn, colors->undef_blu);
148 if (colors->modular.rules) {
149 fprintf(fd,
"%s\n",
"%%");
150 write_rules(fd, colors->modular.rules, colors->cmin, colors->cmax);
151 fprintf(fd,
"%s\n",
"%%");
153 if (colors->fixed.rules)
154 write_rules(fd, colors->fixed.rules, colors->cmin, colors->cmax);
159 static int write_rules(FILE * fd,
struct _Color_Rule_ *crules, DCELL dmin, DCELL
dmax
162 struct _Color_Rule_ *
rule;
171 for (; rule; rule = rule->prev) {
172 if (rule->low.value == dmin)
173 format_min(str, (
double)rule->low.value);
175 sprintf(str,
"%.10f", (
double)rule->low.value);
178 fprintf(fd,
"%s:%d", str, (
int)rule->low.red);
179 if (rule->low.red != rule->low.grn || rule->low.red != rule->low.blu)
180 fprintf(fd,
":%d:%d", rule->low.grn, rule->low.blu);
182 if (rule->high.value == dmax || rule->low.value != rule->high.value) {
183 if (rule->high.value == dmax)
184 format_max(str, (
double)rule->high.value);
186 sprintf(str,
"%.10f", (
double)rule->high.value);
189 fprintf(fd,
" %s:%d", str, (
int)rule->high.red);
190 if (rule->high.red != rule->high.grn ||
191 rule->high.red != rule->high.blu)
192 fprintf(fd,
":%d:%d", rule->high.grn, rule->high.blu);
200 static int write_old_colors(FILE * fd,
struct Colors *colors)
204 fprintf(fd,
"#%ld first color\n", (
long)colors->fixed.min);
205 if (colors->null_set) {
206 fprintf(fd,
"%d %d %d\n",
207 (
int)colors->null_red,
208 (
int)colors->null_grn, (
int)colors->null_blu);
211 fprintf(fd,
"255 255 255\n");
213 n = colors->fixed.max - colors->fixed.min + 1;
215 for (i = 0; i < n; i++) {
216 fprintf(fd,
"%d", (
int)colors->fixed.lookup.red[i]);
217 if (colors->fixed.lookup.red[i] != colors->fixed.lookup.grn[i]
218 || colors->fixed.lookup.red[i] != colors->fixed.lookup.blu[i])
219 fprintf(fd,
" %d %d",
220 (
int)colors->fixed.lookup.grn[i],
221 (
int)colors->fixed.lookup.blu[i]);
228 static int forced_write_old_colors(FILE * fd,
struct Colors *colors)
233 fprintf(fd,
"#%ld first color\n", (
long)colors->cmin);
235 fprintf(fd,
"%d %d %d\n", red, grn, blu);
237 for (cat = colors->cmin; cat <= colors->cmax; cat++) {
239 fprintf(fd,
"%d", red);
240 if (red != grn || red != blu)
241 fprintf(fd,
" %d %d", grn, blu);
248 static int format_min(
char *str,
double dval)
254 sscanf(str,
"%lf", &dtmp);
263 static int format_max(
char *str,
double dval)
269 sscanf(str,
"%lf", &dtmp);