color_rand.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <time.h> /* For time() */
  2. #include <stdio.h> /* For NULL */
  3. #include <stdlib.h> /* For rand() and srand() */
  4. #include <grass/gis.h>
  5. #include <grass/raster.h>
  6. #include <grass/glocale.h>
  7. #define MAX_COLORS 1024
  8. #define DEVIATION 128
  9. /*!
  10. * \brief make random colors
  11. *
  12. * Generates random colors. Good as a first pass at a
  13. * color table for nominal data.
  14. *
  15. * \param colors
  16. * \param min
  17. * \param max
  18. * \return
  19. */
  20. void Rast_make_random_colors(struct Colors *colors, CELL min, CELL max)
  21. {
  22. unsigned char red, grn, blu;
  23. int count;
  24. CELL n;
  25. Rast_init_colors(colors);
  26. if (min > max)
  27. G_fatal_error(_("Rast_make_random_colors: min (%d) > max (%d)"),
  28. min, max);
  29. /* FIXME - allow seed to be specified for repeatability */
  30. G_srand48_auto();
  31. count = MAX_COLORS - DEVIATION + G_lrand48() % DEVIATION;
  32. if (count > max - min + 1)
  33. count = max - min + 1;
  34. for (n = 1; n <= count; n++) {
  35. red = G_lrand48() & 0xff;
  36. grn = G_lrand48() & 0xff;
  37. blu = G_lrand48() & 0xff;
  38. Rast_add_modular_c_color_rule(&n, red, grn, blu,
  39. &n, red, grn, blu, colors);
  40. }
  41. Rast_set_c_color_range(min, max, colors);
  42. }