http://slackware.bholcomb.com/slackware/slackware-11.0/source/xap/xscreensaver/xscree...
[xscreensaver] / utils / colors.c
index a7953c10b3b0e46e98c4fd1a6744adcbbc005293..2e10b55b428f478b425d3fa749ca82edc926a01e 100644 (file)
@@ -1,4 +1,4 @@
-/* xscreensaver, Copyright (c) 1997 Jamie Zawinski <jwz@jwz.org>
+/* xscreensaver, Copyright (c) 1997, 2002 Jamie Zawinski <jwz@jwz.org>
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -77,16 +77,18 @@ static void
 complain (int wanted_colors, int got_colors,
          Bool wanted_writable, Bool got_writable)
 {
-  if (wanted_writable && !got_writable)
-    fprintf(stderr,
-           "%s: wanted %d writable colors; got %d read-only colors.\n",
-           progname, wanted_colors, got_colors);
-
-  else if (wanted_colors > (got_colors + 10))
+  if (got_colors > wanted_colors - 10)
     /* don't bother complaining if we're within ten pixels. */
-    fprintf(stderr, "%s: wanted %d%s colors; got %d.\n",
-           progname, wanted_colors, (got_writable ? " writable" : ""),
-           got_colors);
+    return;
+
+  if (wanted_writable && !got_writable)
+    fprintf (stderr,
+             "%s: wanted %d writable colors; got %d read-only colors.\n",
+             progname, wanted_colors, got_colors);
+  else
+    fprintf (stderr, "%s: wanted %d%s colors; got %d.\n",
+             progname, wanted_colors, (got_writable ? " writable" : ""),
+             got_colors);
 }
 
 
@@ -102,12 +104,17 @@ make_color_ramp (Display *dpy, Colormap cmap,
 {
   Bool verbose_p = True;  /* argh. */
   int i;
-  int ncolors = *ncolorsP;
-  int wanted = ncolors;
+  int total_ncolors = *ncolorsP;
+  int ncolors, wanted;
   Bool wanted_writable = (allocate_p && writable_p);
   double dh, ds, dv;           /* deltas */
 
+  wanted = total_ncolors;
+  if (closed_p)
+    wanted = (wanted / 2) + 1;
+
  AGAIN:
+  ncolors = total_ncolors;
 
   memset (colors, 0, (*ncolorsP) * sizeof(*colors));
 
@@ -180,20 +187,23 @@ make_color_ramp (Display *dpy, Colormap cmap,
   /* we weren't able to allocate all the colors we wanted;
      decrease the requested number and try again.
    */
-  ncolors = (ncolors > 170 ? ncolors - 20 :
-            ncolors > 100 ? ncolors - 10 :
-            ncolors >  75 ? ncolors -  5 :
-            ncolors >  25 ? ncolors -  3 :
-            ncolors >  10 ? ncolors -  2 :
-            ncolors >   2 ? ncolors -  1 :
-            0);
-  *ncolorsP = ncolors;
-  if (ncolors > 0)
+  total_ncolors = (total_ncolors > 170 ? total_ncolors - 20 :
+                   total_ncolors > 100 ? total_ncolors - 10 :
+                   total_ncolors >  75 ? total_ncolors -  5 :
+                   total_ncolors >  25 ? total_ncolors -  3 :
+                   total_ncolors >  10 ? total_ncolors -  2 :
+                   total_ncolors >   2 ? total_ncolors -  1 :
+                   0);
+  *ncolorsP = total_ncolors;
+  ncolors = total_ncolors;
+  if (total_ncolors > 0)
     goto AGAIN;
 
  WARN:
   
-  if (verbose_p)
+  if (verbose_p &&
+      /* don't warn if we got 0 writable colors -- probably TrueColor. */
+      (ncolors != 0 || !wanted_writable))
     complain (wanted, ncolors, wanted_writable, wanted_writable && writable_p);
 }
 
@@ -469,7 +479,8 @@ make_smooth_colormap (Display *dpy, Visual *visual, Colormap cmap,
   double v[MAXPOINTS];
   double total_s = 0;
   double total_v = 0;
-  Screen *screen = DefaultScreenOfDisplay(dpy); /* #### WRONG! */
+  Screen *screen = (dpy ? DefaultScreenOfDisplay(dpy) : 0); /* #### WRONG! */
+  int loop = 0;
 
   if (*ncolorsP <= 0) return;
 
@@ -485,6 +496,7 @@ make_smooth_colormap (Display *dpy, Visual *visual, Colormap cmap,
   for (i = 0; i < npoints; i++)
     {
     REPICK_THIS_COLOR:
+      if (++loop > 10000) abort();
       h[i] = random() % 360;
       s[i] = frand(1.0);
       v[i] = frand(0.8) + 0.2;
@@ -552,7 +564,7 @@ make_uniform_colormap (Display *dpy, Visual *visual, Colormap cmap,
 {
   int ncolors = *ncolorsP;
   Bool wanted_writable = (allocate_p && writable_pP && *writable_pP);
-  Screen *screen = DefaultScreenOfDisplay(dpy); /* #### WRONG! */
+  Screen *screen = (dpy ? DefaultScreenOfDisplay(dpy) : 0); /* #### WRONG! */
 
   double S = ((double) (random() % 34) + 66) / 100.0;  /* range 66%-100% */
   double V = ((double) (random() % 34) + 66) / 100.0;  /* range 66%-100% */
@@ -568,7 +580,7 @@ make_uniform_colormap (Display *dpy, Visual *visual, Colormap cmap,
                  0,   S, V,
                  359, S, V,
                  colors, &ncolors,
-                 False, True,
+                 False, allocate_p,
                   (writable_pP && *writable_pP));
 
   /* If we tried for writable cells and got none, try for non-writable. */
@@ -598,7 +610,7 @@ make_random_colormap (Display *dpy, Visual *visual, Colormap cmap,
   Bool wanted_writable = (allocate_p && writable_pP && *writable_pP);
   int ncolors = *ncolorsP;
   int i;
-  Screen *screen = DefaultScreenOfDisplay(dpy); /* #### WRONG! */
+  Screen *screen = (dpy ? DefaultScreenOfDisplay(dpy) : 0); /* #### WRONG! */
 
   if (*ncolorsP <= 0) return;