color_rules.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /****************************************************************************
  2. *
  3. * MODULE: gis library
  4. * AUTHOR(S): Glynn Clements <glynn@gclements.plus.com>
  5. * COPYRIGHT: (C) 2007 Glynn Clements and the GRASS Development Team
  6. *
  7. * NOTE: Based upon r.colors/rules.c
  8. * The colors are stored in ./colors/
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. *****************************************************************************/
  21. #include <stdio.h>
  22. #include <grass/gis.h>
  23. #include <grass/glocale.h>
  24. struct rule
  25. {
  26. int set;
  27. int r, g, b;
  28. DCELL val;
  29. };
  30. enum rule_error
  31. {
  32. CR_OK = 0,
  33. CR_ERROR_SYNTAX,
  34. CR_ERROR_RGB,
  35. CR_ERROR_COLOR,
  36. CR_ERROR_PERCENT,
  37. CR_ERROR_VALUE,
  38. };
  39. int G_parse_color_rule(DCELL min, DCELL max, const char *buf,
  40. DCELL * val, int *r, int *g, int *b,
  41. int *norm, int *nval, int *dflt)
  42. {
  43. char value[80], color[80];
  44. double x;
  45. char c;
  46. *norm = *nval = *dflt = 0;
  47. if (sscanf(buf, "%s %[^\n]", value, color) != 2)
  48. return CR_ERROR_SYNTAX;
  49. G_chop(color);
  50. if (sscanf(color, "%d:%d:%d", r, g, b) == 3 ||
  51. sscanf(color, "%d %d %d", r, g, b) == 3) {
  52. if (*r < 0 || *r > 255 || *g < 0 || *g > 255 || *b < 0 || *b > 255)
  53. return CR_ERROR_RGB;
  54. }
  55. else {
  56. float fr, fg, fb;
  57. if (G_color_values(color, &fr, &fg, &fb) < 0)
  58. return CR_ERROR_COLOR;
  59. *r = (int)(fr * 255.99);
  60. *g = (int)(fg * 255.99);
  61. *b = (int)(fb * 255.99);
  62. }
  63. G_chop(value);
  64. if (G_strcasecmp(value, "default") == 0) {
  65. *dflt = 1;
  66. return CR_OK;
  67. }
  68. if (G_strcasecmp(value, "nv") == 0) {
  69. *nval = 1;
  70. return CR_OK;
  71. }
  72. if (sscanf(value, "%lf%c", &x, &c) == 2 && c == '%') {
  73. if (x < 0 || x > 100)
  74. return CR_ERROR_PERCENT;
  75. *val = min + (max - min) * (x / 100);
  76. *norm = 1;
  77. return CR_OK;
  78. }
  79. if (sscanf(value, "%lf", val) == 1) {
  80. *norm = 1;
  81. return CR_OK;
  82. }
  83. return CR_ERROR_VALUE;
  84. }
  85. const char *G_parse_color_rule_error(int code)
  86. {
  87. switch (code) {
  88. case CR_OK:
  89. return "";
  90. case CR_ERROR_SYNTAX:
  91. return _("syntax error");
  92. case CR_ERROR_RGB:
  93. return _("R/G/B not in range 0-255");
  94. case CR_ERROR_COLOR:
  95. return _("invalid color name");
  96. case CR_ERROR_PERCENT:
  97. return _("percentage not in range 0-100");
  98. case CR_ERROR_VALUE:
  99. return _("invalid value");
  100. default:
  101. return _("unknown error");
  102. }
  103. }
  104. int G_read_color_rule(void *closure, DCELL min, DCELL max,
  105. DCELL * val, int *r, int *g, int *b,
  106. int *norm, int *nval, int *dflt)
  107. {
  108. char buf[1024];
  109. FILE *fp = closure;
  110. int ret;
  111. *norm = *nval = *dflt = 0;
  112. for (;;) {
  113. if (!G_getl2(buf, sizeof(buf), fp))
  114. return 0;
  115. G_strip(buf);
  116. G_debug(5, "color buf = [%s]", buf);
  117. if (*buf == '\0')
  118. continue;
  119. if (*buf == '#')
  120. continue;
  121. ret =
  122. G_parse_color_rule(min, max, buf, val, r, g, b, norm, nval, dflt);
  123. if (ret == 0)
  124. return 1;
  125. G_fatal_error(_("bad rule (%s): [%s]"),
  126. G_parse_color_rule_error(ret), buf);
  127. }
  128. return 0;
  129. }
  130. int G_read_color_rules(struct Colors *colors, DCELL min, DCELL max,
  131. read_rule_fn * read_rule, void *closure)
  132. {
  133. struct rule *rule = NULL;
  134. int nrules = 0;
  135. struct rule dflt, null;
  136. int set, is_null, is_dflt, r, g, b;
  137. DCELL val;
  138. int n;
  139. if (!read_rule)
  140. read_rule = G_read_color_rule;
  141. G_init_colors(colors);
  142. /* initialization */
  143. dflt.r = dflt.g = dflt.b = dflt.set = 0;
  144. null.r = null.g = null.b = null.set = 0;
  145. while ((*read_rule)
  146. (closure, min, max, &val, &r, &g, &b, &set, &is_null, &is_dflt)) {
  147. struct rule *p;
  148. if (set) {
  149. n = nrules++;
  150. rule = G_realloc(rule, nrules * sizeof(struct rule));
  151. p = &rule[n];
  152. }
  153. else if (is_dflt)
  154. p = &dflt;
  155. else if (is_null)
  156. p = &null;
  157. p->r = r;
  158. p->g = g;
  159. p->b = b;
  160. p->set = 1;
  161. p->val = val;
  162. }
  163. if (nrules == 0)
  164. return 0;
  165. if (nrules == 1) {
  166. const struct rule *p = &rule[0];
  167. G_set_d_color(p->val, p->r, p->g, p->b, colors);
  168. }
  169. for (n = 1; n < nrules; n++) {
  170. struct rule *lo = &rule[n - 1];
  171. struct rule *hi = &rule[n];
  172. G_add_d_raster_color_rule(&lo->val, lo->r, lo->g, lo->b,
  173. &hi->val, hi->r, hi->g, hi->b, colors);
  174. }
  175. /* null value and default color set up, if rules are set up by user */
  176. if (null.set)
  177. G_set_null_value_color(null.r, null.g, null.b, colors);
  178. if (dflt.set)
  179. G_set_default_color(dflt.r, dflt.g, dflt.b, colors);
  180. return 1;
  181. }
  182. static int load_rules_file(struct Colors *colors, const char *path, DCELL min, DCELL max)
  183. {
  184. FILE *fp;
  185. int ret;
  186. fp = fopen(path, "r");
  187. if (!fp)
  188. return 0;
  189. ret = G_read_color_rules(colors, min, max, G_read_color_rule, (void *)fp);
  190. fclose(fp);
  191. return ret;
  192. }
  193. int G_load_colors(struct Colors *colors, const char *path, CELL min, CELL max)
  194. {
  195. return load_rules_file(colors, path, (DCELL) min, (DCELL) max);
  196. }
  197. int G_load_fp_colors(struct Colors *colors, const char *path, DCELL min, DCELL max)
  198. {
  199. return load_rules_file(colors, path, min, max);
  200. }
  201. static void load_rules_name(struct Colors *colors, const char *name, DCELL min, DCELL max)
  202. {
  203. char path[GPATH_MAX];
  204. sprintf(path, "%s/etc/colors/%s", G_gisbase(), name);
  205. if (!load_rules_file(colors, path, min, max))
  206. G_fatal_error(_("Unable to load color rules <%s>"), name);
  207. }
  208. void G_make_colors(struct Colors *colors, const char *name, CELL min, CELL max)
  209. {
  210. return load_rules_name(colors, name, (DCELL) min, (DCELL) max);
  211. }
  212. void G_make_fp_colors(struct Colors *colors, const char *name, DCELL min, DCELL max)
  213. {
  214. return load_rules_name(colors, name, min, max);
  215. }