Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/DRESsedAlarm184/LWXGL/llms.txt

Use this file to discover all available pages before exploring further.

LWXGL uses a fixed 16-entry indexed color palette rather than arbitrary RGB values. Every color in the library is expressed as a small integer index between 0 and 15, or the special sentinel CLR_NONE (-1) for transparency. The palette is allocated from the X colormap during CreateWindow and shared across all rendering operations, which means a single palette change instantly affects every widget and image that uses the modified index.

The 16 Palette Entries

The default palette is defined in main.cc as the color_palette array. Named constants for each entry are declared in libLWXGL.h.
ConstantValueRGB
CLR_NONE-1Transparent / no-draw
CLR_BLACK0x00, 0, 0
CLR_BLUE0x13, 3, 173
CLR_GREEN0x20, 170, 0
CLR_CYAN0x30, 168, 168
CLR_RED0x4186, 6, 6
CLR_MAGENTA0x5168, 0, 168
CLR_ORANGE0x6230, 126, 34
CLR_LGRAY0x7168, 168, 168
CLR_GRAY0x885, 87, 83
CLR_LBLUE0x987, 87, 255
CLR_LGREEN0xA85, 255, 85
CLR_LCYAN0xB96, 240, 240
CLR_LRED0xC255, 85, 85
CLR_LMAGENTA0xD240, 84, 240
CLR_YELLOW0xE244, 242, 54
CLR_WHITE0xF255, 255, 255
CLR_NONE is never stored in the palette array — it is a sentinel value recognized by drawing functions to mean “skip this draw.” The 16 real palette entries occupy indices 0x0 through 0xF.

Packing Two Colors into One int

Many element-creation functions accept a single int that encodes both a fill color and a border/outline color. The packing uses the low nibble for the fill and the high nibble for the border:
NibbleMeaning
Low nibble (value & 0x0F)Fill / interior color
High nibble ((value >> 4) & 0x0F)Border / outline color
This is expressed in source code with the L(b) and H(b) macros defined in main.cc:
#define L(b)  ((b) & 0x0F)
#define H(b)  (((b) >> 4) & 0x0F)

Building a Packed Color

// Fill = CLR_LGRAY (0x7), border = CLR_WHITE (0xF)
int style = CLR_LGRAY | (CLR_WHITE << 4);   // 0xF7

// Fill = CLR_BLACK (0x0), border = CLR_CYAN (0x3)
int dark_box = CLR_BLACK | (CLR_CYAN << 4); // 0x30

Where Packed Colors Are Used

CreateButton accepts three packed-color arguments — one for each interactive state:
void CreateButton(int id, int x, int y, int w, int h,
                  int u,    // unpressed state: fill | (border << 4)
                  int hvr,  // hover state:     fill | (border << 4)
                  int p,    // pressed state:   fill | (border << 4)
                  const char* label, void (*onclick)(void));
int normal  = CLR_LGRAY   | (CLR_GRAY  << 4);  // 0x87
int hovered = CLR_WHITE   | (CLR_LBLUE << 4);  // 0x9F
int pressed = CLR_LBLUE   | (CLR_BLUE  << 4);  // 0x19

CreateButton(1, 20, 20, 100, 28, normal, hovered, pressed, "Click me", on_click);
CreateInput, CreateCheckbox, and CreateConsole use the same packing scheme for their color parameters.

Runtime Palette Modification

The palette can be changed at any time after CreateWindow using the three palette functions.

PaletteModify

void PaletteModify(int idx, unsigned char r, unsigned char g, unsigned char b, int redraw);
Replaces palette entry idx with the new RGB values. Internally, XFreeColors releases the old pixel value and XAllocColor claims a new one. If redraw is non-zero, RedrawAllImages is called immediately to re-blit every ImageElement using the new color.
// Swap CLR_RED to a softer coral tone
PaletteModify(CLR_RED, 220, 80, 60, 1);
Changing a palette entry affects every element that uses that color index — buttons, rects, text, images, and the window background. There is no per-element override. Use PaletteReset to restore the defaults when needed.

PaletteQuery

void PaletteQuery(int idx, unsigned char* r, unsigned char* g, unsigned char* b);
Reads the current RGB values of palette entry idx from the X colormap into the provided pointers. Useful for saving palette state before making temporary modifications.
unsigned char r, g, b;
PaletteQuery(CLR_GREEN, &r, &g, &b);
// r=0, g=170, b=0 (default)

PaletteReset

void PaletteReset();
Frees all 16 current color allocations and re-allocates them from the original color_palette array in main.cc. Also calls RedrawAllImages to refresh pixel data.
// Restore all colors to their defaults
PaletteReset();

Palette Changes and TGA Images

TGA images can be loaded with an option to replace the image’s black (RGB 0, 0, 0) pixels with the current palette color for a given index. The change_palette parameter in AllocateTGA, AllocateMemoryTGA, and CreateTGAImage enables this behavior:
// Load a TGA; black pixels → current CLR_WHITE color
int ok = AllocateTGA("icon", "/path/to/icon.tga", CLR_WHITE, 0);
When PaletteModify is subsequently called with redraw = 1, RedrawAllImages re-applies this substitution using the updated color, so icons automatically recolor when the palette changes.
To create a recolorable monochrome icon, draw it in pure black on a transparent (or distinct background) color, then load it with a non-negative change_palette index. Cycling the palette entry effectively re-tints all instances of that image.

Build docs developers (and LLMs) love