tran_colr.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Takes a color name in ascii, returns the color number for that color.
  2. * returns 0 if color is not known.
  3. */
  4. #include <string.h>
  5. #include <grass/display.h>
  6. #include <grass/colors.h>
  7. #include <grass/raster.h>
  8. #include <grass/glocale.h>
  9. #include "driver.h"
  10. static struct color_rgb *colors;
  11. static int ncolors;
  12. static int nalloc;
  13. /*!
  14. * \brief color name and suggested number to actual number
  15. *
  16. * Takes a color <b>name</b> or <b>red:green:blue</b> code in ascii
  17. * and a <b>suggested color index</b>.
  18. * If the color is a standard preallocated color it returns the color number for that color.
  19. * Otherwise (if the color is not standard) it sets the color of the supplied index to
  20. * the specified color.
  21. * Returns -1 if color is not known and 0 if the color is none.
  22. *
  23. * \param name_or_code
  24. * \param suggested_color_index
  25. * \return int
  26. */
  27. static int translate_or_add_color(const char *str)
  28. {
  29. int num_names = G_num_standard_color_names();
  30. int index;
  31. int red, grn, blu;
  32. int i, ret;
  33. char lowerstr[MAX_COLOR_LEN];
  34. /* Make the color string lowercase for display colors */
  35. strcpy(lowerstr, str);
  36. G_chop(lowerstr);
  37. G_tolcase(lowerstr);
  38. for (i = 0; i < num_names; i++) {
  39. const struct color_name *name = G_standard_color_name(i);
  40. if (G_strcasecmp(str, name->name) == 0)
  41. return name->number;
  42. }
  43. if (!nalloc) {
  44. ncolors = G_num_standard_colors();
  45. nalloc = 2 * ncolors;
  46. colors = G_malloc(nalloc * sizeof(struct color_rgb));
  47. for (i = 0; i < ncolors; i++)
  48. colors[i] = G_standard_color_rgb(i);
  49. }
  50. ret = G_str_to_color(str, &red, &grn, &blu);
  51. /* None color */
  52. if (ret == 2)
  53. return 0;
  54. if (ret != 1)
  55. return -1;
  56. for (i = 1; i < ncolors; i++)
  57. if (colors[i].r == red && colors[i].g == grn && colors[i].b == blu)
  58. return i;
  59. if (ncolors >= nalloc) {
  60. nalloc *= 2;
  61. colors = G_realloc(colors, nalloc * sizeof(struct color_rgb));
  62. }
  63. index = ncolors++;
  64. colors[index].r = red;
  65. colors[index].g = grn;
  66. colors[index].b = blu;
  67. return index;
  68. }
  69. /*!
  70. * \brief color option text to usable color number
  71. *
  72. * Converts or looks up the color provided in the string.
  73. * Returns a color number usable by D_use_color.
  74. * If the color does not exist exits with a fatal error and message.
  75. * If the color is none and none_acceptable is not true exits with
  76. * a fatal error and message.
  77. *
  78. * \param name_or_code
  79. * \param none_acceptable
  80. * \return int
  81. */
  82. int D_parse_color(const char *str, int none_acceptable)
  83. {
  84. int color;
  85. color = translate_or_add_color(str);
  86. if (color == -1)
  87. G_fatal_error(_("[%s]: No such color"), str);
  88. if (color == 0 && !none_acceptable)
  89. G_fatal_error(_("[%s]: No such color"), str);
  90. return color;
  91. }
  92. /*!
  93. * \brief color name to number
  94. *
  95. * Takes a
  96. * color <b>name</b> in ascii and returns the color number for that color.
  97. * Returns 0 if color is not known. The color number returned is for lines and
  98. * text, not raster graphics.
  99. *
  100. * \param name
  101. * \return int
  102. */
  103. int D_translate_color(const char *str)
  104. {
  105. return D_parse_color(str, 0);
  106. }
  107. /*!
  108. * \brief draw with a color from D_parse_color
  109. *
  110. * Calls R_color or R_standard_color to use the color provided by
  111. * D_parse_color. Returns 1 if color can be used to draw (is
  112. * good and isn't none), 0 otherwise.
  113. *
  114. * \param color
  115. * \return int
  116. */
  117. int D_use_color(int color)
  118. {
  119. if (color <= 0)
  120. return 0;
  121. if (color < G_num_standard_colors()) {
  122. COM_Standard_color(color);
  123. return 1;
  124. }
  125. if (color < ncolors) {
  126. const struct color_rgb *c = &colors[color];
  127. D_RGB_color(c->r, c->g, c->b);
  128. return 1;
  129. }
  130. return 0;
  131. }
  132. /*!
  133. * \brief get RGB values from color number
  134. *
  135. * Translates the color number provided by D_parse_color
  136. * into 0-255 RGB values.
  137. *
  138. * Returns 1 if color can be used to draw (is good and
  139. * isn't 'none'), 0 otherwise.
  140. *
  141. * \param color_number
  142. * \param red
  143. * \param green
  144. * \param blue
  145. *
  146. * \return int
  147. */
  148. int D_color_number_to_RGB(int color, int *r, int *g, int *b)
  149. {
  150. const struct color_rgb *c;
  151. if (color <= 0)
  152. return 0;
  153. if (color < G_num_standard_colors()) {
  154. struct color_rgb col = G_standard_color_rgb(color);
  155. if (r)
  156. *r = col.r;
  157. if (g)
  158. *g = col.g;
  159. if (b)
  160. *b = col.b;
  161. return 1;
  162. }
  163. if (color >= ncolors)
  164. return 0;
  165. c = &colors[color];
  166. if (r)
  167. *r = c->r;
  168. if (g)
  169. *g = c->g;
  170. if (b)
  171. *b = c->b;
  172. return 1;
  173. }
  174. void D_RGB_color(int red, int grn, int blu)
  175. {
  176. COM_Color_RGB(red, grn, blu);
  177. }