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 renders every pixel using a fixed 16-color palette. Each color is allocated in the X server’s colormap at startup, and every image element stores pixel data as 4-bit palette indices rather than direct RGB values. This means changing a single palette entry immediately affects every pixel that uses that index — across all image elements, text elements, rectangles, and buttons — as soon as those elements are next redrawn.

Default Palette

The default palette is defined in main.cc and loaded at CreateWindow time. Use these constants (defined in libLWXGL.h) to refer to palette entries by name throughout your application.
IndexConstantDefault RGB
0x0CLR_BLACK(0, 0, 0)
0x1CLR_BLUE(3, 3, 173)
0x2CLR_GREEN(0, 170, 0)
0x3CLR_CYAN(0, 168, 168)
0x4CLR_RED(186, 6, 6)
0x5CLR_MAGENTA(168, 0, 168)
0x6CLR_ORANGE(230, 126, 34)
0x7CLR_LGRAY(168, 168, 168)
0x8CLR_GRAY(85, 87, 83)
0x9CLR_LBLUE(87, 87, 255)
0xACLR_LGREEN(85, 255, 85)
0xBCLR_LCYAN(96, 240, 240)
0xCCLR_LRED(255, 85, 85)
0xDCLR_LMAGENTA(240, 84, 240)
0xECLR_YELLOW(244, 242, 54)
0xFCLR_WHITE(255, 255, 255)

PaletteQuery

void PaletteQuery(int idx, unsigned char* r, unsigned char* g, unsigned char* b);
Reads the current RGB values of palette entry idx via XQueryColor. The values returned reflect the live colormap state, so they will differ from the defaults if you have called PaletteModify.
idx
int
required
Palette index to query — must be in the range 0–15.
r
unsigned char*
required
Output pointer. Receives the red component (0–255).
g
unsigned char*
required
Output pointer. Receives the green component (0–255).
b
unsigned char*
required
Output pointer. Receives the blue component (0–255).

PaletteModify

void PaletteModify(int idx, unsigned char r, unsigned char g, unsigned char b, int redraw);
Replaces palette entry idx with a new RGB color. The old X server color allocation is freed, and a new one is requested via XAllocColor. If redraw is 1, RedrawAllImages() is called immediately to update all image elements.
idx
int
required
Palette index to modify — must be in the range 0–15.
r
unsigned char
required
New red component (0–255).
g
unsigned char
required
New green component (0–255).
b
unsigned char
required
New blue component (0–255).
redraw
int
required
Pass 1 to trigger an immediate full redraw of all image elements. Pass 0 to defer — useful when modifying several entries in a loop (call RedrawAllImages() manually after all changes).
Modifying a palette entry affects every element that renders that index, including buttons, text, and all image elements. If you want per-image color variation, use different palette slots or draw with different indices rather than swapping palette entries at runtime.

PaletteReset

void PaletteReset(void);
Frees all 16 current color allocations and re-allocates each one from the built-in color_palette[] defaults, then calls RedrawAllImages(). Use this to undo a temporary palette swap applied by a TGA with change_palette = 1.
TGA files loaded with change_palette = 1 override the palette each time DrawIndexedTGA is called. Call PaletteReset() after drawing them if you want to restore the standard colors for subsequent rendering.

Code Example

#include "libLWXGL.h"

// Cycle the background color (index 0) through a dim red animation
static unsigned char flash_r = 0;
static int flashing = 0;

void start_flash(void) {
    flashing = 1;
    flash_r  = 0;
}

void on_frame(int tick, float dt) {
    if (flashing) {
        flash_r = (unsigned char)(flash_r + 8);
        // Modify index 0 (black) to a dim red — no redraw until last change
        PaletteModify(CLR_BLACK, flash_r, 0, 0, 0);
        RedrawAllImages();

        if (flash_r >= 248) {
            // Flash complete — restore defaults
            PaletteReset();
            flashing = 0;
        }
    }
}

void on_click(int x, int y, int btn) {
    if (btn == 1) start_flash();
}

int main(void) {
    CreateWindow(320, 200, "Palette Demo", CLR_BLACK, FLAG_NONE);
    CreateImage(1, 0, 0, 0, 0);

    // Query a palette entry to confirm its value
    unsigned char r, g, b;
    PaletteQuery(CLR_RED, &r, &g, &b);
    // r=186, g=6, b=6 (default CLR_RED)

    EventAttachClick(on_click);
    MainWindowLoop(60, on_frame);
    TerminateWindow();
    return 0;
}

Build docs developers (and LLMs) love