color_rand.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. srand(time(NULL));
  30. count = MAX_COLORS - DEVIATION + rand() % DEVIATION;
  31. if (count > max - min + 1)
  32. count = max - min + 1;
  33. for (n = 1; n <= count; n++) {
  34. red = rand() & 0377;
  35. grn = rand() & 0377;
  36. blu = rand() & 0377;
  37. Rast_add_modular_c_color_rule(&n, red, grn, blu,
  38. &n, red, grn, blu, colors);
  39. }
  40. Rast_set_c_color_range(min, max, colors);
  41. }