11 RImage
*imgh
, *imgv
, *imgd
;
17 printf("usage: %s [-options] color1 [color2 ...]\n", ProgName
);
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;
32 XSetWindowAttributes val
;
35 ProgName
= strrchr(argv
[0], '/');
41 color_name
= (char **)malloc(sizeof(char *) * argc
);
42 if (color_name
== NULL
) {
43 fprintf(stderr
, "Cannot allocate memory!\n");
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) {
56 fprintf(stderr
, "too few arguments for %s\n", argv
[i
- 1]);
59 if (sscanf(argv
[i
], "%i", &cpc
) != 1) {
60 fprintf(stderr
, "bad value for colors per channel: \"%s\"\n", argv
[i
]);
63 } else if (strcmp(argv
[i
], "-v") == 0) {
66 fprintf(stderr
, "too few arguments for %s\n", argv
[i
- 1]);
69 if (sscanf(argv
[i
], "%i", &visualID
) != 1) {
70 fprintf(stderr
, "bad value for visual ID: \"%s\"\n", argv
[i
]);
73 } else if (argv
[i
][0] != '-') {
74 color_name
[ncolors
++] = argv
[i
];
87 dpy
= XOpenDisplay("");
89 puts("cant open display");
92 attr
.flags
= RC_RenderMode
| RC_ColorsPerChannel
;
94 attr
.render_mode
= rmode
;
95 attr
.colors_per_channel
= cpc
;
98 attr
.flags
|= RC_VisualID
;
99 attr
.visualid
= visualID
;
102 ctx
= RCreateContext(dpy
, DefaultScreen(dpy
), &attr
);
105 printf("could not initialize graphics library context: %s\n", RMessageForError(RErrorCode
));
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
]);
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
);
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
);
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);