2 * XCrDynCmap.c - X11 library routine to create dynamic colormaps.
4 * Copyright (c) 1990, 2011, Oracle and/or its affiliates. All rights reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
25 * Author: Patrick J. Naughton
33 #include <X11/Xutil.h>
37 XCreateDynamicColormap(
40 Colormap
*cmap
, /* return */
41 Visual
**visual
, /* return */
49 unsigned long pixels
[256];
56 planes
= DisplayPlanes(dsp
, screen
);
57 if (!XMatchVisualInfo(dsp
, screen
, planes
, PseudoColor
, &vinfo
))
58 if (!XMatchVisualInfo(dsp
, screen
, planes
, GrayScale
, &vinfo
))
59 if (!XMatchVisualInfo(dsp
, screen
, planes
, DirectColor
, &vinfo
))
63 *visual
= vinfo
.visual
;
64 *cmap
= XCreateColormap(dsp
, RootWindow(dsp
, screen
),
66 ncolors
= vinfo
.colormap_size
;
71 allocReturn
= XAllocColorCells(dsp
, *cmap
,
75 /* This should return Success, but it doesn't... Xlib bug?
76 * (I'll ignore the return value for now...)
79 if (allocReturn
!= Success
)
83 for (i
= 0; i
< ncolors
- count
; i
++) {
84 colors
[i
].pixel
= pixels
[i
];
85 XQueryColor(dsp
, DefaultColormap(dsp
, screen
), &colors
[i
]);
87 for (i
= ncolors
- count
; i
< ncolors
; i
++) {
88 colors
[i
].pixel
= pixels
[i
];
89 colors
[i
].red
= *red
++ << 8;
90 colors
[i
].green
= *green
++ << 8;
91 colors
[i
].blue
= *blue
++ << 8;
92 colors
[i
].flags
= DoRed
| DoGreen
| DoBlue
;
94 XStoreColors(dsp
, *cmap
, colors
, ncolors
);