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 palette stored in the X11 colormap. Every pixel in every ImageElement is stored as a 4-bit palette index (0–15), and every drawing primitive — including text, buttons, rectangles, and image data — references these indices. The palette API lets you query the live RGB value of any entry, replace individual entries at runtime, and restore the factory defaults. Because the palette is global, a single PaletteModify call instantly changes the appearance of every element that uses that color index.

PaletteQuery

Queries the current RGB values of a palette entry from the X11 colormap.
void PaletteQuery(int idx, unsigned char* r, unsigned char* g, unsigned char* b);
Calls XQueryColor on the Colormap for the pixel value stored at colors[idx] and converts the 16-bit X11 channel values to 8-bit by dividing by 257. The result reflects the true color allocated in the colormap at query time, including any runtime modifications made with PaletteModify.
idx
int
required
Palette index to query (0–15).
r
unsigned char*
required
Pointer to receive the red channel value (0–255).
g
unsigned char*
required
Pointer to receive the green channel value (0–255).
b
unsigned char*
required
Pointer to receive the blue channel value (0–255).

PaletteModify

Replaces one palette entry with a new RGB color.
void PaletteModify(int idx, unsigned char r, unsigned char g, unsigned char b, int redraw);
Frees the old X11 colormap allocation for colors[idx] with XFreeColors, then allocates a new color with XAllocColor using the supplied RGB values (scaled to 16-bit internally by multiplying by 257). The resulting pixel value is stored back into colors[idx]. If redraw is non-zero, RedrawAllImages() is called immediately after to force all ImageElement canvases to re-upload their pixel data with the new color mapping.
Palette modifications are global. All elements and image pixel data using the modified index will immediately display the new color, including retained Rect, Text, Button, and Image elements. Pass redraw = 1 whenever you want the change to take effect visually on the next frame.
idx
int
required
Palette index to replace (0–15).
r
unsigned char
required
New red channel value (0–255).
g
unsigned char
required
New green channel value (0–255).
b
unsigned char
required
New blue channel value (0–255).
redraw
int
required
Pass 1 to call RedrawAllImages() after modifying the color, forcing all image elements to re-render with the new palette. Pass 0 to defer the redraw (useful when modifying multiple entries in a loop — call RedrawAllImages() manually afterwards).

PaletteReset

Restores all 16 palette entries to the LWXGL factory defaults and redraws all image elements.
void PaletteReset();
Frees all 16 current colormap allocations in one XFreeColors call, then re-allocates each entry from the compile-time color_palette table. RedrawAllImages() is called unconditionally at the end.

Default palette

The table below lists the 16 factory-default palette entries as defined in main.cc.
IndexConstantNameRGB
0x0CLR_BLACKBlack000
0x1CLR_BLUEDark Blue33173
0x2CLR_GREENDark Green01700
0x3CLR_CYANDark Cyan0168168
0x4CLR_REDDark Red18666
0x5CLR_MAGENTADark Magenta1680168
0x6CLR_ORANGEOrange23012634
0x7CLR_LGRAYLight Gray168168168
0x8CLR_GRAYDark Gray858783
0x9CLR_LBLUELight Blue8787255
0xACLR_LGREENLight Green8525585
0xBCLR_LCYANLight Cyan96240240
0xCCLR_LREDLight Red2558585
0xDCLR_LMAGENTALight Magenta24084240
0xECLR_YELLOWYellow24424254
0xFCLR_WHITEWhite255255255

Code example

#include "libLWXGL.h"
#include <stdio.h>

void print_palette(void) {
    for (int i = 0; i < 16; i++) {
        unsigned char r, g, b;
        PaletteQuery(i, &r, &g, &b);
        printf("  [%2d] rgb(%3d, %3d, %3d)\n", i, r, g, b);
    }
}

int main(void) {
    CreateWindow(320, 240, "Palette Demo", CLR_BLACK);

    printf("Default palette:\n");
    print_palette();

    // Replace CLR_BLUE (index 1) with a custom indigo, redraw immediately
    PaletteModify(CLR_BLUE, 75, 0, 130, 1);

    // Batch-modify several entries, then redraw once at the end
    PaletteModify(CLR_RED,   220, 20,  60, 0);  // Crimson
    PaletteModify(CLR_GREEN,  34, 139, 34, 0);  // Forest green
    PaletteModify(CLR_YELLOW, 255, 215, 0, 0);  // Gold
    RedrawAllImages();

    printf("\nModified palette:\n");
    print_palette();

    MainWindowLoop(30, NULL);

    // Restore factory defaults before exit
    PaletteReset();

    TerminateWindow();
    return 0;
}
When modifying multiple palette entries in sequence, pass redraw = 0 for all but the last call (or for all of them and call RedrawAllImages() once manually) to avoid triggering a full image redraw for each individual color change.

Build docs developers (and LLMs) love