color_rand.c 955 B

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