rules.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /****************************************************************************
  2. *
  3. * MODULE: r.colors
  4. *
  5. * AUTHOR(S): Michael Shapiro - CERL
  6. * David Johnson
  7. *
  8. * PURPOSE: Allows creation and/or modification of the color table
  9. * for a raster map layer.
  10. *
  11. * COPYRIGHT: (C) 2006 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. ***************************************************************************/
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include "local_proto.h"
  23. /* color structure for default and null value */
  24. static int read_rule(void *, DCELL, DCELL, DCELL *, int *, int *, int *,
  25. int *, int *, int *);
  26. static void badrule(int, const char *, int);
  27. static int show_colors(FILE *);
  28. int read_color_rules(FILE * fp, struct Colors *colors, DCELL min, DCELL max,
  29. int is_fp)
  30. {
  31. DCELL rulemin, rulemax;
  32. if (isatty(fileno(fp))) {
  33. fprintf(stderr,
  34. _("Enter rules, \"end\" when done, \"help\" if you need it.\n"));
  35. if (is_fp) {
  36. char minstr[64], maxstr[64];
  37. sprintf(minstr, "%.15g", (double)min);
  38. sprintf(maxstr, "%.15g", (double)max);
  39. G_trim_decimal(minstr);
  40. G_trim_decimal(maxstr);
  41. fprintf(stderr, _("fp: Data range is %s to %s\n"), minstr,
  42. maxstr);
  43. }
  44. else
  45. fprintf(stderr, _("Data range is %ld to %ld\n"), (long)min,
  46. (long)max);
  47. }
  48. if (!Rast_read_color_rules(colors, min, max, read_rule, fp))
  49. return 0;
  50. Rast_get_d_color_range(&rulemin, &rulemax, colors);
  51. G_debug(3, "rulemin=%.3f rulemax=%.3f", rulemin, rulemax);
  52. if (rulemin > min || rulemax < max)
  53. G_warning(_("Your color rules do not cover the whole range of data!\n (rules %f to %f but data %f to %f)"),
  54. rulemin, rulemax, min, max);
  55. return 1;
  56. }
  57. static int read_rule(void *closure, DCELL min, DCELL max,
  58. DCELL * val, int *r, int *g, int *b,
  59. int *norm, int *nval, int *dflt)
  60. {
  61. FILE *fp = closure;
  62. int tty = isatty(fileno(fp));
  63. *norm = *nval = *dflt = 0;
  64. for (;;) {
  65. char buf[1024];
  66. int ret, i;
  67. if (tty)
  68. fprintf(stderr, "> ");
  69. if (!G_getl2(buf, sizeof(buf), fp))
  70. return 0;
  71. for (i = 0; buf[i]; i++)
  72. if (buf[i] == ',')
  73. buf[i] = ' ';
  74. G_strip(buf);
  75. if (*buf == '\0')
  76. continue;
  77. if (*buf == '#')
  78. continue;
  79. if (strncmp(buf, "end", 3) == 0)
  80. return 0;
  81. if (strncmp(buf, "help", 4) == 0) {
  82. fprintf(stderr, _("Enter a rule in one of these formats:\n"));
  83. fprintf(stderr, _(" val color\n"));
  84. fprintf(stderr, _(" n%% color\n"));
  85. fprintf(stderr, _(" nv color\n"));
  86. fprintf(stderr, _(" default color\n"));
  87. fprintf(stderr, _("color can be one of:\n"));
  88. show_colors(stderr);
  89. fprintf(stderr, _("or an R:G:B triplet, e.g.: 0:127:255\n"));
  90. continue;
  91. }
  92. ret =
  93. Rast_parse_color_rule(min, max, buf, val, r, g, b, norm, nval, dflt);
  94. if (ret == 0)
  95. return 1;
  96. badrule(tty, buf, ret);
  97. }
  98. return 0;
  99. }
  100. static void badrule(int tty, const char *s, int code)
  101. {
  102. const char *err = Rast_parse_color_rule_error(code);
  103. if (tty)
  104. G_warning(_("bad rule (%s); rule not added"), err);
  105. else
  106. G_fatal_error(_("bad rule (%s): [%s]"), err, s);
  107. }
  108. static int show_colors(FILE * fp)
  109. {
  110. int len;
  111. int i, n;
  112. const char *color;
  113. len = 0;
  114. for (i = 0; (color = G_color_name(i)); i++) {
  115. n = strlen(color) + 1;
  116. if (len + n > 78) {
  117. fprintf(fp, "\n");
  118. len = 0;
  119. }
  120. fprintf(fp, " %s", color);
  121. len += n;
  122. }
  123. fprintf(fp, "\n");
  124. return 0;
  125. }