color_get.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * \file color_get.c
  3. *
  4. * \brief GIS Library - Functions to get colors from a raster map.
  5. *
  6. * (C) 2001-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author GRASS GIS Development Team
  12. *
  13. * \date 1999-2008
  14. */
  15. #include <grass/gis.h>
  16. /**
  17. * \brief Get a category color.
  18. *
  19. * The <b>red, green</b>, and
  20. * <b>blue</b> intensities for the color associated with category <b>n</b>
  21. * are extracted from the <b>colors</b> structure. The intensities will be in
  22. * the range 0 ­- 255. Also works for null cells.
  23. *
  24. * \param[in] n CELL to get color from
  25. * \param[out] red red value
  26. * \param[out] grn green value
  27. * \param[out] blu blue value
  28. * \param[in] colors Colors struct
  29. * \return int
  30. */
  31. int G_get_color(CELL n, int *red, int *grn, int *blu, struct Colors *colors)
  32. {
  33. CELL cat;
  34. unsigned char r, g, b, set;
  35. cat = n;
  36. G_lookup_colors(&cat, &r, &g, &b, &set, 1, colors);
  37. *red = (int)r;
  38. *grn = (int)g;
  39. *blu = (int)b;
  40. return (int)set;
  41. }
  42. /**
  43. * \brief Gets color from raster.
  44. *
  45. * Looks up the rgb colors for
  46. * <em>rast</em> in the color table <em>colors</em>
  47. *
  48. * \param[in] rast input raster map
  49. * \param[out] red red value
  50. * \param[out] grn green value
  51. * \param[out] blu blue value
  52. * \param[in] colors Colors struct
  53. * \param[in] map_type type of map (CELL_TYPE,FCELL_TYPE,DCELL_TYPE)
  54. * \return int
  55. */
  56. int G_get_raster_color(const void *rast,
  57. int *red, int *grn, int *blu,
  58. struct Colors *colors, RASTER_MAP_TYPE map_type)
  59. {
  60. unsigned char r, g, b, set;
  61. G_lookup_raster_colors(rast, &r, &g, &b, &set, 1, colors, map_type);
  62. *red = (int)r;
  63. *grn = (int)g;
  64. *blu = (int)b;
  65. return (int)set;
  66. }
  67. /**
  68. * \brief Gets color for a CELL raster.
  69. *
  70. * Looks up the rgb colors for CELL
  71. * <em>rast</em> in the color table <em>colors</em>
  72. *
  73. * \param[in] rast input CELL raster
  74. * \param[out] red red value
  75. * \param[out] grn green value
  76. * \param[out] blu blue value
  77. * \param[in] colors Colors struct
  78. * \return int
  79. */
  80. int G_get_c_raster_color(const CELL * rast,
  81. int *red, int *grn, int *blu, struct Colors *colors)
  82. {
  83. return G_get_raster_color(rast, red, grn, blu, colors, CELL_TYPE);
  84. }
  85. /**
  86. * \brief Gets color for a FCELL raster.
  87. *
  88. * Looks up the rgb colors for FCELL <em>rast</em> in the color table
  89. * <em>colors</em>
  90. *
  91. * \param[in] rast input FCELL raster
  92. * \param[out] red red value
  93. * \param[out] grn green value
  94. * \param[out] blu blue value
  95. * \param[in] colors Colors struct
  96. * \return int
  97. */
  98. int G_get_f_raster_color(const FCELL * rast,
  99. int *red, int *grn, int *blu, struct Colors *colors)
  100. {
  101. return G_get_raster_color(rast, red, grn, blu, colors, FCELL_TYPE);
  102. }
  103. /**
  104. * \brief Gets color for a DCELL raster.
  105. *
  106. * Looks up the rgb colors for DCELL <em>rast</em> in the color table
  107. * <em>colors</em>
  108. *
  109. * \param[in] rast input DCELL raster
  110. * \param[out] red red value
  111. * \param[out] grn green value
  112. * \param[out] blu blue value
  113. * \param[in] colors Colors struct
  114. * \return int
  115. */
  116. int G_get_d_raster_color(const DCELL * rast,
  117. int *red, int *grn, int *blu, struct Colors *colors)
  118. {
  119. return G_get_raster_color(rast, red, grn, blu, colors, DCELL_TYPE);
  120. }
  121. /**
  122. * \brief Gets color for null value.
  123. *
  124. * Puts the red, green, and blue components of <b>colors</b> for the
  125. * NULL-value into <b>red, grn, and blu</b>.
  126. *
  127. * \param[out] red red value
  128. * \param[out] grn green value
  129. * \param[out] blu blue value
  130. * \param[in] colors Colors struct
  131. * \return
  132. */
  133. void G_get_null_value_color(int *red, int *grn, int *blu,
  134. const struct Colors *colors)
  135. {
  136. if (colors->null_set) {
  137. *red = (int)colors->null_red;
  138. *grn = (int)colors->null_grn;
  139. *blu = (int)colors->null_blu;
  140. }
  141. else if (colors->undef_set) {
  142. *red = (int)colors->undef_red;
  143. *grn = (int)colors->undef_grn;
  144. *blu = (int)colors->undef_blu;
  145. }
  146. else
  147. *red = *blu = *grn = 255; /* white */
  148. }
  149. /**
  150. * \brief Gets default color.
  151. *
  152. * Puts the red, green, and blue components of the
  153. * <tt>"default"</tt> color into <b>red, grn, and blu</b>.
  154. *
  155. * \param[out] red red value
  156. * \param[out] grn green value
  157. * \param[out] blu blue value
  158. * \param[in] colors Colors struct
  159. * \return
  160. */
  161. void G_get_default_color(int *red, int *grn, int *blu,
  162. const struct Colors *colors)
  163. {
  164. if (colors->undef_set) {
  165. *red = (int)colors->undef_red;
  166. *grn = (int)colors->undef_grn;
  167. *blu = (int)colors->undef_blu;
  168. }
  169. else
  170. *red = *blu = *grn = 255; /* white */
  171. }