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