GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
color_out.c
Go to the documentation of this file.
1 /*!
2  \file lib/raster/color_out.c
3 
4  \brief Raster Library - Print color table
5 
6  (C) 2010-2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public
9  License (>=v2). Read the file COPYING that comes with GRASS
10  for details.
11 
12  \author Glynn Clements
13 */
14 
15 #include <grass/raster.h>
16 
17 static void write_rule(DCELL *val, DCELL *min, DCELL *max, int r, int g, int b,
18  FILE *fp, int perc)
19 {
20  static DCELL v0;
21  static int r0 = -1, g0 = -1, b0 = -1;
22 
23  if (v0 == *val && r0 == r && g0 == g && b0 == b)
24  return;
25  v0 = *val, r0 = r, g0 = g, b0 = b;
26 
27  if (perc)
28  fprintf(fp, "%g%% %d:%d:%d\n", 100 * (*val - *min) / (*max - *min), r, g, b);
29  else
30  fprintf(fp, "%g %d:%d:%d\n", *val, r, g, b);
31 }
32 
33 /*!
34  \brief Print color table
35 
36  \param colors pointer to Colors structure
37  \param min,max minimum and maximum value for percentage output (used only when \p perc is non-zero)
38  \param fp file where to print color table rules
39  \param perc TRUE for percentage output
40 */
41 void Rast_print_colors(struct Colors *colors, DCELL min, DCELL max, FILE *fp,
42  int perc)
43 {
44  int i, count;
45 
46  count = 0;
47  if (colors->version < 0) {
48  /* 3.0 format */
49  CELL lo, hi;
50 
51  Rast_get_c_color_range(&lo, &hi, colors);
52 
53  for (i = lo; i <= hi; i++) {
54  unsigned char r, g, b, set;
55  DCELL val = (DCELL) i;
56  Rast_lookup_c_colors(&i, &r, &g, &b, &set, 1, colors);
57  write_rule(&val, &min, &max, r, g, b, fp, perc);
58  }
59  }
60  else {
61  count = Rast_colors_count(colors);
62 
63  for (i = 0; i < count; i++) {
64  DCELL val1, val2;
65  unsigned char r1, g1, b1, r2, g2, b2;
66 
68  &val1, &r1, &g1, &b1,
69  &val2, &r2, &g2, &b2,
70  colors, count - 1 - i);
71 
72  write_rule(&val1, &min, &max, r1, g1, b1, fp, perc);
73  write_rule(&val2, &min, &max, r2, g2, b2, fp, perc);
74  }
75  }
76 
77  {
78  int r, g, b;
79  Rast_get_null_value_color(&r, &g, &b, colors);
80  fprintf(fp, "nv %d:%d:%d\n", r, g, b);
81  Rast_get_default_color(&r, &g, &b, colors);
82  fprintf(fp, "default %d:%d:%d\n", r, g, b);
83  }
84 
85  if (fp != stdout)
86  fclose(fp);
87 }
#define min(x, y)
Definition: draw2.c:31
int version
Definition: gis.h:667
double DCELL
Definition: gis.h:603
int count
void Rast_print_colors(struct Colors *colors, DCELL min, DCELL max, FILE *fp, int perc)
Print color table.
Definition: color_out.c:41
#define max(x, y)
Definition: draw2.c:32
int Rast_get_fp_color_rule(DCELL *, unsigned char *, unsigned char *, unsigned char *, DCELL *, unsigned char *, unsigned char *, unsigned char *, const struct Colors *, int)
Get color rule from both modular and fixed rules.
void Rast_get_default_color(int *, int *, int *, const struct Colors *)
Gets default color.
Definition: color_get.c:155
double b
Definition: r_raster.c:39
void Rast_get_null_value_color(int *, int *, int *, const struct Colors *)
Gets color for null value.
Definition: color_get.c:127
float g
Definition: named_colr.c:8
Definition: gis.h:665
void Rast_get_c_color_range(CELL *, CELL *, const struct Colors *)
Get color range values (CELL)
Definition: color_range.c:64
int CELL
Definition: gis.h:602
void Rast_lookup_c_colors(const CELL *, unsigned char *, unsigned char *, unsigned char *, unsigned char *, int, struct Colors *)
Lookup an array of colors.
Definition: color_look.c:45
int Rast_colors_count(const struct Colors *)
Get both modular and fixed rules count.
double r
Definition: r_raster.c:39