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

xtk_scheme_wheel.h

A hue ring with draggable markers and a saturation/value triangle, for choosing colors and color schemes.

The scheme wheel is a square child window that shows a hue ring with a triangle inside it. Points on the ring are hues; the triangle shows saturation and value (or chroma and lightness) for the primary marker's hue. The wheel can hold up to 32 markers on the ring, so a program can show a color harmony (complementary, triadic and so on) as several dots and move them together. One marker is the primary one: it is drawn larger, and the triangle belongs to it. xtk_colorpicker.h uses a wheel with one marker as its color chooser.

Color models. The wheel can be drawn in any CSP_WheelModel of xtk_color_space.h: RGB, HSV and HSL (all drawn as HSV), the two artist's RYB wheels, the perceptual models (LCh(ab), LCh(uv), OKLCh, JzCzhz, CAM16-UCS, Lab, OKLab) and wide-gamut RGB spaces (Display P3, Rec.2020, Adobe RGB, ACEScg, ProPhoto). In the perceptual and wide-gamut models, parts of the triangle cannot be shown in sRGB; they are drawn as a gray checkerboard and the triangle point is kept inside the part that can. The default model is CSP_WHEEL_MODEL_RGB.

Hues. Each marker stores a hue from 0 to 1, called its RGB hue. For the two RYB models this is the plain RGB (HSV) hue, and the wheel maps it to its place on the RYB ring. For every other model it is the model's own hue (for example the LCh hue angle divided by 360) and is also its place on the ring. The setters and getters of marker hues use this value; the callback gives the place on the ring.

Making one. xtk_scheme_wheel_create() makes the window in parent but does not map it; call xtk_scheme_wheel_set_position() once to map it. A new wheel has no markers: add at least one with xtk_scheme_wheel_add_marker() or xtk_scheme_wheel_set_base_hue(). The wheel owns its window and frees it in xtk_scheme_wheel_destroy().

Events. Pass every event to xtk_scheme_wheel_handle_event(). With the first button the user can:

  • press in the triangle and drag, to set saturation and value;
  • press on a marker (within 10 pixels) and drag around the center, to move its hue; the marker becomes the primary one;
  • press anywhere else on the ring, to move the primary marker there and drag it from there.

The callback set with xtk_scheme_wheel_set_callback() is called on the press and on each motion of such a drag. The wheel redraws itself after the callback. It takes no keyboard focus.

Drawing. The ring and triangle are made pixel by pixel, sent with the MIT-SHM extension when it is there, and shown through a double buffer. The ring is cached until the model, size, white point or CAM16 viewing conditions change. The setters that change markers or the triangle do not redraw (so that a program can change many markers and draw once); call xtk_scheme_wheel_draw() after them. The exceptions are xtk_scheme_wheel_set_model(), xtk_scheme_wheel_add_marker() and xtk_scheme_wheel_set_marker_visible(), which redraw at once.

Theme keys. None. The wheel's background is black and its checkerboard is two fixed grays.

Example. A wheel that keeps a second marker opposite the first:

static void on_wheel_changed(XtkSchemeWheel *wheel, int marker_index, double hue, void *user_data) {
    (void)marker_index; (void)hue; (void)user_data;
    double base = xtk_scheme_wheel_get_marker_hue(wheel, 0);
    xtk_scheme_wheel_set_marker_hue(wheel, 1, fmod(base + 0.5, 1.0));
}

XtkSchemeWheel *wheel = xtk_scheme_wheel_create(display, window, 10, 10, 220);
xtk_scheme_wheel_set_model(wheel, CSP_WHEEL_MODEL_HSV);
xtk_scheme_wheel_add_marker(wheel, 0.0);   // marker 0: red
xtk_scheme_wheel_add_marker(wheel, 0.5);   // marker 1: cyan
xtk_scheme_wheel_set_callback(wheel, on_wheel_changed, NULL);
xtk_scheme_wheel_set_position(wheel, 10, 10);   // maps it

// in the event loop:
if (xtk_scheme_wheel_handle_event(wheel, &event)) continue;
 * 

Limitations.

#include "xtk_scheme_wheel.h"

Types: XtkSchemeWheel, XtkSchemeWheelCallback

Functions: xtk_scheme_wheel_create, xtk_scheme_wheel_destroy, xtk_scheme_wheel_set_model, xtk_scheme_wheel_get_model, xtk_scheme_wheel_set_base_hue, xtk_scheme_wheel_get_base_hue, xtk_scheme_wheel_add_marker, xtk_scheme_wheel_ensure_markers, xtk_scheme_wheel_set_marker_hue, xtk_scheme_wheel_get_marker_hue, xtk_scheme_wheel_set_marker_visible, xtk_scheme_wheel_get_marker_visible, xtk_scheme_wheel_get_primary_index, xtk_scheme_wheel_set_primary_index, xtk_scheme_wheel_set_independent_sv, xtk_scheme_wheel_get_independent_sv, xtk_scheme_wheel_set_marker_sv, xtk_scheme_wheel_get_marker_sv, xtk_scheme_wheel_set_callback, xtk_scheme_wheel_set_triangle_sv, xtk_scheme_wheel_get_triangle_sv, xtk_scheme_wheel_handle_event, xtk_scheme_wheel_draw, xtk_scheme_wheel_get_window, xtk_scheme_wheel_get_size, xtk_scheme_wheel_set_position, xtk_scheme_wheel_set_size, xtk_scheme_wheel_set_cam16_viewing_conditions, xtk_scheme_wheel_set_white_point, xtk_scheme_wheel_set_alpha, xtk_scheme_wheel_hsv_to_rgb8, xtk_scheme_wheel_rgb8_to_hsv

Types

XtkSchemeWheel

typedef struct XtkSchemeWheel XtkSchemeWheel;

A hue wheel with markers and a saturation/value triangle (opaque).

Make one with xtk_scheme_wheel_create() and free it with xtk_scheme_wheel_destroy().

XtkSchemeWheelCallback

typedef void (*XtkSchemeWheelCallback)(XtkSchemeWheel *scheme_wheel, int marker_index, double hue, void *user_data);

A function the wheel calls when the user changes it.

It is called on the button press and on each motion of a drag. The wheel redraws after the call, so the function may change markers and values (they show at once), but it must not destroy the wheel.

Functions

xtk_scheme_wheel_create

XtkSchemeWheel *xtk_scheme_wheel_create(
    Display *display,
    Window parent,
    int x,
    int y,
    int size);

Creates a wheel as a child window of parent.

The window is not mapped until xtk_scheme_wheel_set_position(). The wheel starts with no markers, the RGB model, the triangle at full saturation and value, a D65 white point and sRGB CAM16 viewing conditions. The ring is 24 pixels wide.

Parameter Description
display The display connection; the wheel uses its default screen.
parent The window the wheel sits in.
x The X position in parent.
y The Y position in parent.
size The width and height in pixels; 0 or less gives 260.

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

xtk_scheme_wheel_destroy

void xtk_scheme_wheel_destroy(XtkSchemeWheel *scheme_wheel);

Destroys the wheel's window and frees the wheel.

Parameter Description
scheme_wheel The wheel. NULL does nothing.

xtk_scheme_wheel_set_model

void xtk_scheme_wheel_set_model(XtkSchemeWheel *scheme_wheel, CSP_WheelModel model);

Sets the color model the ring and triangle are drawn in, and redraws.

The markers keep their RGB hues, so they move to where those hues are on the new ring. For a perceptual or wide-gamut model, the triangle point moves to the most saturated color it can show at the primary hue; to keep a color instead, set it again with xtk_scheme_wheel_rgb8_to_hsv() and the setters after this call.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
model The model.

xtk_scheme_wheel_get_model

CSP_WheelModel xtk_scheme_wheel_get_model(const XtkSchemeWheel *scheme_wheel);

Gives the wheel's color model.

Parameter Description
scheme_wheel The wheel.

Returns: The model; CSP_WHEEL_MODEL_RGB when scheme_wheel is NULL.

xtk_scheme_wheel_set_base_hue

void xtk_scheme_wheel_set_base_hue(XtkSchemeWheel *scheme_wheel, double rgb_hue);

Sets the hue of marker 0 and makes it the primary marker.

When the wheel has no marker, a visible marker 0 is made. It does not redraw; call xtk_scheme_wheel_draw() after the other changes.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
rgb_hue The RGB hue, from 0 to 1; values outside are clamped.

xtk_scheme_wheel_get_base_hue

double xtk_scheme_wheel_get_base_hue(const XtkSchemeWheel *scheme_wheel);

Gives the hue of marker 0.

Parameter Description
scheme_wheel The wheel.

Returns: The RGB hue, from 0 to 1; 0 when there are no markers or scheme_wheel is NULL.

xtk_scheme_wheel_add_marker

int xtk_scheme_wheel_add_marker(XtkSchemeWheel *scheme_wheel, double hue);

Adds a visible marker after the last one, and redraws.

The new marker takes the saturation and value the triangle shows. The primary marker does not change (it is marker 0 at first).

Parameter Description
scheme_wheel The wheel.
hue The RGB hue, from 0 to 1; values outside are clamped.

Returns: The new marker's index; -1 when the wheel already has 32 markers or scheme_wheel is NULL.

xtk_scheme_wheel_ensure_markers

void xtk_scheme_wheel_ensure_markers(XtkSchemeWheel *scheme_wheel, int count);

Adds hidden markers at hue 0 until the wheel has at least count markers.

Use it before a scheme sets the hues and visibility of a fixed number of markers. It does not redraw.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
count The number of markers needed; more than 32 is taken as 32.

xtk_scheme_wheel_set_marker_hue

void xtk_scheme_wheel_set_marker_hue(
    XtkSchemeWheel *scheme_wheel,
    int marker_index,
    double hue);

Sets a marker's hue, without a redraw and without calling the callback.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
marker_index The marker, from 0. A marker that does not exist is ignored.
hue The RGB hue, from 0 to 1; values outside are clamped.

xtk_scheme_wheel_get_marker_hue

double xtk_scheme_wheel_get_marker_hue(
    const XtkSchemeWheel *scheme_wheel,
    int marker_index);

Gives a marker's hue.

Parameter Description
scheme_wheel The wheel.
marker_index The marker, from 0.

Returns: The RGB hue, from 0 to 1; 0 when there is no such marker or scheme_wheel is NULL.

xtk_scheme_wheel_set_marker_visible

void xtk_scheme_wheel_set_marker_visible(
    XtkSchemeWheel *scheme_wheel,
    int marker_index,
    int visible);

Shows or hides a marker, and redraws.

A hidden marker keeps its hue but is not drawn and cannot be grabbed.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
marker_index The marker, from 0. A marker that does not exist is ignored.
visible Nonzero to show it, 0 to hide it.

xtk_scheme_wheel_get_marker_visible

int xtk_scheme_wheel_get_marker_visible(
    const XtkSchemeWheel *scheme_wheel,
    int marker_index);

Tells if a marker is shown.

Parameter Description
scheme_wheel The wheel.
marker_index The marker, from 0.

Returns: 1 when it is shown; 0 when it is hidden, does not exist, or scheme_wheel is NULL.

xtk_scheme_wheel_get_primary_index

int xtk_scheme_wheel_get_primary_index(const XtkSchemeWheel *scheme_wheel);

Gives the primary marker: the large dot the triangle belongs to.

Pressing a marker makes it the primary one.

Parameter Description
scheme_wheel The wheel.

Returns: The marker's index; -1 when there are no markers or scheme_wheel is NULL.

xtk_scheme_wheel_set_primary_index

void xtk_scheme_wheel_set_primary_index(XtkSchemeWheel *scheme_wheel, int marker_index);

Makes a marker the primary one.

The triangle is then drawn for that marker's hue, and with independent saturation and value it shows that marker's own. It does not redraw and does not call the callback.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
marker_index The marker, from 0. A marker that does not exist is ignored.

xtk_scheme_wheel_set_independent_sv

void xtk_scheme_wheel_set_independent_sv(XtkSchemeWheel *scheme_wheel, int independent);

Chooses whether each marker has its own saturation and value.

By default all markers share the triangle's saturation and value. When this is on, the triangle shows and changes the primary marker's own, and each marker keeps its values when another becomes primary. Turning it on gives every marker the values the triangle shows now.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
independent Nonzero for one saturation and value for each marker, 0 for one shared by all.

xtk_scheme_wheel_get_independent_sv

int xtk_scheme_wheel_get_independent_sv(const XtkSchemeWheel *scheme_wheel);

Tells if each marker has its own saturation and value.

Parameter Description
scheme_wheel The wheel.

Returns: 1 when markers have their own; 0 when they share one, or when scheme_wheel is NULL.

xtk_scheme_wheel_set_marker_sv

void xtk_scheme_wheel_set_marker_sv(
    XtkSchemeWheel *scheme_wheel,
    int marker_index,
    double s,
    double v);

Sets a marker's saturation and value.

Without independent values this sets the shared triangle, as xtk_scheme_wheel_set_triangle_sv() does. With them it sets the marker's own, and the triangle too when it is the primary marker. It does not redraw.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
marker_index The marker, from 0. A marker that does not exist is ignored.
s The saturation, from 0 to 1; values outside are clamped.
v The value, from 0 to 1; values outside are clamped.

xtk_scheme_wheel_get_marker_sv

void xtk_scheme_wheel_get_marker_sv(
    const XtkSchemeWheel *scheme_wheel,
    int marker_index,
    double *s,
    double *v);

Gives a marker's saturation and value.

Without independent values, or for a marker that does not exist, these are the triangle's.

Parameter Description
scheme_wheel The wheel. NULL writes nothing.
marker_index The marker, from 0.
s Receives the saturation, from 0 to 1; may be NULL.
v Receives the value, from 0 to 1; may be NULL.

xtk_scheme_wheel_set_callback

void xtk_scheme_wheel_set_callback(
    XtkSchemeWheel *scheme_wheel,
    XtkSchemeWheelCallback callback,
    void *user_data);

Sets the function that is called when the user changes the wheel.

See XtkSchemeWheelCallback for when it is called and what it may do.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
callback The function, or NULL for none.
user_data A pointer passed to the function.

xtk_scheme_wheel_set_triangle_sv

void xtk_scheme_wheel_set_triangle_sv(XtkSchemeWheel *scheme_wheel, double s, double v);

Sets the saturation and value the triangle shows.

In a perceptual or wide-gamut model, a saturation that cannot be shown in sRGB at that value is reduced to the largest one that can, and a small ring marks the point that was asked for. With independent values the primary marker takes them too. It does not redraw and does not call the callback.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
s The saturation (or chroma), from 0 to 1; values outside are clamped.
v The value (or lightness), from 0 to 1; values outside are clamped.

xtk_scheme_wheel_get_triangle_sv

void xtk_scheme_wheel_get_triangle_sv(
    const XtkSchemeWheel *scheme_wheel,
    double *s,
    double *v);

Gives the saturation and value the triangle shows.

Parameter Description
scheme_wheel The wheel. NULL writes nothing.
s Receives the saturation, from 0 to 1; may be NULL.
v Receives the value, from 0 to 1; may be NULL.

xtk_scheme_wheel_handle_event

int xtk_scheme_wheel_handle_event(XtkSchemeWheel *scheme_wheel, XEvent *event);

Gives an event to the wheel.

See the file description for what the user can do.

Parameter Description
scheme_wheel The wheel.
event The event.

Returns: 1 when the wheel used the event: a first-button press that hit the triangle, a marker or the ring, motion during such a drag, a first-button release, or the last Expose of a series. 0 for every other event, for events of other windows, and when an argument is NULL.

xtk_scheme_wheel_draw

void xtk_scheme_wheel_draw(XtkSchemeWheel *scheme_wheel);

Draws the wheel into its window and flushes the display.

Call it after setters that do not redraw.

Parameter Description
scheme_wheel The wheel. NULL does nothing.

xtk_scheme_wheel_get_window

Window xtk_scheme_wheel_get_window(XtkSchemeWheel *scheme_wheel);

Gives the wheel's X window.

Parameter Description
scheme_wheel The wheel.

Returns: The window; None when scheme_wheel is NULL.

xtk_scheme_wheel_get_size

void xtk_scheme_wheel_get_size(
    const XtkSchemeWheel *scheme_wheel,
    int *width,
    int *height);

Gives the wheel's size; width and height are the same.

Parameter Description
scheme_wheel The wheel. NULL writes nothing.
width Receives the width in pixels; may be NULL.
height Receives the height in pixels; may be NULL.

xtk_scheme_wheel_set_position

void xtk_scheme_wheel_set_position(XtkSchemeWheel *scheme_wheel, int x, int y);

Moves the wheel in its parent and maps its window.

Call it once after xtk_scheme_wheel_create(), even with the same position, because the window is mapped only here.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
x The new X position in the parent.
y The new Y position in the parent.

xtk_scheme_wheel_set_size

void xtk_scheme_wheel_set_size(XtkSchemeWheel *scheme_wheel, int size);

Changes the wheel's size.

The ring and triangle are made again at the next draw; call xtk_scheme_wheel_draw() after it.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
size The new width and height in pixels; 0 or less does nothing.

xtk_scheme_wheel_set_cam16_viewing_conditions

void xtk_scheme_wheel_set_cam16_viewing_conditions(
    XtkSchemeWheel *scheme_wheel,
    CSP_CAM16ViewingConditions vc);

Sets the CAM16 viewing conditions for the CAM16-UCS model.

The default is CSP_CAM16_VC_SRGB_D65. The ring is made again at the next draw; call xtk_scheme_wheel_draw() after it.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
vc The viewing conditions.

xtk_scheme_wheel_set_white_point

void xtk_scheme_wheel_set_white_point(
    XtkSchemeWheel *scheme_wheel,
    const CSP_WhitePoint *white_point);

Sets the white point for the LCh(ab), LCh(uv), Lab, OKLCh, OKLab and JzCzhz models.

The wheel keeps the pointer, so the white point must stay valid (the predefined ones of xtk_color_space.h do). The ring is made again at the next draw; call xtk_scheme_wheel_draw() after it.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
white_point The white point, or NULL for D65, the default.

xtk_scheme_wheel_set_alpha

void xtk_scheme_wheel_set_alpha(XtkSchemeWheel *scheme_wheel, unsigned int alpha);

Sets the opacity of the triangle, to show a color with alpha.

Below 255 the triangle is blended over a checkerboard. It does not redraw; call xtk_scheme_wheel_draw() after it.

Parameter Description
scheme_wheel The wheel. NULL does nothing.
alpha 0 (transparent) to 255 (opaque, the default); larger values are taken as 255.

xtk_scheme_wheel_hsv_to_rgb8

void xtk_scheme_wheel_hsv_to_rgb8(
    const XtkSchemeWheel *scheme_wheel,
    CSP_WheelModel model,
    double h,
    double s,
    double v,
    uint8_t *r8,
    uint8_t *g8,
    uint8_t *b8);

Converts a model's hue, saturation and value to an 8-bit sRGB color.

The three values are scaled as the triangle scales them: for the perceptual models h is the hue angle divided by 360, s the chroma divided by the model's largest sRGB chroma (134 for LCh(ab) and Lab, 179 for LCh(uv), 0.322 for OKLCh and OKLab, 0.34 for JzCzhz, 0.92 for CAM16-UCS) and v the lightness divided by its largest value. For the RYB models h is the place on the RYB ring. For the other models they are plain HSV, read in that model's RGB space for the wide-gamut ones. A color outside sRGB is clipped.

Parameter Description
scheme_wheel The wheel whose white point and CAM16 viewing conditions apply; NULL uses D65 and CSP_CAM16_VC_SRGB_D65.
model The model h, s and v are in.
h The hue, from 0 to 1 (for the RYB models, the place on the RYB ring, not the RGB hue).
s The saturation or chroma, from 0 to 1.
v The value or lightness, from 0 to 1.
r8 Receives red, 0 to 255. When it is NULL nothing is written.
g8 Receives green, 0 to 255; may be NULL.
b8 Receives blue, 0 to 255; may be NULL.

xtk_scheme_wheel_rgb8_to_hsv

void xtk_scheme_wheel_rgb8_to_hsv(
    const XtkSchemeWheel *scheme_wheel,
    CSP_WheelModel model,
    uint8_t r8,
    uint8_t g8,
    uint8_t b8,
    double *h,
    double *s,
    double *v);

Converts an 8-bit sRGB color to a model's hue, saturation and value.

This is the inverse of xtk_scheme_wheel_hsv_to_rgb8(), with the same scaling, for the RGB, HSV, HSL, RYB, perceptual and wide-gamut models. The results are clamped to 0 to 1. Use it to place the wheel at a known color: give s and v to xtk_scheme_wheel_set_triangle_sv(), and h to xtk_scheme_wheel_set_marker_hue(). For the RYB models h is the place on the RYB ring, so turn it into an RGB hue first with xtk_color_space_wheel_wheelhue_to_rgbhue().

Parameter Description
scheme_wheel The wheel whose white point and CAM16 viewing conditions apply; NULL uses D65 and CSP_CAM16_VC_SRGB_D65.
model The model to convert into. The wide-gamut RGB models give the HSV of the color in that space; the linear and XYZ models give plain sRGB HSV.
r8 Red, 0 to 255.
g8 Green, 0 to 255.
b8 Blue, 0 to 255.
h Receives the hue, from 0 to 1 (for the RYB models, the place on the RYB ring). When h, s or v is NULL nothing is written.
s Receives the saturation or chroma, from 0 to 1.
v Receives the value or lightness, from 0 to 1.