color_rand.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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/glocale.h>
  6. #define MAX_COLORS 1024
  7. #define DEVIATION 128
  8. /*!
  9. * \brief make random colors
  10. *
  11. * Generates random colors. Good as a first pass at a
  12. * color table for nominal data.
  13. *
  14. * \param colors
  15. * \param min
  16. * \param max
  17. * \return
  18. */
  19. void G_make_random_colors(struct Colors *colors, CELL min, CELL max)
  20. {
  21. unsigned char red, grn, blu;
  22. int count;
  23. CELL n;
  24. G_init_colors(colors);
  25. if (min > max)
  26. G_fatal_error(_("G_make_random_colors: min (%d) > max (%d)"),
  27. min, max);
  28. srand(time(NULL));
  29. count = MAX_COLORS - DEVIATION + rand() % DEVIATION;
  30. if (count > max - min + 1)
  31. count = max - min + 1;
  32. for (n = 1; n <= count; n++) {
  33. red = rand() & 0377;
  34. grn = rand() & 0377;
  35. blu = rand() & 0377;
  36. G_add_modular_color_rule(n, red, grn, blu, n, red, grn, blu, colors);
  37. }
  38. G_set_color_range(min, max, colors);
  39. }