GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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,
39  int *red, int *grn, int *blu,
40  struct Colors *colors, RASTER_MAP_TYPE map_type)
41 {
42  unsigned char r, g, b, set;
43 
44  Rast_lookup_colors(rast, &r, &g, &b, &set, 1, colors, map_type);
45 
46  *red = (int)r;
47  *grn = (int)g;
48  *blu = (int)b;
49 
50  return (int)set;
51 }
52 
53 /*!
54  * \brief Gets color from raster map (CELL)
55  *
56  * Looks up the rgb colors for <i>rast</i> in the color table
57  * <i>colors</i>.
58  *
59  * \param rast raster cell value
60  * \param[out] red red value
61  * \param[out] grn green value
62  * \param[out] blu blue value
63  * \param colors pointer to Colors structure which holds color info
64  *
65  * \return 1 if color is set
66  * \return 0 if color is not set
67  */
68 int Rast_get_c_color(const CELL * rast,
69  int *red, int *grn, int *blu, struct Colors *colors)
70 {
71  return Rast_get_color(rast, red, grn, blu, colors, CELL_TYPE);
72 }
73 
74 /*!
75  * \brief Gets color from raster map (FCELL)
76  *
77  * Looks up the rgb colors for <i>rast</i> in the color table
78  * <i>colors</i>.
79  *
80  * \param rast raster cell value
81  * \param[out] red red value
82  * \param[out] grn green value
83  * \param[out] blu blue value
84  * \param colors pointer to Colors structure which holds color info
85  *
86  * \return 1 if color is set
87  * \return 0 if color is not set
88  */
89 int Rast_get_f_color(const FCELL * rast,
90  int *red, int *grn, int *blu, struct Colors *colors)
91 {
92  return Rast_get_color(rast, red, grn, blu, colors, FCELL_TYPE);
93 }
94 
95 /*!
96  * \brief Gets color from raster map (DCELL)
97  *
98  * Looks up the rgb colors for <i>rast</i> in the color table
99  * <i>colors</i>.
100  *
101  * \param rast raster cell value
102  * \param[out] red red value
103  * \param[out] grn green value
104  * \param[out] blu blue value
105  * \param colors pointer to Colors structure which holds color info
106  *
107  * \return 1 if color is set
108  * \return 0 if color is not set
109  */
110 int Rast_get_d_color(const DCELL * rast,
111  int *red, int *grn, int *blu, struct Colors *colors)
112 {
113  return Rast_get_color(rast, red, grn, blu, colors, DCELL_TYPE);
114 }
115 
116 /*!
117  * \brief Gets color for null value.
118  *
119  * Puts the red, green, and blue components of <i>colors</i> for the
120  * NULL-value into <i>red, grn, and blu</i>.
121  *
122  * \param[out] red red value
123  * \param[out] grn green value
124  * \param[out] blu blue value
125  * \param colors pointer to Colors structure which holds color info
126  */
127 void Rast_get_null_value_color(int *red, int *grn, int *blu,
128  const struct Colors *colors)
129 {
130  if (colors->null_set) {
131  *red = (int)colors->null_red;
132  *grn = (int)colors->null_grn;
133  *blu = (int)colors->null_blu;
134  }
135  else if (colors->undef_set) {
136  *red = (int)colors->undef_red;
137  *grn = (int)colors->undef_grn;
138  *blu = (int)colors->undef_blu;
139  }
140  else
141  *red = *blu = *grn = 255; /* white */
142 }
143 
144 /*!
145  * \brief Gets default color.
146  *
147  * Puts the red, green, and blue components of the <tt>"default"</tt>
148  * color into <i>red, grn, and blu</i>.
149  *
150  * \param[out] red red value
151  * \param[out] grn green value
152  * \param[out] blu blue value
153  * \param colors pointer to Colors structure which holds color info
154  */
155 void Rast_get_default_color(int *red, int *grn, int *blu,
156  const struct Colors *colors)
157 {
158  if (colors->undef_set) {
159  *red = (int)colors->undef_red;
160  *grn = (int)colors->undef_grn;
161  *blu = (int)colors->undef_blu;
162  }
163  else
164  *red = *blu = *grn = 255; /* white */
165 }
#define CELL_TYPE
Definition: raster.h:11
int null_set
Definition: gis.h:671
unsigned char null_blu
Definition: gis.h:674
unsigned char null_grn
Definition: gis.h:673
double DCELL
Definition: gis.h:603
unsigned char undef_red
Definition: gis.h:676
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:127
void Rast_get_default_color(int *red, int *grn, int *blu, const struct Colors *colors)
Gets default color.
Definition: color_get.c:155
#define DCELL_TYPE
Definition: raster.h:13
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:110
double b
Definition: r_raster.c:39
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
float g
Definition: named_colr.c:8
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
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:68
unsigned char null_red
Definition: gis.h:672
float FCELL
Definition: gis.h:604
Definition: gis.h:665
unsigned char undef_blu
Definition: gis.h:678
unsigned char undef_grn
Definition: gis.h:677
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:89
int CELL
Definition: gis.h:602
int undef_set
Definition: gis.h:675
int RASTER_MAP_TYPE
Definition: raster.h:25
#define FCELL_TYPE
Definition: raster.h:12
double r
Definition: r_raster.c:39