XCreateColormap(3)
๐ XCreateColormap(3) โ the libX11 era โจ
Okay bestie, sit down, grab a strawberry ๐, because today we're talking colormaps โ the Xlib functions XCreateColormap, XCopyColormapAndFree, XFreeColormap, and the XColor structure. That's create, copy, or destroy colormaps and the color struct itself. It's giving foundational. It's giving "your pixels literally cannot exist without me." ๐
๐ธ SYNTAX ๐ธ
Colormap XCreateColormap(Display *display, Window w, Visual *visual, int alloc);
Colormap XCopyColormapAndFree(Display *display, Colormap colormap);
int XFreeColormap(Display *display, Colormap colormap);
Three little calls, one big vibe. Main character energy fr fr. ๐
๐ ARGUMENTS ๐
allocโ tells the server which colormap entries get allocated. Your options areAllocNoneorAllocAll. That's it, that's the whole menu, no secret third thing. ๐colormapโ the colormap you wanna create, copy, set, or destroy. Basically whichever one you're currently obsessing over.displayโ the connection to the X server. Your link to the mothership, iykyk.visualโ a visual type that's supported on the screen. And listen: if that visual type is not one supported by the screen, you get aBadMatcherror. Not a vibe. Read your visuals. ๐wโ the window on whose screen you want the colormap created. Tiny plot twist: the window is only used to figure out which screen you mean. It's a passenger princess, not the driver. ๐โจ
โจ DESCRIPTION โจ
So XCreateColormap creates a colormap of the specified visual type, for the screen that your specified window lives on, and hands you back the colormap ID. And again โ bestie, hear me โ the window is only there to determine the screen. That's it. That's its whole job. ๐ซถ
Now the juicy part, the initial values of the entries:
- For the visual classes
GrayScale,PseudoColor, andDirectColor, the initial values of the colormap entries are undefined. Undefined! Meaning don't come crying to me about what's in there, nobody promised you anything. ๐ญ - For
StaticGray,StaticColor, andTrueColor, the entries do have defined values ๐ โ but those values are specific to the visual and are not defined by X. So defined, but not that defined. It's giving "trust me bro." - Also for
StaticGray,StaticColor, andTrueColor:allocmust beAllocNone, or you get aBadMatcherror. Periodt. No exceptions, no negotiating. ๐ซ - For the other visual classes, if
allocisAllocNone, the colormap starts out with zero allocated entries and clients can go ahead and allocate them. Fresh canvas energy. ๐จ
Want the whole scoop on visual types? See section 3.1. Go read it, it's worth it, I promise. ๐
๐ the AllocAll situation ๐
If alloc is AllocAll, the entire colormap is allocated writable. Slay. But โ the initial values of all allocated entries are undefined. Again. Yes. We know. ๐
For GrayScale and PseudoColor, it's as if an XAllocColorCells call returned all pixel values from zero to N - 1, where N is the colormap entries value in the specified visual. Math but make it fashion. ๐
For DirectColor, it's as if an XAllocColorPlanes call returned a pixel value of zero and red_mask, green_mask, and blue_mask values containing the same bits as the corresponding masks in the specified visual. Mask match, cute. โจ
โ ๏ธ But in all cases โ every single one โ none of these entries can be freed by using XFreeColors. You allocated the whole map, you live with the whole map. No takebacks. ๐
XCreateColormap can generate BadAlloc, BadMatch, BadValue, and BadWindow errors. Collect them all, I guess? ๐ญ
๐ฆ XCopyColormapAndFree โ the glow-up copy ๐ฆ
This one creates a colormap of the same visual type, for the same screen as the specified colormap, and returns the new colormap ID. Same vibes, new ID. ๐
It also moves all of the client's existing allocation from the specified colormap over to the new colormap โ with their color values intact and their read-only or writable characteristics intact โ and then frees those entries in the specified colormap. Color values in the other entries of the new colormap are undefined. (You knew that was coming. ๐)
Two paths, bestie:
- If the specified colormap was created by the client with
allocset toAllocAll, the new colormap is also created withAllocAll, all color values for all entries get copied from the specified colormap, and then all entries in the specified colormap are freed. Clean slate, full handoff. ๐ซฑ - If the specified colormap was not created by the client with
AllocAll, then the allocations being moved are all those pixels and planes that have been allocated by the client usingXAllocColor,XAllocNamedColor,XAllocColorCells, orXAllocColorPlanesโ and that have not been freed since they were allocated. Track your allocations, bestie, keep receipts. ๐งพ
XCopyColormapAndFree can generate BadAlloc and BadColor errors. ๐
๐ง XFreeColormap โ the goodbye ๐ง
XFreeColormap deletes the association between the colormap resource ID and the colormap, and frees the colormap storage. Bye bestie, it was real. ๐ฅฒ
๐จ But heads up: this function has no effect on the default colormap for a screen. That one's protected. Untouchable. Iconic.
The deets:
- If the specified colormap is an installed map for a screen, it gets uninstalled. (See
XUninstallColormap.) - If the specified colormap is defined as the colormap for a window โ whether by
XCreateWindow,XSetWindowColormap, orXChangeWindowAttributesโ thenXFreeColormapchanges the colormap associated with that window toNoneand generates aColormapNotifyevent. - And X does not define the colors displayed for a window with a colormap of
None. That's a mystery even to me. ๐ฎ
XFreeColormap can generate a BadColor error. ๐ธ
๐ STRUCTURES ๐
The XColor structure contains:
typedef struct {
unsigned long pixel; /* pixel value */
unsigned short red, green, blue; /* rgb values */
char flags; /* DoRed, DoGreen, DoBlue */
char pad;
} XColor;
Look at her. Gorgeous. ๐
Now the rules for red, green, and blue โ these values are always in the range 0 to 65535 inclusive, independent of how many bits the display hardware actually uses. The server scales them down to whatever range the hardware wants. It's like me translating my group chat for the group project. ๐ญ
- Black is
(0,0,0). - White is
(65535,65535,65535).
And for some functions, the flags member controls which of red, green, and blue gets used โ it can be the inclusive OR of zero or more of DoRed, DoGreen, and DoBlue. Pick your fighters. โญ
๐ฅ DIAGNOSTICS ๐ฅ
BadAllocโ the server failed to allocate the requested resource or server memory. It tried, it ran out, it's over. ๐ญBadColorโ a value for aColormapargument does not name a definedColormap. You referenced something that isn't real. Not me when I say I'll sleep early. ๐BadMatchโ anInputOnlywindow is used as aDrawable. Wrong tool, wrong job.BadMatchโ some argument or pair of arguments has the correct type and range but fails to match in some other way required by the request. Right on paper, wrong in context. It's giving situationship. ๐BadValueโ some numeric value falls outside the range of values accepted by the request. Unless a specific range is specified for an argument, the full range defined by the argument's type is accepted. Any argument defined as a set of alternatives can generate this error. Boundaries, bestie. ๐BadWindowโ a value for aWindowargument does not name a definedWindow. Fake window, no cap. ๐ช
๐ซถ SEE ALSO ๐ซถ
XAllocColor(3)XChangeWindowAttributes(3)XCreateWindow(3)XQueryColor(3)XStoreColors(3)Xlib - C Language X Interface
Go read your friends, they miss you. ๐๐
Ok that's the whole page bestie, go forth and allocate responsibly ๐ โจ
Signing off: this is the X Version 11 release, libX11 1.8.14 ๐ โ catch you in the source, muah! ๐๐ฆ
๐ฆ where this page comes from
- alpine 3.24: libx11-doc 1.8.14-r0 ยท X11
- arch rolling: libx11 1.8.14-1 ยท MIT AND X11
- nixos 26.05: libx11 1.8.14
- nixpkgs unstable: libx11 1.8.14
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms