http://svn.poeml.de/viewvc/ppc/src-unpacked/xscreensaver/xscreensaver-4.12.tar.bz2...
[xscreensaver] / utils / colorbars.c
1 /* xscreensaver, Copyright (c) 2001, 2003 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 (Screen *screen, Visual *visual,
57                 Drawable drawable, Colormap cmap,
58                 int x, int y, int width, int height)
59 {
60   Display *dpy = DisplayOfScreen (screen);
61   int oy = y;
62   int ypct = 0;
63   int j;
64   XGCValues gcv;
65   GC gc = XCreateGC (dpy, drawable, 0, &gcv);
66
67   if (width <= 0 || height <= 0)
68     {
69       Window root;
70       int xx, yy;
71       unsigned int bw, d;
72       XGetGeometry (dpy, drawable,
73                     &root, &xx, &yy,
74                     (unsigned int *) &width,
75                     (unsigned int *) &height,
76                     &bw, &d);
77     }
78
79   for (j = 0; j < sizeof(colors) / sizeof(*colors); j++)
80     {
81       int i, h, ncols;
82       int x1 = 0;
83       int y2;
84       for (ncols = 0; ncols < sizeof(*colors) / sizeof(**colors); ncols++)
85         if (!colors[j][ncols]) break;
86       ypct += heights[j];
87       y2 = height * ypct / 100;
88       h = y2 - y; /* avoid roundoff fencepost */
89       for (i = 0; i < ncols; i++)
90         {
91           XColor xcolor;
92           const char *color = colors[j][i];
93           int x2 = x + (width * (i+1) / ncols);
94           int w = x2 - x1; /* avoid roundoff fencepost */
95           if (! XParseColor (dpy, cmap, color, &xcolor))
96             abort();
97           xcolor.flags = DoRed|DoGreen|DoBlue;
98           if (!XAllocColor (dpy, cmap, &xcolor))
99             continue;
100           XSetForeground (dpy, gc, xcolor.pixel);
101           XFillRectangle (dpy, drawable, gc, x1, y, w, h);
102           x1 = x2;
103         }
104       y = y2;
105     }
106
107   y = oy;
108
109   /* Add in the xscreensaver logo */
110   {
111     unsigned long *pixels; /* ignored - unfreed */
112     int npixels;
113     unsigned long bg = ~0;
114     Pixmap logo_mask = 0;
115     Pixmap logo_map = xscreensaver_logo (screen, visual, drawable, cmap, bg,
116                                          &pixels, &npixels, &logo_mask,
117                                          True);
118     if (logo_map)
119       {
120         Window root;
121         unsigned int logo_width, logo_height;
122         unsigned int w = width;
123         unsigned int h = height * heights[0] / 100;
124         int x1, y1;
125         unsigned int bw, d;
126         XGetGeometry (dpy, logo_map, &root, &x1, &y1,
127                       &logo_width, &logo_height, &bw, &d);
128         x1 = x + (w - logo_width) / 2;
129         y1 = y + (h - logo_height) / 2;
130         if (logo_mask)
131           {
132             XSetClipMask (dpy, gc, logo_mask);
133             XSetClipOrigin (dpy, gc, x1, y1);
134           }
135         XCopyArea (dpy, logo_map, drawable, gc,
136                    0, 0, logo_width, logo_height, x1, y1);
137         XFreePixmap (dpy, logo_map);
138         if (logo_mask)
139           XFreePixmap (dpy, logo_mask);
140       }
141   }
142
143   XFreeGC(dpy, gc);
144 }