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

xtk_paint.h

Gradient fills and bevel edges that a theme can give a widget.

In a theme or config file, a color key can hold a CSS gradient instead of a color, and a widget section can have a bevel: a light line inside the top edge (highlight) and a dark line inside the bottom edge (shade), like an inset box shadow.

[button]
background = linear-gradient(180deg, #4A4A50, #33333A)
highlight = #FFFFFF30
shade = #00000060
 * 

The paint table. xtk_config_set() keeps each gradient in a table, by section and key, and gives the widget's color field the gradient's average color. A widget that does not draw gradients therefore still gets a fitting flat color. A widget that draws them asks xtk_paint_for() at draw time and fills through xtk_paint_fill_rect(), xtk_paint_fill_rounded_rect() or xtk_paint_fill_bordered_rect(). The bevel colors are only kept in the table (no widget field holds them); a widget draws them with xtk_paint_draw_bevel().

The table belongs to the theme: xtk_theme_apply() empties it before it sets the theme's keys. A widget that was given colors of its own instead of the theme's keeps drawing them flat, because xtk_paint_for() only gives the gradient while the widget's color is still the gradient's average.

Gradient syntax. linear-gradient(...) and radial-gradient(...), as in CSS:

  • The first argument of a linear gradient may be an angle (180deg, 0.5turn, 3.14rad) or a direction (to bottom, to top right). The default is 180 degrees, top to bottom.
  • Then up to eight color stops, each a color and an optional position in percent (#FF0000 30%). Stops without a position are spread evenly between their neighbors.
  • Colors: #rgb, #rgba, #rrggbb, #rrggbbaa, rgb(), rgba(), and the names white, black and transparent.

Drawing. xtk_paint_begin() renders the gradient into a tile pixmap the size of the box and sets it on a GC, so any fill drawn with that GC (a rounded rectangle, a polygon) shows the gradient, clipped to its shape. Tiles are cached, as widgets often repaint at the same size.

Example. A widget draws its background:

const XtkPaint *paint = xtk_paint_for("button", "background", config.button.background);
XSetForeground(display, gc, xtk_paint_pixel(display, screen, config.button.background));
xtk_paint_fill_rounded_rect(display, screen, target, gc, paint, 0, 0, width, height, 4);
xtk_paint_draw_bevel(display, screen, target, gc, "button", 0, 0, width, height, 4,
                     1, config.button.background);
 * 

Limitations.

  • A radial gradient is always an ellipse centered in the box, reaching its corners; a shape, size or position given in the CSS is ignored. A color space (in oklch) is ignored too; colors are mixed in sRGB.
  • Stop positions other than percentages (for example 10px) are not read; a stop that cannot be read is skipped.
  • The table holds at most 256 gradients, and bevels for at most 64 sections. In the table, section names longer than 31 characters and keys longer than 47 are cut.
#include "xtk_paint.h"

Types: XtkPaintKind, XtkPaint

Constants: XTK_PAINT_MAX_STOPS

Functions: xtk_paint_is_gradient, xtk_paint_parse, xtk_paint_parse_color, xtk_paint_average, xtk_paint_format, xtk_paint_format_color, xtk_paint_color_at, xtk_paint_table_clear, xtk_paint_table_set, xtk_paint_table_set_bevel, xtk_paint_for, xtk_paint_bevel_for, xtk_paint_table_entry, xtk_paint_begin, xtk_paint_end, xtk_paint_fill_rounded_rect, xtk_paint_fill_bordered_rect, xtk_paint_fill_rect, xtk_paint_draw_bevel, xtk_paint_window_background, xtk_paint_pixel

Types

XtkPaintKind

What a paint fills with.

Name Value Description
XTK_PAINT_FLAT 0 One color.
XTK_PAINT_LINEAR A linear gradient.
XTK_PAINT_RADIAL A radial gradient.

XtkPaint

A flat color or a gradient, as xtk_paint_parse() reads it.

Field Type Description
kind XtkPaintKind Flat, linear or radial.
angle double Linear only: the direction in CSS degrees (0 = to top, 90 = to right, 180 = to bottom)
n int The number of stops used, 2 to XTK_PAINT_MAX_STOPS.
position[XTK_PAINT_MAX_STOPS] double Each stop's position along the gradient, 0 to 1, in rising order.
color[XTK_PAINT_MAX_STOPS] ConfigColor Each stop's color, with alpha.

Constants

XTK_PAINT_MAX_STOPS

#define XTK_PAINT_MAX_STOPS 8

The most color stops a gradient can have.

Functions

xtk_paint_is_gradient

int xtk_paint_is_gradient(const char *value);

Whether a value is a CSS gradient.

Parameter Description
value The text to check (leading blanks are skipped)

Returns: 1 if it starts with linear-gradient( or radial-gradient(, 0 otherwise (also when value is NULL)

xtk_paint_parse

int xtk_paint_parse(const char *value, XtkPaint *out);

Read a CSS gradient.

See the syntax above. A gradient with one stop becomes two stops of the same color. Missing positions are filled in: the first stop at 0, the last at 1, the rest evenly between; a position lower than the one before it is raised to that one.

Parameter Description
value The gradient's CSS text
out Receives the gradient

Returns: 1 on success; 0 if value is not a gradient, no stop could be read, or out is NULL

xtk_paint_parse_color

int xtk_paint_parse_color(const char *value, ConfigColor *out);

Read a color as a gradient stop would be written, alpha included.

Accepts #rgb, #rgba, #rrggbb, #rrggbbaa, rgb() and rgba() (with numbers 0 to 255 or percentages, and an alpha 0 to 1 or a percentage), and the names white, black and transparent.

Parameter Description
value The color's text
out Receives the color

Returns: 1 on success, 0 if the text is not a color (out is then unchanged)

xtk_paint_average

ConfigColor xtk_paint_average(const XtkPaint *paint);

A gradient's average color: what a widget that draws flat shows.

Rounded to 8 bits per channel, as a config file writes a color, so a color read back from a file still matches it.

Parameter Description
paint The gradient

Returns: The average color; opaque black if paint is NULL or has no stops

xtk_paint_format

void xtk_paint_format(const XtkPaint *paint, char *out, size_t size);

Write a gradient as CSS text.

A linear gradient is written with its angle in degrees; every stop is written with its position in percent.

Parameter Description
paint The gradient
out Receives the text; an empty string if paint is NULL or has no stops
size The size of out in bytes

xtk_paint_format_color

void xtk_paint_format_color(ConfigColor color, char *out, size_t size);

Write a color as #RRGGBB, or #RRGGBBAA when it is not opaque.

Parameter Description
color The color
out Receives the text (at least 10 bytes are needed)
size The size of out in bytes

xtk_paint_color_at

ConfigColor xtk_paint_color_at(const XtkPaint *paint, double t);

A gradient's color at a point along it.

Parameter Description
paint The gradient
t Where along it, from 0 (the start) to 1 (the end); values outside give the end colors

Returns: The color there, mixed between the two stops around t; opaque black if paint is NULL or has no stops

xtk_paint_table_clear

void xtk_paint_table_clear(void);

Empty the paint table: remove every gradient and bevel. xtk_theme_apply() calls this.

xtk_paint_table_set

void xtk_paint_table_set(const char *section, const char *key, const XtkPaint *paint);

Set or remove the gradient of a section's key.

xtk_config_set() calls this for each gradient value it reads. When the table is full, a new entry is not added.

Parameter Description
section The config section, for example "button"
key The key in it, for example "background"
paint The gradient (it is copied), or NULL to remove the entry

xtk_paint_table_set_bevel

void xtk_paint_table_set_bevel(
    const char *section,
    const char *which,
    const ConfigColor *color);

Set or remove one of a section's bevel colors.

Parameter Description
section The config section
which "highlight" (the line inside the top edge) or "shade" (the line inside the bottom edge); anything else is ignored
color The color, with alpha (it is copied), or NULL to remove it

xtk_paint_for

const XtkPaint *xtk_paint_for(const char *section, const char *key, ConfigColor flat);

The gradient a widget should draw for one of its keys, if any.

Gives the gradient only while the widget's own color is still the gradient's average, that is, while the widget shows the theme's color and not one it was given separately. Alpha is not compared.

Parameter Description
section The config section
key The key in it
flat The color the widget would draw flat (the field in its block)

Returns: The gradient, owned by the table and valid until the table changes; or NULL to draw flat

xtk_paint_bevel_for

int xtk_paint_bevel_for(const char *section, ConfigColor *highlight, ConfigColor *shade);

A section's bevel colors.

Parameter Description
section The config section
highlight Receives the highlight color when the section has a bevel entry; may be NULL
shade Receives the shade color when the section has a bevel entry; may be NULL

Returns: Which colors are set: 1 the highlight, 2 the shade, 3 both, 0 neither

xtk_paint_table_entry

int xtk_paint_table_entry(
    int n,
    const char **section,
    const char **key,
    const XtkPaint **paint);

The table's gradients one by one, for listing them (in a theme editor, say).

Parameter Description
n Which entry, from 0
section Receives the entry's section; may be NULL
key Receives its key; may be NULL
paint Receives its gradient; may be NULL

Returns: 1 if there is an n-th entry, 0 past the end

xtk_paint_begin

int xtk_paint_begin(
    Display *display,
    int screen,
    Drawable drawable,
    GC gc,
    const XtkPaint *paint,
    int x,
    int y,
    int w,
    int h);

Set a gradient on a GC, for the fills of one box.

Renders the gradient into a tile the size of the box (or takes it from the cache) and sets the GC to tile with it from the box's corner. Every fill drawn with the GC then shows the gradient, clipped to the fill's shape. Call xtk_paint_end() after the fills.

Parameter Description
display The X connection
screen The screen number
drawable Where the fill will draw
gc The GC the fill uses
paint The gradient
x The box's left edge
y The box's top edge
w The box's width
h The box's height

Returns: 1 when the GC is set to tile, and xtk_paint_end() must be called after the fill. 0 when paint is NULL or flat, or the box is empty (the GC keeps its color), or the tile could not be made (the GC's foreground is set to the gradient's average color).

xtk_paint_end

void xtk_paint_end(Display *display, GC gc);

Finish fills begun with xtk_paint_begin(): set the GC back to solid fills.

Sets the fill style to FillSolid and the tile origin to 0, 0.

Parameter Description
display The X connection
gc The GC given to xtk_paint_begin()

xtk_paint_fill_rounded_rect

void xtk_paint_fill_rounded_rect(
    Display *display,
    int screen,
    Drawable drawable,
    GC gc,
    const XtkPaint *paint,
    int x,
    int y,
    int w,
    int h,
    int radius);

Fill a rounded rectangle with a paint.

With a gradient, the rectangle shows it; with NULL or a flat paint, it is filled in the GC's current foreground color, so set that first.

Parameter Description
display The X connection
screen The screen number
drawable Where to draw
gc The GC to draw with
paint The paint, or NULL
x The left edge
y The top edge
w The width
h The height
radius The corner radius (see xtk_fill_rounded_rect() in xtk_draw.h)

xtk_paint_fill_bordered_rect

void xtk_paint_fill_bordered_rect(
    Display *display,
    int screen,
    Drawable drawable,
    GC gc,
    int x,
    int y,
    int w,
    int h,
    int radius,
    int border_width,
    unsigned long border_pixel,
    unsigned long fill_pixel,
    const XtkPaint *fill);

Draw a bordered rounded rectangle whose inside may be a gradient.

The same as xtk_fill_bordered_rect() in xtk_draw.h, except that the inside is filled with fill when that is a gradient. The GC's foreground is changed.

Parameter Description
display The X connection
screen The screen number
drawable Where to draw
gc The GC to draw with
x The left edge
y The top edge
w The width
h The height
radius The outer corner radius
border_width The border's width; 0 for no border
border_pixel The border's pixel value
fill_pixel The inside's pixel value, used when fill is NULL or flat
fill The inside's paint, or NULL

xtk_paint_fill_rect

void xtk_paint_fill_rect(
    Display *display,
    int screen,
    Drawable drawable,
    GC gc,
    const XtkPaint *paint,
    int x,
    int y,
    int w,
    int h);

Fill a rectangle with a paint.

With NULL or a flat paint, the rectangle is filled in the GC's current foreground color. Nothing is drawn when w or h is less than 1.

Parameter Description
display The X connection
screen The screen number
drawable Where to draw
gc The GC to draw with
paint The paint, or NULL
x The left edge
y The top edge
w The width
h The height

xtk_paint_draw_bevel

void xtk_paint_draw_bevel(
    Display *display,
    int screen,
    Drawable drawable,
    GC gc,
    const char *section,
    int x,
    int y,
    int w,
    int h,
    int radius,
    int border_width,
    ConfigColor under);

Draw a section's bevel inside a box's border, if the theme gives it one.

Draws a one-pixel highlight line just inside the top border and a shade line just inside the bottom border, kept clear of the rounded corners. A bevel color with alpha is mixed over under. Draws nothing when the section has no bevel or the box is too small. The GC's foreground and line attributes are changed.

Parameter Description
display The X connection
screen The screen number
drawable Where to draw
gc The GC to draw with
section The config section whose bevel is drawn, for example "button"
x The box's left edge
y The box's top edge
w The box's width
h The box's height
radius The box's corner radius
border_width The box's border width
under The color beneath the lines (usually the box's fill), for mixing a bevel color that has alpha

xtk_paint_window_background

void xtk_paint_window_background(
    Display *display,
    int screen,
    Window window,
    const XtkPaint *paint,
    unsigned long pixel,
    int w,
    int h);

Give a window a paint as its background.

The window background is what the server shows behind the window's children, and through a child whose background is ParentRelative. Call it again when the window's size changes. The window is cleared and gets an Expose event.

Parameter Description
display The X connection
screen The screen number
window The window
paint The gradient, or NULL or a flat paint to use pixel
pixel The background pixel value when paint is NULL or flat
w The background's width (the window's width)
h The background's height (the window's height)

xtk_paint_pixel

unsigned long xtk_paint_pixel(Display *display, int screen, ConfigColor color);

A color's pixel value for the screen's visual.

On a TrueColor or DirectColor visual it is computed from the color; otherwise it comes from xtk_config_color_to_pixel(). Alpha is ignored.

Parameter Description
display The X connection
screen The screen number
color The color

Returns: The pixel value