GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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 /*!
14  * \brief make random colors
15  *
16  * Generates random colors. Good as a first pass at a
17  * color table for nominal data.
18  *
19  * \param colors
20  * \param min
21  * \param max
22  * \return
23  */
24 
26 {
27  unsigned char red, grn, blu;
28  int count;
29  CELL n;
30 
31  Rast_init_colors(colors);
32  if (min > max)
33  G_fatal_error(_("Rast_make_random_colors: min (%d) > max (%d)"),
34  min, max);
35 
36  /* You can set GRASS_RANDOM_SEED for repeatability */
38 
39  count = MAX_COLORS - DEVIATION + G_lrand48() % DEVIATION;
40  if (count > max - min + 1)
41  count = max - min + 1;
42 
43  for (n = 1; n <= count; n++) {
44  red = G_lrand48() & 0xff;
45  grn = G_lrand48() & 0xff;
46  blu = G_lrand48() & 0xff;
47  Rast_add_modular_c_color_rule(&n, red, grn, blu,
48  &n, red, grn, blu, colors);
49  }
50  Rast_set_c_color_range(min, max, colors);
51 }
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
unsigned char * blu
Definition: gis.h:647
unsigned char * red
Definition: gis.h:645
#define min(x, y)
Definition: draw2.c:31
int count
unsigned char * grn
Definition: gis.h:646
#define DEVIATION
Definition: color_rand.c:10
#define MAX_COLORS
Definition: color_rand.c:9
#define max(x, y)
Definition: draw2.c:32
long G_srand48_auto(void)
Seed the pseudo-random number generator from the time and PID.
Definition: lrand48.c:71
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_set_c_color_range(CELL, CELL, struct Colors *)
Set color range (CELL version)
Definition: color_range.c:24
Definition: gis.h:665
int CELL
Definition: gis.h:602
#define _(str)
Definition: glocale.h:10
long G_lrand48(void)
Generate an integer in the range [0, 2^31)
Definition: lrand48.c:130
void Rast_init_colors(struct Colors *)
Initialize color structure.
Definition: color_init.c:25
void Rast_make_random_colors(struct Colors *colors, CELL min, CELL max)
make random colors
Definition: color_rand.c:25