1 xtk_colorpicker
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_colorpicker.h

A color chooser dialog with a hue wheel, a hex entry and a row of saved colors.

The color picker is a small dialog for choosing an RGB color: a hue ring with a saturation and value triangle (an XtkSchemeWheel, see xtk_scheme_wheel.h), a preview swatch, a #RRGGBB entry, a palette of 12 custom colors with a "+ Add" button, and OK and Cancel buttons. It is a top-level override-redirect window with its own titlebar ("Choose Color", draggable, with a close icon) drawn in the theme's colors, so it looks the same under any window manager. It is not tied to any other widget; anything that needs a color from the user can use it.

Making one. xtk_colorpicker_create() makes the dialog hidden. Pass the program's XtkConfig (after the theme is applied) and its shared XtkClipboard, so that the entry can cut, copy and paste. The dialog keeps the config pointer, so the config must live as long as the dialog. Show it with xtk_colorpicker_show(), which centers it over the parent window, and destroy it with xtk_colorpicker_destroy(). On a theme change call xtk_colorpicker_set_theme().

Events. While the dialog is shown, pass every event to xtk_colorpicker_handle_event() before other widgets, and call xtk_colorpicker_update() on each pass of the event loop so that the entry's cursor blinks and its flashes end. The return value says what happened:

  • 0: the event was not for the dialog (or the dialog is hidden); give it to other widgets.
  • 1: the user accepted the color with OK or Enter. The dialog is hidden; read the color with xtk_colorpicker_get_color().
  • 2: the user canceled with Cancel, Escape or the close icon. The dialog is hidden, and xtk_colorpicker_get_color() gives the color it was shown with.
  • 3: the dialog used the event and stays open (a drag on the wheel, a click on a swatch, typing, a redraw).

Use by the user. Dragging on the ring sets the hue and dragging in the triangle sets saturation and value. Typing a color in the entry and pressing Enter, or moving the focus away, sets it; a valid new value makes the entry's border flash green, and an invalid one flashes red and puts the last good value back. Enter accepts the dialog. "+ Add" stores the current color in the next free custom slot (when all 12 are full, an old one is replaced); a click on a custom swatch selects its color, a right-click removes it, and a drag holds it up against any color on the screen for comparison. Ctrl+Tab and Ctrl+Shift+Tab move the keyboard focus across the entry and the buttons (and the format list when it is shown).

Code formats. For a program that puts colors into source code, xtk_colorpicker_set_formats_visible() adds a list of 16 formats (hex, C, Python, Java, C#, Go, SDL2, OpenCV, Rust, GLSL, Android) under the preview and moves the entry to a full-width row under the wheel, where it shows and reads the color in the chosen format. xtk_colorpicker_format_color() formats a color the same way without a dialog.

Theme keys. The dialog has no section of its own. It reads:

  • [main] background: the dialog's background and the color of empty custom slots;
  • [toolwindow] header-background, header-foreground, header-border, icon-foreground, icon-hover-foreground, icon-hover-background, font-family and font-size: the titlebar, its close icon and the window border;
  • the [button] block for its buttons, the [combobox] block for the format list, and the entry's colors and config.entry_widget geometry for the entry (see xtk_entry.h).

Example. A real loop also calls xtk_colorpicker_update(picker) on each pass, for example after XPending() returns 0.

XtkConfig config;
xtk_config_init_defaults(&config);
XtkTheme *theme = xtk_theme_new();
if (theme && xtk_theme_builtin("Dark", theme)) xtk_theme_apply(&config, theme);
XtkClipboard *clipboard = xtk_clipboard_create(display);

XtkColorPicker *picker = xtk_colorpicker_create(display, screen, window, &config, clipboard);
ConfigColor ink = xtk_config_parse_color("#E06C75");
xtk_colorpicker_show(picker, ink);

for (;;) {
    XEvent event;
    XNextEvent(display, &event);
    int result = xtk_colorpicker_handle_event(picker, &event);
    if (result == 1) ink = xtk_colorpicker_get_color(picker);
    if (result) continue;
    // ... other widgets
}
 * 

Limitations.

  • There is no alpha: colors come back opaque.
  • The custom colors are not saved; they last as long as the dialog.
  • Escape and Enter are seen only when the dialog's own window or the focused entry gets the key; an override-redirect window does not always get the keyboard.
#include "xtk_colorpicker.h"

Types: XtkColorPicker

Functions: xtk_colorpicker_create, xtk_colorpicker_destroy, xtk_colorpicker_set_theme, xtk_colorpicker_show, xtk_colorpicker_hide, xtk_colorpicker_set_custom_palette_visible, xtk_colorpicker_set_wheel_model, xtk_colorpicker_get_wheel_model, xtk_colorpicker_format_count, xtk_colorpicker_format_name, xtk_colorpicker_format_color, xtk_colorpicker_set_formats_visible, xtk_colorpicker_set_format, xtk_colorpicker_get_format, xtk_colorpicker_handle_event, xtk_colorpicker_get_color, xtk_colorpicker_draw, xtk_colorpicker_update

Types

XtkColorPicker

typedef struct XtkColorPicker XtkColorPicker;

A color picker dialog; opaque, made by xtk_colorpicker_create().

Functions

xtk_colorpicker_create

XtkColorPicker *xtk_colorpicker_create(
    Display *display,
    int screen,
    Window parent,
    const XtkConfig *config,
    XtkClipboard *clipboard);

Creates a color picker dialog, hidden until xtk_colorpicker_show().

The dialog starts at the color red, with the HSV wheel model, the custom palette shown and the code formats hidden.

Parameter Description
display The display connection.
screen The screen number.
parent The window the dialog belongs to: it is centered over it when shown and gets the focus back when the dialog hides. May be None.
config The theme. The dialog keeps the pointer, so it must stay valid. NULL works, with default colors and no title text.
clipboard The program's shared clipboard, for cut, copy and paste in the entry; NULL leaves the entry without them.

Returns: The new dialog, which the caller frees with xtk_colorpicker_destroy(); NULL when display is NULL, memory runs out or the window cannot be made.

xtk_colorpicker_destroy

void xtk_colorpicker_destroy(XtkColorPicker *colorpicker);

Destroys the dialog, its windows and its child widgets.

Parameter Description
colorpicker The color picker. NULL does nothing.

xtk_colorpicker_set_theme

void xtk_colorpicker_set_theme(XtkColorPicker *colorpicker, const XtkConfig *config);

Applies a changed theme to the dialog and all its parts, and redraws.

Call it after a theme change, with the same config whose contents changed or with another one. The dialog keeps the new pointer.

Parameter Description
colorpicker The color picker. NULL does nothing.
config The config with the new theme. NULL does nothing.

xtk_colorpicker_show

void xtk_colorpicker_show(XtkColorPicker *colorpicker, ConfigColor color);

Shows the dialog, centered over its parent, at a given color.

The dialog is mapped and raised (also when it is already shown), the wheel, preview and entry are set to the color, and the entry gets the keyboard focus.

Parameter Description
colorpicker The color picker. NULL does nothing.
color The color to start at; its alpha is ignored.

xtk_colorpicker_hide

void xtk_colorpicker_hide(XtkColorPicker *colorpicker);

Hides the dialog and gives the keyboard focus back to its parent.

handle_event hides the dialog itself on OK and Cancel; call this to close it from the program.

Parameter Description
colorpicker The color picker. NULL does nothing.

xtk_colorpicker_set_custom_palette_visible

void xtk_colorpicker_set_custom_palette_visible(XtkColorPicker *colorpicker, int visible);

Shows or hides the 12 custom color swatches and the "+ Add" button.

They are shown by default. Hidden swatches keep their colors, so showing them again brings them back as they were. The dialog does not change size.

Parameter Description
colorpicker The color picker. NULL does nothing.
visible 1 to show the palette, 0 to hide it.

xtk_colorpicker_set_wheel_model

void xtk_colorpicker_set_wheel_model(XtkColorPicker *colorpicker, CSP_WheelModel model);

Changes the color model of the dialog's wheel.

The wheel can show any CSP_WheelModel (RGB, RYB, HSV, HSL, LCh(ab), LCh(uv), Oklch, JzAzBz, CAM16-UCS). The current color stays the same: the marker and the triangle point move to where that color is in the new model. The default is CSP_WHEEL_MODEL_HSV.

Parameter Description
colorpicker The color picker. NULL does nothing.
model The wheel model.

xtk_colorpicker_get_wheel_model

CSP_WheelModel xtk_colorpicker_get_wheel_model(const XtkColorPicker *colorpicker);

Gives the color model of the dialog's wheel.

Parameter Description
colorpicker The color picker.

Returns: The wheel model; CSP_WHEEL_MODEL_HSV when colorpicker is NULL.

xtk_colorpicker_format_count

int xtk_colorpicker_format_count(void);

Gives the number of code formats that xtk_colorpicker_format_color() knows.

Returns: The number of formats (16).

xtk_colorpicker_format_name

const char *xtk_colorpicker_format_name(int index);

Gives a code format's name as the format list shows it.

For example "Hex (#RRGGBB)", "C/C++ (RGB)" or "GLSL (RGBA f)".

Parameter Description
index The format, from 0; 0 is the hex form.

Returns: The name, a static string; "" when index is out of range.

xtk_colorpicker_format_color

void xtk_colorpicker_format_color(int index, ConfigColor color, char *out, size_t size);

Writes a color as text in a code format.

For example format 0 gives "#E06C75", format 1 "0xE06C75" and format 14 "vec4(0.878, 0.424, 0.459, 1.000)". The alpha is always opaque (255 or 1.0), because the picker has no alpha.

Parameter Description
index The format, from 0. A value out of range gives the hex form.
color The color; its alpha is ignored.
out Receives the text, ended with a NUL byte and cut short when it does not fit.
size The size of out in bytes. NULL or 0 writes nothing.

xtk_colorpicker_set_formats_visible

void xtk_colorpicker_set_formats_visible(XtkColorPicker *colorpicker, int visible);

Shows or hides the code formats.

When shown, a format list replaces the entry beside the preview, and the entry moves to a new row under the wheel, as wide as the dialog, where it shows and reads the color in the chosen format. The dialog grows by that row. They are hidden when the dialog is made.

Parameter Description
colorpicker The color picker. NULL does nothing.
visible 1 to show the formats, 0 to hide them.

xtk_colorpicker_set_format

void xtk_colorpicker_set_format(XtkColorPicker *colorpicker, int index);

Chooses the code format, for example one the program saved from an earlier run.

Parameter Description
colorpicker The color picker. NULL does nothing.
index The format; a value out of range chooses 0, the hex form.

xtk_colorpicker_get_format

int xtk_colorpicker_get_format(const XtkColorPicker *colorpicker);

Gives the chosen code format, for the program to save.

Parameter Description
colorpicker The color picker.

Returns: The format's index; 0 when colorpicker is NULL.

xtk_colorpicker_handle_event

int xtk_colorpicker_handle_event(XtkColorPicker *colorpicker, XEvent *event);

Gives an event to the dialog.

Call it for every event while the dialog is shown, before the program's other widgets.

Parameter Description
colorpicker The color picker.
event The event.

Returns: 0 when the event is not for the dialog, or the dialog is hidden or colorpicker is NULL: give the event to other widgets. 1 when the user accepted with OK or Enter: the dialog is hidden and xtk_colorpicker_get_color() gives the chosen color. 2 when the user canceled with Cancel, Escape or the close icon: the dialog is hidden, and xtk_colorpicker_get_color() gives the color given to xtk_colorpicker_show() again. 3 when the dialog used the event and stays open.

xtk_colorpicker_get_color

ConfigColor xtk_colorpicker_get_color(const XtkColorPicker *colorpicker);

Gives the color the dialog shows now.

Parameter Description
colorpicker The color picker.

Returns: The color, with alpha 1.0; all zeros when colorpicker is NULL.

xtk_colorpicker_draw

void xtk_colorpicker_draw(XtkColorPicker *colorpicker);

Redraws the whole dialog: its frame, titlebar and all its parts.

The dialog redraws itself on Expose and on changes; a program rarely needs this.

Parameter Description
colorpicker The color picker. NULL, or a hidden dialog, does nothing.

xtk_colorpicker_update

void xtk_colorpicker_update(XtkColorPicker *colorpicker);

Moves the dialog's timers on: the entry's cursor blink and the end of its green or red flash.

Call it on each pass of the event loop while the dialog is shown, as for other blinking widgets. It does nothing when the dialog is hidden.

Parameter Description
colorpicker The color picker. NULL does nothing.