GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-809ca1ee84
color_rand.c
Go to the documentation of this file.
1 #include <time.h> /* For time() */
2 #include <stdio.h> /* For NULL */
3 #include <stdlib.h> /* For rand() and srand() */
4 
5 #include <grass/gis.h>
6 #include <grass/raster.h>
7 #include <grass/glocale.h>
8 
9 #define MAX_COLORS 1024
10 #define DEVIATION 128
11 
12 /*!
13  * \brief make random colors
14  *
15  * Generates random colors. Good as a first pass at a
16  * color table for nominal data.
17  *
18  * \param colors
19  * \param min
20  * \param max
21  * \return
22  */
23 
25 {
26  unsigned char red, grn, blu;
27  int count;
28  CELL n;
29 
30  Rast_init_colors(colors);
31  if (min > max)
32  G_fatal_error(_("Rast_make_random_colors: min (%d) > max (%d)"), min,
33  max);
34 
35  /* You can set GRASS_RANDOM_SEED for repeatability */
37 
39  if (count > max - min + 1)
40  count = max - min + 1;
41 
42  for (n = 1; n <= count; n++) {
43  red = G_lrand48() & 0xff;
44  grn = G_lrand48() & 0xff;
45  blu = G_lrand48() & 0xff;
46  Rast_add_modular_c_color_rule(&n, red, grn, blu, &n, red, grn, blu,
47  colors);
48  }
49  Rast_set_c_color_range(min, max, colors);
50 }
void Rast_make_random_colors(struct Colors *colors, CELL min, CELL max)
make random colors
Definition: color_rand.c:24
#define DEVIATION
Definition: color_rand.c:10
#define MAX_COLORS
Definition: color_rand.c:9
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
long G_lrand48(void)
Generate an integer in the range [0, 2^31)
Definition: lrand48.c:136
long G_srand48_auto(void)
Seed the pseudo-random number generator from the time and PID.
Definition: lrand48.c:72
void Rast_set_c_color_range(CELL, CELL, struct Colors *)
Set color range (CELL version)
Definition: color_range.c:24
int Rast_add_modular_c_color_rule(const CELL *, int, int, int, const CELL *, int, int, int, struct Colors *)
Add modular integer color rule (CELL version)
Definition: color_rule.c:184
void Rast_init_colors(struct Colors *)
Initialize color structure.
Definition: color_init.c:25
#define min(x, y)
Definition: draw2.c:29
#define max(x, y)
Definition: draw2.c:30
int CELL
Definition: gis.h:625
#define _(str)
Definition: glocale.h:10
int count
Definition: gis.h:683