color_get.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*!
  2. * \file lib/raster/color_get.c
  3. *
  4. * \brief Raster Library - Get colors from a raster map.
  5. *
  6. * (C) 2001-2009 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 Original author CERL
  12. */
  13. #include <grass/gis.h>
  14. #include <grass/raster.h>
  15. /*!
  16. * \brief Gets color from raster map
  17. *
  18. * Looks up the rgb colors for <i>rast</i> in the color table
  19. * <i>colors</i>.
  20. *
  21. * The <i>red, green</i>, and <i>blue</i> intensities for the color
  22. * associated with category <i>n</i> are extracted from the
  23. * <i>colors</i> structure. The intensities will be in the range 0 -
  24. * 255. Also works for null cells.
  25. *
  26. * \param rast raster cell value
  27. * \param[out] red red value
  28. * \param[out] grn green value
  29. * \param[out] blu blue value
  30. * \param colors pointer to Colors structure which holds color info
  31. * \param map_type map type (CELL, FCELL, DCELL)
  32. *
  33. * \return 1 if color is set
  34. * \return 0 if color is not set
  35. */
  36. int Rast_get_color(const void *rast,
  37. int *red, int *grn, int *blu,
  38. struct Colors *colors, RASTER_MAP_TYPE map_type)
  39. {
  40. unsigned char r, g, b, set;
  41. Rast_lookup_colors(rast, &r, &g, &b, &set, 1, colors, map_type);
  42. *red = (int)r;
  43. *grn = (int)g;
  44. *blu = (int)b;
  45. return (int)set;
  46. }
  47. /*!
  48. * \brief Gets color from raster map (CELL)
  49. *
  50. * Looks up the rgb colors for <i>rast</i> in the color table
  51. * <i>colors</i>.
  52. *
  53. * \param rast raster cell value
  54. * \param[out] red red value
  55. * \param[out] grn green value
  56. * \param[out] blu blue value
  57. * \param colors pointer to Colors structure which holds color info
  58. *
  59. * \return 1 if color is set
  60. * \return 0 if color is not set
  61. */
  62. int Rast_get_c_color(const CELL * rast,
  63. int *red, int *grn, int *blu, struct Colors *colors)
  64. {
  65. return Rast_get_color(rast, red, grn, blu, colors, CELL_TYPE);
  66. }
  67. /*!
  68. * \brief Gets color from raster map (FCELL)
  69. *
  70. * Looks up the rgb colors for <i>rast</i> in the color table
  71. * <i>colors</i>.
  72. *
  73. * \param rast raster cell value
  74. * \param[out] red red value
  75. * \param[out] grn green value
  76. * \param[out] blu blue value
  77. * \param colors pointer to Colors structure which holds color info
  78. *
  79. * \return 1 if color is set
  80. * \return 0 if color is not set
  81. */
  82. int Rast_get_f_color(const FCELL * rast,
  83. int *red, int *grn, int *blu, struct Colors *colors)
  84. {
  85. return Rast_get_color(rast, red, grn, blu, colors, FCELL_TYPE);
  86. }
  87. /*!
  88. * \brief Gets color from raster map (DCELL)
  89. *
  90. * Looks up the rgb colors for <i>rast</i> in the color table
  91. * <i>colors</i>.
  92. *
  93. * \param rast raster cell value
  94. * \param[out] red red value
  95. * \param[out] grn green value
  96. * \param[out] blu blue value
  97. * \param colors pointer to Colors structure which holds color info
  98. *
  99. * \return 1 if color is set
  100. * \return 0 if color is not set
  101. */
  102. int Rast_get_d_color(const DCELL * rast,
  103. int *red, int *grn, int *blu, struct Colors *colors)
  104. {
  105. return Rast_get_color(rast, red, grn, blu, colors, DCELL_TYPE);
  106. }
  107. /*!
  108. * \brief Gets color for null value.
  109. *
  110. * Puts the red, green, and blue components of <i>colors</i> for the
  111. * NULL-value into <i>red, grn, and blu</i>.
  112. *
  113. * \param[out] red red value
  114. * \param[out] grn green value
  115. * \param[out] blu blue value
  116. * \param colors pointer to Colors structure which holds color info
  117. */
  118. void Rast_get_null_value_color(int *red, int *grn, int *blu,
  119. const struct Colors *colors)
  120. {
  121. if (colors->null_set) {
  122. *red = (int)colors->null_red;
  123. *grn = (int)colors->null_grn;
  124. *blu = (int)colors->null_blu;
  125. }
  126. else if (colors->undef_set) {
  127. *red = (int)colors->undef_red;
  128. *grn = (int)colors->undef_grn;
  129. *blu = (int)colors->undef_blu;
  130. }
  131. else
  132. *red = *blu = *grn = 255; /* white */
  133. }
  134. /*!
  135. * \brief Gets default color.
  136. *
  137. * Puts the red, green, and blue components of the <tt>"default"</tt>
  138. * color into <i>red, grn, and blu</i>.
  139. *
  140. * \param[out] red red value
  141. * \param[out] grn green value
  142. * \param[out] blu blue value
  143. * \param colors pointer to Colors structure which holds color info
  144. */
  145. void Rast_get_default_color(int *red, int *grn, int *blu,
  146. const struct Colors *colors)
  147. {
  148. if (colors->undef_set) {
  149. *red = (int)colors->undef_red;
  150. *grn = (int)colors->undef_grn;
  151. *blu = (int)colors->undef_blu;
  152. }
  153. else
  154. *red = *blu = *grn = 255; /* white */
  155. }