wmaker: Created an array to hold the maximize menu entries
[wmaker-crm.git] / wrlib / tests / testgrad.c
blobb4a32570bdecc3fa14fb44aa36df01c8147f9902
2 #include <X11/Xlib.h>
3 #include "wraster.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
8 Display *dpy;
9 Window win;
10 RContext *ctx;
11 RImage *imgh, *imgv, *imgd;
12 Pixmap pix;
13 char *ProgName;
15 void print_help()
17 printf("usage: %s [-options] color1 [color2 ...]\n", ProgName);
18 puts("options:");
19 puts(" -m match colors");
20 puts(" -d dither colors (default)");
21 puts(" -c <cpc> colors per channel to use");
22 puts(" -v <vis-id> visual id to use");
25 int main(int argc, char **argv)
27 RContextAttributes attr;
28 RColor **colors = NULL;
29 int i, rmode = RDitheredRendering, ncolors = 0, cpc = 4;
30 char **color_name;
31 XColor color;
32 XSetWindowAttributes val;
33 int visualID = -1;
35 ProgName = strrchr(argv[0], '/');
36 if (!ProgName)
37 ProgName = argv[0];
38 else
39 ProgName++;
41 color_name = (char **)malloc(sizeof(char *) * argc);
42 if (color_name == NULL) {
43 fprintf(stderr, "Cannot allocate memory!\n");
44 exit(1);
47 if (argc > 1) {
48 for (i = 1; i < argc; i++) {
49 if (strcmp(argv[i], "-m") == 0) {
50 rmode = RBestMatchRendering;
51 } else if (strcmp(argv[i], "-d") == 0) {
52 rmode = RDitheredRendering;
53 } else if (strcmp(argv[i], "-c") == 0) {
54 i++;
55 if (i >= argc) {
56 fprintf(stderr, "too few arguments for %s\n", argv[i - 1]);
57 exit(0);
59 if (sscanf(argv[i], "%i", &cpc) != 1) {
60 fprintf(stderr, "bad value for colors per channel: \"%s\"\n", argv[i]);
61 exit(0);
63 } else if (strcmp(argv[i], "-v") == 0) {
64 i++;
65 if (i >= argc) {
66 fprintf(stderr, "too few arguments for %s\n", argv[i - 1]);
67 exit(0);
69 if (sscanf(argv[i], "%i", &visualID) != 1) {
70 fprintf(stderr, "bad value for visual ID: \"%s\"\n", argv[i]);
71 exit(0);
73 } else if (argv[i][0] != '-') {
74 color_name[ncolors++] = argv[i];
75 } else {
76 print_help();
77 exit(1);
82 if (ncolors == 0) {
83 print_help();
84 exit(1);
87 dpy = XOpenDisplay("");
88 if (!dpy) {
89 puts("cant open display");
90 exit(1);
92 attr.flags = RC_RenderMode | RC_ColorsPerChannel;
94 attr.render_mode = rmode;
95 attr.colors_per_channel = cpc;
97 if (visualID >= 0) {
98 attr.flags |= RC_VisualID;
99 attr.visualid = visualID;
102 ctx = RCreateContext(dpy, DefaultScreen(dpy), &attr);
104 if (!ctx) {
105 printf("could not initialize graphics library context: %s\n", RMessageForError(RErrorCode));
106 exit(1);
109 colors = malloc(sizeof(RColor *) * (ncolors + 1));
110 for (i = 0; i < ncolors; i++) {
111 if (!XParseColor(dpy, ctx->cmap, color_name[i], &color)) {
112 printf("could not parse color \"%s\"\n", color_name[i]);
113 exit(1);
114 } else {
115 colors[i] = malloc(sizeof(RColor));
116 colors[i]->red = color.red >> 8;
117 colors[i]->green = color.green >> 8;
118 colors[i]->blue = color.blue >> 8;
119 printf("0x%02x%02x%02x\n", colors[i]->red, colors[i]->green, colors[i]->blue);
122 colors[i] = NULL;
124 val.background_pixel = ctx->black;
125 val.colormap = ctx->cmap;
126 val.backing_store = Always;
127 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 750, 250,
128 0, ctx->depth, InputOutput, ctx->visual,
129 CWColormap | CWBackPixel | CWBackingStore, &val);
130 XMapRaised(dpy, win);
131 XFlush(dpy);
133 imgh = RRenderMultiGradient(250, 250, colors, RGRD_HORIZONTAL);
134 imgv = RRenderMultiGradient(250, 250, colors, RGRD_VERTICAL);
135 imgd = RRenderMultiGradient(250, 250, colors, RGRD_DIAGONAL);
136 RConvertImage(ctx, imgh, &pix);
137 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 0, 0);
139 RConvertImage(ctx, imgv, &pix);
140 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 250, 0);
142 RConvertImage(ctx, imgd, &pix);
143 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 500, 0);
145 XFlush(dpy);
147 getchar();
148 return 0;