http://packetstormsecurity.org/UNIX/admin/xscreensaver-3.32.tar.gz
[xscreensaver] / utils / colorbars.c
1 /* xscreensaver, Copyright (c) 2001 Jamie Zawinski <jwz@jwz.org>
2  *
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 
9  * implied warranty.
10  */
11
12 /* This file contains code for drawing NTSC colorbars.
13    A couple of things use this.
14  */
15
16 #include "utils.h"
17 #include "resources.h"
18 #include "colorbars.h"
19
20 static const char * const colors[7][18] = {
21   { "#CCCCCC", "#FFFF00", "#00FFFF", "#00FF00",         /* tall bars */
22     "#FF00FF", "#FF0000", "#0000FF", "#000000",
23     0
24   }, {
25     "#000000", "#0000FF", "#FF0000", "#FF00FF",         /* short rev bars */
26     "#00FF00", "#00FFFF", "#FFFF00", "#CCCCCC",
27     0
28   }, {
29     "#000000", 0                                        /* blank */
30   }, {
31     "#FFFFFF", "#EEEEEE", "#DDDDDD", "#CCCCCC",         /* gray ramp */
32     "#BBBBBB", "#AAAAAA", "#999999", "#888888",
33     "#777777", "#666666", "#555555", "#444444",
34     "#333333", "#222222", "#111111", "#000000"
35   }, {
36     "#000000", "#111111", "#222222", "#333333",         /* gray rev ramp */
37     "#444444", "#555555", "#666666", "#777777",
38     "#888888", "#999999", "#AAAAAA", "#BBBBBB",
39     "#CCCCCC", "#DDDDDD", "#EEEEEE", "#FFFFFF"
40   }, {
41     "#000000", 0                                        /* blank */
42   }, {
43     "#FF00FF", "#FF00FF", "#FF00FF",                    /* blacklevel row */
44     "#FFFFFF", "#FFFFFF", "#FFFFFF",
45     "#0000AD", "#0000AD", "#0000AD",
46     "#131313", "#131313", "#131313",
47     "#000000", "#000000", "#262626",
48     "#000000", "#000000", "#000000"
49   }
50 };
51
52 static const int heights[7] = { 63, 10, 1, 5, 5, 1, 15 };   /* percentages */
53
54
55 void
56 draw_colorbars (Display *dpy, Window window,
57                 int x, int y, int width, int height)
58 {
59   int oy = y;
60   int ypct = 0;
61   int j;
62   XGCValues gcv;
63   GC gc = XCreateGC (dpy, window, 0, &gcv);
64   Colormap cmap;
65   XWindowAttributes xgwa;
66   XGetWindowAttributes (dpy, window, &xgwa);
67   cmap = xgwa.colormap;
68
69   for (j = 0; j < sizeof(colors) / sizeof(*colors); j++)
70     {
71       int i, h, ncols;
72       int x1 = 0;
73       int y2;
74       for (ncols = 0; ncols < sizeof(*colors) / sizeof(**colors); ncols++)
75         if (!colors[j][ncols]) break;
76       ypct += heights[j];
77       y2 = height * ypct / 100;
78       h = y2 - y; /* avoid roundoff fencepost */
79       for (i = 0; i < ncols; i++)
80         {
81           XColor xcolor;
82           const char *color = colors[j][i];
83           int x2 = x + (width * (i+1) / ncols);
84           int w = x2 - x1; /* avoid roundoff fencepost */
85           if (! XParseColor (dpy, cmap, color, &xcolor))
86             abort();
87           xcolor.flags = DoRed|DoGreen|DoBlue;
88           if (!XAllocColor (dpy, cmap, &xcolor))
89             continue;
90           XSetForeground (dpy, gc, xcolor.pixel);
91           XFillRectangle (dpy, window, gc, x1, y, w, h);
92           x1 = x2;
93         }
94       y = y2;
95     }
96
97   y = oy;
98
99   /* Add in the xscreensaver logo */
100   {
101     unsigned long *pixels; /* ignored - unfreed */
102     int npixels;
103     unsigned long bg = ~0;
104     Pixmap logo_mask = 0;
105     Pixmap logo_map = xscreensaver_logo (dpy, window, cmap, bg,
106                                          &pixels, &npixels, &logo_mask,
107                                          True);
108     if (logo_map)
109       {
110         Window root;
111         int logo_width, logo_height;
112         int w = width;
113         int h = height * heights[0] / 100;
114         int x1, y1, bw, d;
115         XGetGeometry (dpy, logo_map, &root, &x1, &y1,
116                       &logo_width, &logo_height, &bw, &d);
117         x1 = x + (w - logo_width) / 2;
118         y1 = y + (h - logo_height) / 2;
119         if (logo_mask)
120           {
121             XSetClipMask (dpy, gc, logo_mask);
122             XSetClipOrigin (dpy, gc, x1, y1);
123           }
124         XCopyArea (dpy, logo_map, window, gc,
125                    0, 0, logo_width, logo_height, x1, y1);
126         XFreePixmap (dpy, logo_map);
127         if (logo_mask)
128           XFreePixmap (dpy, logo_mask);
129       }
130   }
131
132   XFreeGC(dpy, gc);
133 }