GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72090
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  /* FIXME - allow seed to be specified 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 }
unsigned char * blu
Definition: gis.h:608
int Rast_add_modular_c_color_rule(const CELL *val1, int r1, int g1, int b1, const CELL *val2, int r2, int g2, int b2, struct Colors *colors)
Add modular integer color rule (CELL version)
Definition: color_rule.c:184
unsigned char * red
Definition: gis.h:606
#define min(x, y)
Definition: draw2.c:31
int count
unsigned char * grn
Definition: gis.h:607
#define DEVIATION
Definition: color_rand.c:10
#define MAX_COLORS
Definition: color_rand.c:9
void Rast_init_colors(struct Colors *colors)
Initialize color structure.
Definition: color_init.c:25
#define max(x, y)
Definition: draw2.c:32
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
long G_lrand48(void)
Generate an integer in the range [0, 2^31)
Definition: lrand48.c:129
void Rast_set_c_color_range(CELL min, CELL max, struct Colors *colors)
Set color range (CELL version)
Definition: color_range.c:24
Definition: gis.h:626
long G_srand48_auto(void)
Seed the pseudo-random number generator from the time and PID.
Definition: lrand48.c:71
int CELL
Definition: gis.h:580
#define _(str)
Definition: glocale.h:13
void Rast_make_random_colors(struct Colors *colors, CELL min, CELL max)
make random colors
Definition: color_rand.c:25