cf4fd92d7a2fcd5b6c686a9660c1ea1e57921f7c
[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, &width, &height, &bw, &d);
74     }
75
76   for (j = 0; j < sizeof(colors) / sizeof(*colors); j++)
77     {
78       int i, h, ncols;
79       int x1 = 0;
80       int y2;
81       for (ncols = 0; ncols < sizeof(*colors) / sizeof(**colors); ncols++)
82         if (!colors[j][ncols]) break;
83       ypct += heights[j];
84       y2 = height * ypct / 100;
85       h = y2 - y; /* avoid roundoff fencepost */
86       for (i = 0; i < ncols; i++)
87         {
88           XColor xcolor;
89           const char *color = colors[j][i];
90           int x2 = x + (width * (i+1) / ncols);
91           int w = x2 - x1; /* avoid roundoff fencepost */
92           if (! XParseColor (dpy, cmap, color, &xcolor))
93             abort();
94           xcolor.flags = DoRed|DoGreen|DoBlue;
95           if (!XAllocColor (dpy, cmap, &xcolor))
96             continue;
97           XSetForeground (dpy, gc, xcolor.pixel);
98           XFillRectangle (dpy, drawable, gc, x1, y, w, h);
99           x1 = x2;
100         }
101       y = y2;
102     }
103
104   y = oy;
105
106   /* Add in the xscreensaver logo */
107   {
108     unsigned long *pixels; /* ignored - unfreed */
109     int npixels;
110     unsigned long bg = ~0;
111     Pixmap logo_mask = 0;
112     Pixmap logo_map = xscreensaver_logo (screen, visual, drawable, cmap, bg,
113                                          &pixels, &npixels, &logo_mask,
114                                          True);
115     if (logo_map)
116       {
117         Window root;
118         int logo_width, logo_height;
119         int w = width;
120         int h = height * heights[0] / 100;
121         int x1, y1, bw, d;
122         XGetGeometry (dpy, logo_map, &root, &x1, &y1,
123                       &logo_width, &logo_height, &bw, &d);
124         x1 = x + (w - logo_width) / 2;
125         y1 = y + (h - logo_height) / 2;
126         if (logo_mask)
127           {
128             XSetClipMask (dpy, gc, logo_mask);
129             XSetClipOrigin (dpy, gc, x1, y1);
130           }
131         XCopyArea (dpy, logo_map, drawable, gc,
132                    0, 0, logo_width, logo_height, x1, y1);
133         XFreePixmap (dpy, logo_map);
134         if (logo_mask)
135           XFreePixmap (dpy, logo_mask);
136       }
137   }
138
139   XFreeGC(dpy, gc);
140 }