1 /* xscreensaver, Copyright (c) 1997 Jamie Zawinski <jwz@netscape.com>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
11 * Concept snarfed from Michael D. Bayne in
12 * http://www.go2net.com/internet/deep/1997/04/16/body.html
15 #include "screenhack.h"
16 #include <X11/Xutil.h>
23 unsigned long fg_pixel = 0;
24 unsigned long bg_pixel = 0;
27 init_moire (Display *dpy, Window window)
32 double fgs, fgv, bgs, bgv;
33 XWindowAttributes xgwa;
36 XGetWindowAttributes (dpy, window, &xgwa);
38 offset = get_integer_resource ("offset", "Integer");
39 if (offset < 2) offset = 2;
44 for (i = 0; i < ncolors; i++)
45 XFreeColors (dpy, xgwa.colormap, &colors[i].pixel, 1, 0);
52 fg_pixel = WhitePixelOfScreen (DefaultScreenOfDisplay(dpy));
53 bg_pixel = BlackPixelOfScreen (DefaultScreenOfDisplay(dpy));
57 fg_pixel = get_pixel_resource ("foreground", "Foreground", dpy,
59 bg_pixel = get_pixel_resource ("background", "Background", dpy,
65 offset *= 20; /* compensate for lack of shading */
66 gcv.foreground = fg_pixel;
70 ncolors = get_integer_resource ("ncolors", "Integer");
71 if (ncolors < 2) ncolors = 2;
74 fgc.flags = bgc.flags = DoRed|DoGreen|DoBlue;
75 if (get_boolean_resource("random","Boolean"))
77 fgc.red = random() & 0xFFFF;
78 fgc.green = random() & 0xFFFF;
79 fgc.blue = random() & 0xFFFF;
80 bgc.red = random() & 0xFFFF;
81 bgc.green = random() & 0xFFFF;
82 bgc.blue = random() & 0xFFFF;
88 XQueryColor (dpy, xgwa.colormap, &fgc);
89 XQueryColor (dpy, xgwa.colormap, &bgc);
91 rgb_to_hsv (fgc.red, fgc.green, fgc.blue, &fgh, &fgs, &fgv);
92 rgb_to_hsv (bgc.red, bgc.green, bgc.blue, &bgh, &bgs, &bgv);
94 colors = (XColor *) malloc (sizeof (XColor) * (ncolors+2));
95 memset(colors, 0, (sizeof (XColor) * (ncolors+2)));
96 make_color_ramp (dpy, xgwa.colormap,
97 fgh, fgs, fgv, bgh, bgs, bgv,
100 if (ncolors != oncolors)
101 fprintf(stderr, "%s: got %d of %d requested colors.\n",
102 progname, ncolors, oncolors);
110 gcv.foreground = colors[0].pixel;
112 gc = XCreateGC (dpy, window, GCForeground, &gcv);
117 moire (Display *dpy, Window window, int offset, XColor *colors, int ncolors)
120 int factor = (random() % offset) + 1;
122 XWindowAttributes xgwa;
125 XGetWindowAttributes (dpy, window, &xgwa);
127 xo = (random() % xgwa.width) - xgwa.width/2;
128 yo = (random() % xgwa.height) - xgwa.height/2;
130 depth = visual_depth(DefaultScreenOfDisplay(dpy), xgwa.visual);
131 image = XCreateImage (dpy, xgwa.visual,
132 depth, ZPixmap, 0, /* depth, format, offset */
133 0, xgwa.width, 1, 8, 0); /* data, w, h, pad, bpl */
135 image->data = (char *) malloc (((xgwa.width + 1) * depth / 8)
136 * 2 /* uh, I dunno... */
139 for (y = 0; y < xgwa.height; y++)
141 for (x = 0; x < xgwa.width; x++)
145 double i = ((xx * xx) + (yy * yy)) / (double) factor;
147 gcv.foreground = ((((long) i) & 1) ? fg_pixel : bg_pixel);
149 gcv.foreground = colors[((long) i) % ncolors].pixel;
150 XPutPixel (image, x, 0, gcv.foreground);
152 XPutImage (dpy, window, gc, image, 0, 0, 0, y, xgwa.width, 1);
155 if (image->data) free(image->data);
157 XDestroyImage (image);
161 char *progclass = "Moire";
163 char *defaults [] = {
173 XrmOptionDescRec options [] = {
174 { "-random", ".random", XrmoptionSepArg, 0 },
175 { "-delay", ".delay", XrmoptionSepArg, 0 },
176 { "-ncolors", ".ncolors", XrmoptionSepArg, 0 },
177 { "-offset", ".offset", XrmoptionSepArg, 0 },
182 screenhack (Display *dpy, Window window)
184 int delay = get_integer_resource ("delay", "Integer");
187 init_moire (dpy, window);
188 moire (dpy, window, offset, colors, ncolors);