1 /*!
2  * \file lib/raster/color_rule_get.c
3  *
4  * \brief Raster Library - Get color rules.
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 Get both modular and fixed rules count
19 
20    \param colors pointer to color table structure
21 
22    \return number of rules in color table
23  */
Rast_colors_count(const struct Colors * colors)24 int Rast_colors_count(const struct Colors *colors)
25 {
26     int count = 0;
27     struct _Color_Rule_ *rule;
28 
29     if (colors->fixed.rules) {
30 	count++;
31 	rule = colors->fixed.rules;
32 
33 	while (rule->next) {
34 	    count++;
35 	    rule = rule->next;
36 	}
37     }
38     if (colors->modular.rules) {
39 	count++;
40 	rule = colors->modular.rules;
41 
42 	while (rule->next) {
43 	    count++;
44 	    rule = rule->next;
45 	}
46     }
47     return count;
48 }
49 
50 /*!
51    \brief Get color rule from both modular and fixed rules
52 
53    Rules are returned in the order as stored in the table
54    (i.e. unexpected, high values first)
55 
56    \param val1 color value
57    \param[out] r1,g1,b1 color value
58    \param val2 color value
59    \param[out] r2,g2,b2 color value
60    \param colors pointer to color table structure
61    \param rule rule index from 0 to G_color_count()-1
62 
63    \return 0 success
64    \return 1 index out of range
65  */
Rast_get_fp_color_rule(DCELL * val1,unsigned char * r1,unsigned char * g1,unsigned char * b1,DCELL * val2,unsigned char * r2,unsigned char * g2,unsigned char * b2,const struct Colors * colors,int rule)66 int Rast_get_fp_color_rule(DCELL * val1, unsigned char *r1, unsigned char *g1,
67 			   unsigned char *b1, DCELL * val2, unsigned char *r2,
68 			   unsigned char *g2, unsigned char *b2,
69 			   const struct Colors *colors, int rule)
70 {
71     int index = -1;
72     int found = 0;
73     const struct _Color_Rule_ *rl;
74 
75     *val1 = *val2 = 0.0;
76     *r1 = *g1 = *b1 = *r2 = *g2 = *b2 = 0;
77 
78     /* Find the rule */
79     if (colors->fixed.rules) {
80 	rl = colors->fixed.rules;
81 	index++;
82 	if (index == rule)
83 	    found = 1;
84 
85 	while (!found && rl->next) {
86 	    rl = rl->next;
87 	    index++;
88 	    if (index == rule)
89 		found = 1;
90 	}
91     }
92     if (!found && colors->modular.rules) {
93 	rl = colors->modular.rules;
94 	index++;
95 	if (index == rule)
96 	    found = 1;
97 
98 	while (!found && rl->next) {
99 	    rl = rl->next;
100 	    index++;
101 	    if (index == rule)
102 		found = 1;
103 	}
104     }
105 
106     if (!found)
107 	return 1;
108 
109     /* Set values */
110     *val1 = rl->low.value;
111     *val2 = rl->high.value;
112 
113     *r1 = rl->low.red;
114     *g1 = rl->low.grn;
115     *b1 = rl->low.blu;
116 
117     *r2 = rl->high.red;
118     *g2 = rl->high.grn;
119     *b2 = rl->high.blu;
120 
121     return 0;
122 }
123