GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-112dd97adf
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,
29  g, b);
30  else
31  fprintf(fp, "%g %d:%d:%d\n", *val, r, g, b);
32 }
33 
34 /*!
35  \brief Print color table
36 
37  \param colors pointer to Colors structure
38  \param min,max minimum and maximum value for percentage output (used only
39  when \p perc is non-zero) \param fp file where to print color table rules
40  \param perc TRUE for percentage output
41  */
42 void Rast_print_colors(struct Colors *colors, DCELL min, DCELL max, FILE *fp,
43  int perc)
44 {
45  int i, count;
46 
47  count = 0;
48  if (colors->version < 0) {
49  /* 3.0 format */
50  CELL lo, hi;
51 
52  Rast_get_c_color_range(&lo, &hi, colors);
53 
54  for (i = lo; i <= hi; i++) {
55  unsigned char r, g, b, set;
56  DCELL val = (DCELL)i;
57 
58  Rast_lookup_c_colors(&i, &r, &g, &b, &set, 1, colors);
59  write_rule(&val, &min, &max, r, g, b, fp, perc);
60  }
61  }
62  else {
63  count = Rast_colors_count(colors);
64 
65  for (i = 0; i < count; i++) {
66  DCELL val1, val2;
67  unsigned char r1, g1, b1, r2, g2, b2;
68 
69  Rast_get_fp_color_rule(&val1, &r1, &g1, &b1, &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 
80  Rast_get_null_value_color(&r, &g, &b, colors);
81  fprintf(fp, "nv %d:%d:%d\n", r, g, b);
82  Rast_get_default_color(&r, &g, &b, colors);
83  fprintf(fp, "default %d:%d:%d\n", r, g, b);
84  }
85 
86  if (fp != stdout)
87  fclose(fp);
88 }
void Rast_print_colors(struct Colors *colors, DCELL min, DCELL max, FILE *fp, int perc)
Print color table.
Definition: color_out.c:42
int Rast_colors_count(const struct Colors *)
Get both modular and fixed rules count.
void Rast_get_c_color_range(CELL *, CELL *, const struct Colors *)
Get color range values (CELL)
Definition: color_range.c:64
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_null_value_color(int *, int *, int *, const struct Colors *)
Gets color for null value.
Definition: color_get.c:126
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
void Rast_get_default_color(int *, int *, int *, const struct Colors *)
Gets default color.
Definition: color_get.c:154
#define min(x, y)
Definition: draw2.c:29
#define max(x, y)
Definition: draw2.c:30
double DCELL
Definition: gis.h:626
int CELL
Definition: gis.h:625
int count
float g
Definition: named_colr.c:7
double b
Definition: r_raster.c:39
double r
Definition: r_raster.c:39
Definition: gis.h:683
int version
Definition: gis.h:684