1 /* xscreensaver, Copyright (c) 1992, 1995, 1996, 1997
2 * Jamie Zawinski <jwz@netscape.com>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
13 /* Beauty is only skin deep, unless you've got an alpha channel. */
20 #include "resources.h"
23 # define countof(x) (sizeof(*(x))/sizeof((x)))
27 /* I don't believe this fucking language doesn't have builtin exponentiation.
28 I further can't believe that the fucking ^ character means fucking XOR!! */
39 merge_colors (int argc, XColor **argv, XColor *into_color, int mask,
43 *into_color = *argv [0];
44 into_color->pixel |= mask;
46 for (j = 1; j < argc; j++)
48 # define SHORT_INC(x,y) (x = ((((x)+(y)) > 0xFFFF) ? 0xFFFF : ((x)+(y))))
49 # define SHORT_DEC(x,y) (x = ((((x)-(y)) < 0) ? 0 : ((x)-(y))))
52 SHORT_INC (into_color->red, argv[j]->red);
53 SHORT_INC (into_color->green, argv[j]->green);
54 SHORT_INC (into_color->blue, argv[j]->blue);
58 SHORT_DEC (into_color->red, argv[j]->red);
59 SHORT_DEC (into_color->green, argv[j]->green);
60 SHORT_DEC (into_color->blue, argv[j]->blue);
68 permute_colors (XColor *pcolors, XColor *colors,
70 unsigned long *plane_masks,
74 int max = i_exp (2, count);
75 if (count > 31) abort ();
76 for (out = 1; out < max; out++)
82 for (bit = 0; bit < 32; bit++)
85 argv [argc++] = &pcolors [bit];
86 this_mask |= plane_masks [bit];
88 merge_colors (argc, argv, &colors [out-1], this_mask, additive_p);
94 allocate_color_planes (Display *dpy, Colormap cmap,
95 int nplanes, unsigned long *plane_masks,
96 unsigned long *base_pixel_ret)
99 !XAllocColorCells (dpy, cmap, False, plane_masks, nplanes,
108 initialize_transparency_colormap (Display *dpy, Colormap cmap,
110 unsigned long base_pixel,
111 unsigned long *plane_masks,
116 int total_colors = i_exp (2, nplanes);
117 XColor *all_colors = (XColor *) calloc (total_colors, sizeof (XColor));
119 for (i = 0; i < nplanes; i++)
120 colors[i].pixel = base_pixel | plane_masks [i];
121 permute_colors (colors, all_colors, nplanes, plane_masks, additive_p);
123 /* clone the default background of the window into our "base" pixel */
124 all_colors [total_colors - 1].pixel =
125 get_pixel_resource ("background", "Background", dpy, cmap);
126 XQueryColor (dpy, cmap, &all_colors [total_colors - 1]);
127 all_colors [total_colors - 1].pixel = base_pixel;
129 for (i = 0; i < total_colors; i++)
130 all_colors[i].flags = DoRed|DoGreen|DoBlue;
131 XStoreColors (dpy, cmap, all_colors, total_colors);
132 XFree ((XPointer) all_colors);
137 allocate_alpha_colors (Display *dpy, Colormap cmap,
138 int *nplanesP, Bool additive_p,
139 unsigned long **plane_masks,
140 unsigned long *base_pixelP)
143 int nplanes = *nplanesP;
146 if (nplanes > 31) nplanes = 31;
147 *plane_masks = (unsigned long *) malloc(sizeof(unsigned long) * nplanes);
149 nplanes = allocate_color_planes (dpy, cmap, nplanes, *plane_masks,
160 colors = (XColor *) calloc (nplanes, sizeof (XColor));
161 for (i = 0; i < nplanes; i++)
163 /* pick the base colors. If we are in subtractive mode, pick higher
165 hsv_to_rgb (random () % 360,
167 frand (0.5) + (additive_p ? 0.2 : 0.5),
172 initialize_transparency_colormap (dpy, cmap, nplanes,
173 *base_pixelP, *plane_masks, colors,
175 XFree ((XPointer) colors);