Table of Contents
- xtk_gradient.h
- Types
- Constants
- Functions
- xtk_gradient_init
- xtk_gradient_add_stop
- xtk_gradient_remove_stop
- xtk_gradient_sort_stops
- xtk_gradient_evaluate_at
- xtk_gradient_evaluate_at_with_interpolation
- xtk_gradient_render_strip
- xtk_gradient_render_strip_with_interpolation
- xtk_gradient_render_angled
- xtk_gradient_render_radial
- xtk_gradient_render_radial_bar
- xtk_gradient_to_css
- xtk_gradient_to_css_legacy
- xtk_gradient_to_stops
- xtk_gradient_to_svg
- xtk_gradient_to_glsl
- xtk_gradient_to_hlsl
- xtk_gradient_to_unity_cs
- xtk_gradient_to_godot_gd
- xtk_gradient_to_json
- xtk_gradient_to_javascript
- xtk_gradient_to_android_xml
- xtk_gradient_to_ios_swift
- xtk_gradient_to_gimp_ggr
- xtk_gradient_interpolation_is_cylindrical
- xtk_gradient_interpolation_label
- xtk_gradient_interpolation_css_name
xtk_gradient.h
A color gradient: its stops, its colors at any point, pixel previews and text exports.
An XtkGradient holds up to 32 color stops, each with a position, an sRGB color with alpha, an easing curve and a midpoint for the stretch to the next stop. It is plain data: the program owns the struct (on the stack or inside its own data) and changes its fields directly or with the functions here. There is no window and nothing to free.
Color between stops. The color is blended in a color space chosen with the interpolation field (any CSP_WheelModel of xtk_color_space.h). Spaces with a hue (OKLCh, LCh(ab), LCh(uv), JzCzhz, CAM16-UCS, HSL, HSV) go round the hue circle the way hue_method says, as CSS does. Alpha is always blended linearly. xtk_gradient_evaluate_at() gives the color at any point.
Pixel previews. The render functions fill a buffer of packed 0x00RRGGBB pixels, row by row, for a preview: a strip, a rectangle at an angle, a radial disc, and a "radial bar". Transparent parts are blended over an 8 pixel checkerboard of light grays. The caller owns the buffer, which must hold width * height values.
Exports. xtk_gradient_to_css() writes a CSS Color 4 gradient that uses the same interpolation space when CSS has it; other spaces give 64 sRGB samples. The other export functions write the stops for CSS 3, SVG, GLSL, HLSL, Unity, Godot, JSON, JavaScript, Android, iOS and GIMP. Most of them write the stop colors as they are, for a target that blends in sRGB, so the interpolation space, easing and midpoints are lost. Each export writes a NUL-ended text into the caller's buffer, which should be XTK_GRADIENT_CSS_MAX bytes; text that does not fit is cut short. The buffer is always NUL-ended, also when nothing is written into it (a gradient with no stops); a NULL buffer, or a size of 0 or less, writes nothing.
Example.
XtkGradient gradient;
xtk_gradient_init(&gradient); // coral to sky blue, in OKLCh
xtk_gradient_add_stop(&gradient, 0.5, 255, 220, 0, 255); // yellow in the middle
gradient.angle = 45.0;
unsigned int pixels[256 * 32];
xtk_gradient_render_strip(&gradient, pixels, 256, 32);
char css[XTK_GRADIENT_CSS_MAX];
xtk_gradient_to_css(&gradient, css, sizeof(css));
// css: "linear-gradient(45deg in oklch shorter hue, oklch(...) 0%, ...)"
*
Limitations.
- In the Display P3, Adobe RGB and Rec.2020 spaces the stops' sRGB colors are turned into that space's 8-bit values, mixed there, and turned back, so a blend starts and ends at the stop colors to within one step of 8 bits. The RYB spaces blend as plain sRGB.
xtk_gradient_render_radial()always centers the disc; it ignorescxandcy. The CSS export of a radial gradient does not write its center or size either.
#include "xtk_gradient.h"
Types: XtkGradientEasing, XtkGradientHueMethod, XtkGradientType, XtkGradientStop, XtkGradient
Constants: XTK_GRADIENT_MAX_STOPS, XTK_GRADIENT_CSS_MAX, XTK_GRADIENT_SAMPLE_COUNT
Functions: xtk_gradient_init, xtk_gradient_add_stop, xtk_gradient_remove_stop, xtk_gradient_sort_stops, xtk_gradient_evaluate_at, xtk_gradient_evaluate_at_with_interpolation, xtk_gradient_render_strip, xtk_gradient_render_strip_with_interpolation, xtk_gradient_render_angled, xtk_gradient_render_radial, xtk_gradient_render_radial_bar, xtk_gradient_to_css, xtk_gradient_to_css_legacy, xtk_gradient_to_stops, xtk_gradient_to_svg, xtk_gradient_to_glsl, xtk_gradient_to_hlsl, xtk_gradient_to_unity_cs, xtk_gradient_to_godot_gd, xtk_gradient_to_json, xtk_gradient_to_javascript, xtk_gradient_to_android_xml, xtk_gradient_to_ios_swift, xtk_gradient_to_gimp_ggr, xtk_gradient_interpolation_is_cylindrical, xtk_gradient_interpolation_label, xtk_gradient_interpolation_css_name
Types
XtkGradientEasing
How fast the color changes along the stretch from one stop to the next.
| Name | Value | Description |
|---|---|---|
XTK_GRADIENT_EASE_LINEAR |
0 |
At an even rate. |
XTK_GRADIENT_EASE_IN |
Slowly at the start, then faster (a cubic curve). | |
XTK_GRADIENT_EASE_OUT |
Fast at the start, then slowly (a cubic curve). | |
XTK_GRADIENT_EASE_IN_OUT |
Slowly at both ends and fast in the middle. |
XtkGradientHueMethod
Which way round the hue circle the color goes, in a space with a hue.
| Name | Value | Description |
|---|---|---|
XTK_GRADIENT_HUE_SHORTER |
0 |
The shorter way (CSS "shorter hue"); the default. |
XTK_GRADIENT_HUE_LONGER |
The longer way (CSS "longer hue"). | |
XTK_GRADIENT_HUE_CLOCKWISE |
Always toward smaller hue angles (CSS "decreasing hue"). | |
XTK_GRADIENT_HUE_COUNTERCLOCKWISE |
Always toward larger hue angles (CSS "increasing hue"). |
XtkGradientType
A gradient's shape.
| Name | Value | Description |
|---|---|---|
XTK_GRADIENT_TYPE_LINEAR |
0 |
Along a straight line at the gradient's angle. |
XTK_GRADIENT_TYPE_RADIAL |
Out from a center. |
XtkGradientStop
One color stop of a gradient.
| Field | Type | Description |
|---|---|---|
position |
double |
The place along the gradient, from 0 (start) to 1 (end). |
r |
uint8_t |
Red, 0 to 255, in sRGB. |
g |
uint8_t |
Green, 0 to 255, in sRGB. |
b |
uint8_t |
Blue, 0 to 255, in sRGB. |
a |
uint8_t |
Alpha, 0 (transparent) to 255 (opaque). |
easing |
XtkGradientEasing |
The easing of the stretch from this stop to the next one. |
midpoint |
double |
Where in the stretch to the next stop the two colors are mixed half and half, from 0 to 1; 0.5 is even, and less moves that point toward this stop. |
XtkGradient
A linear or radial gradient: its stops and how it blends between them.
Set it up with xtk_gradient_init(). The program may change any field; after it changes stop positions directly, it calls xtk_gradient_sort_stops(), because the other functions expect the stops in order.
| Field | Type | Description |
|---|---|---|
type |
XtkGradientType |
Linear or radial. |
angle |
double |
For a linear gradient: the direction in degrees, as in CSS (0 points up, 90 to the right). |
cx |
double |
For a radial gradient: the X of the center, from 0 to 1 across the area. Kept for the program; the render and export functions do not use it. |
cy |
double |
For a radial gradient: the Y of the center, from 0 to 1 down the area. Kept for the program; the render and export functions do not use it. |
interpolation |
CSP_WheelModel |
The color space the colors are blended in. |
hue_method |
XtkGradientHueMethod |
Which way round the hue goes, in a space with a hue. |
size |
double |
For a radial gradient: the radius in percent of the distance from the center to the nearest edge (100 reaches that edge). |
stops[XTK_GRADIENT_MAX_STOPS] |
XtkGradientStop |
The stops, sorted by position. |
stop_count |
int |
The number of stops in use. |
selection_stop |
int |
For a gradient editor: the selected stop, or -1 for none. The evaluate and render functions do not use it. |
selection_midpoint |
int |
For a gradient editor: the selected midpoint (the stretch after stop N), or -1 for none. |
Constants
XTK_GRADIENT_MAX_STOPS
#define XTK_GRADIENT_MAX_STOPS 32
The most stops a gradient can hold.
XTK_GRADIENT_CSS_MAX
#define XTK_GRADIENT_CSS_MAX 8192
The size an export buffer should have, in bytes, so that no export is cut short.
XTK_GRADIENT_SAMPLE_COUNT
#define XTK_GRADIENT_SAMPLE_COUNT 64
The number of sRGB samples xtk_gradient_to_css() writes for a space that CSS cannot blend in.
Functions
xtk_gradient_init
void xtk_gradient_init(XtkGradient *gradient);
Sets up a gradient with two stops: coral (EF476F) at 0 and sky blue (#06B6D4) at 1.
The gradient is linear at 90 degrees, blends in OKLCh the shorter way, has its center at (0.5, 0.5) and a size of 100. Stop 0 is selected and no midpoint is. All other fields are set to 0.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL does nothing. |
xtk_gradient_add_stop
int xtk_gradient_add_stop(
XtkGradient *gradient,
double position,
uint8_t r,
uint8_t gr,
uint8_t b,
uint8_t a);
Adds a stop and keeps the stops sorted by position.
The new stop has linear easing and a midpoint of 0.5.
| Parameter | Description |
|---|---|
gradient |
The gradient. |
position |
The place, from 0 to 1 (it is not clamped). |
r |
Red, 0 to 255. |
gr |
Green, 0 to 255. |
b |
Blue, 0 to 255. |
a |
Alpha, 0 to 255. |
Returns: The new stop's index after sorting; -1 when the gradient already has XTK_GRADIENT_MAX_STOPS stops or gradient is NULL.
xtk_gradient_remove_stop
void xtk_gradient_remove_stop(XtkGradient *gradient, int index);
Removes a stop.
A gradient keeps at least two stops: when it has two, nothing happens. The selection is moved back when it pointed past the new end.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL does nothing. |
index |
The stop's index. An index out of range does nothing. |
xtk_gradient_sort_stops
void xtk_gradient_sort_stops(XtkGradient *gradient);
Sorts the stops by position.
Call it after changing positions in the stops array directly. Stops with the same position keep their order. The selection indices are not changed.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL does nothing. |
xtk_gradient_evaluate_at
void xtk_gradient_evaluate_at(
const XtkGradient *gradient,
double t,
uint8_t *r_out,
uint8_t *g_out,
uint8_t *b_out,
uint8_t *a_out);
Gives the gradient's color at a point.
The color is blended in the gradient's own interpolation space with its hue_method, after the stretch's midpoint and easing are applied. Before the first stop the color is the first stop's, after the last stop the last one's. A color outside sRGB is clipped. With no stops the color is opaque black.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
t |
The place along the gradient, from 0 to 1. |
r_out |
Receives red, 0 to 255. None of the four may be NULL. |
g_out |
Receives green, 0 to 255. |
b_out |
Receives blue, 0 to 255. |
a_out |
Receives alpha, 0 to 255. |
xtk_gradient_evaluate_at_with_interpolation
void xtk_gradient_evaluate_at_with_interpolation(
const XtkGradient *gradient,
CSP_WheelModel interpolation,
XtkGradientHueMethod hue_method,
double t,
uint8_t *r_out,
uint8_t *g_out,
uint8_t *b_out,
uint8_t *a_out);
Gives the gradient's color at a point, blended in a given space.
This is xtk_gradient_evaluate_at() with another space and hue method than the gradient's own, for example to show the same stops blended in several spaces side by side.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
interpolation |
The color space to blend in. |
hue_method |
Which way round the hue goes, in a space with a hue. |
t |
The place along the gradient, from 0 to 1. |
r_out |
Receives red, 0 to 255. None of the four may be NULL. |
g_out |
Receives green, 0 to 255. |
b_out |
Receives blue, 0 to 255. |
a_out |
Receives alpha, 0 to 255. |
xtk_gradient_render_strip
void xtk_gradient_render_strip(
const XtkGradient *gradient,
unsigned int *buffer,
int width,
int height);
Draws the gradient from left to right into a pixel buffer.
The first column is the gradient's start and the last column its end; every row is the same. Transparent parts are blended over a checkerboard.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives width * height packed 0x00RRGGBB pixels, row by row. |
width |
The width in pixels. |
height |
The height in pixels. |
xtk_gradient_render_strip_with_interpolation
void xtk_gradient_render_strip_with_interpolation(
const XtkGradient *gradient,
CSP_WheelModel interpolation,
XtkGradientHueMethod hue_method,
unsigned int *buffer,
int width,
int height);
Draws the gradient from left to right, blended in a given space.
This is xtk_gradient_render_strip() with another space and hue method than the gradient's own.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
interpolation |
The color space to blend in. |
hue_method |
Which way round the hue goes, in a space with a hue. |
buffer |
Receives width * height packed 0x00RRGGBB pixels, row by row. |
width |
The width in pixels. |
height |
The height in pixels. |
xtk_gradient_render_angled
void xtk_gradient_render_angled(
const XtkGradient *gradient,
unsigned int *buffer,
int width,
int height,
double angle_degrees);
Draws the gradient as a linear gradient at an angle across a rectangle.
The angle works as in CSS linear-gradient(): 0 runs from the bottom to the top, 90 from left to right, and the gradient line is long enough that the corners get the end colors. To save time the color is computed once for each column (or row, for angles nearer the vertical), so diagonal angles are close but not exact.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives width * height packed 0x00RRGGBB pixels, row by row. |
width |
The width in pixels. |
height |
The height in pixels. |
angle_degrees |
The angle in degrees; the gradient's own angle field is not used. |
xtk_gradient_render_radial
void xtk_gradient_render_radial(
const XtkGradient *gradient,
unsigned int *buffer,
int width,
int height);
Draws the gradient as a circle from the center of a rectangle.
The gradient's start is at the center and its end at the gradient's size percent of the distance to the nearest edge; beyond that the last color continues.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives width * height packed 0x00RRGGBB pixels, row by row. |
width |
The width in pixels. |
height |
The height in pixels. |
xtk_gradient_render_radial_bar
void xtk_gradient_render_radial_bar(
const XtkGradient *gradient,
unsigned int *buffer,
int width,
int height);
Draws a radial gradient as a bar: the start in the middle and the end at both the left and the right edge.
Every row is the same, so a gradient editor can show a radial gradient in a wide, low bar and keep its stop handles meaningful.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives width * height packed 0x00RRGGBB pixels, row by row. |
width |
The width in pixels. |
height |
The height in pixels. |
xtk_gradient_to_css
int xtk_gradient_to_css(const XtkGradient *gradient, char *buffer, int buffer_size);
Writes the gradient as a CSS Color 4 linear-gradient() or radial-gradient().
When CSS can blend in the gradient's space (see xtk_gradient_interpolation_css_name()), the text names that space (and the hue method for a space with a hue), gives each stop in that space, writes a color hint for a midpoint that is not 0.5, and adds 7 samples for a stretch with easing. Otherwise it starts with a comment and gives 64 sRGB samples of the gradient.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives the text, ended with a NUL byte. |
buffer_size |
The size of buffer; XTK_GRADIENT_CSS_MAX is always enough. |
Returns: 1 when CSS blends in the same space; 0 when sRGB samples were written instead, or gradient is NULL.
xtk_gradient_to_css_legacy
void xtk_gradient_to_css_legacy(
const XtkGradient *gradient,
char *buffer,
int buffer_size);
Writes the gradient as a CSS 3 gradient with #rrggbb (or #rrggbbaa) stops.
This form works in older browsers; it blends in sRGB.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives the text, ended with a NUL byte. |
buffer_size |
The size of buffer; XTK_GRADIENT_CSS_MAX is always enough. |
xtk_gradient_to_stops
void xtk_gradient_to_stops(const XtkGradient *gradient, char *buffer, int buffer_size);
Writes the stops as a list, one #rrggbb position% line for each.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives the text, ended with a NUL byte. A gradient with no stops writes "". |
buffer_size |
The size of buffer; XTK_GRADIENT_CSS_MAX is always enough. |
xtk_gradient_to_svg
void xtk_gradient_to_svg(const XtkGradient *gradient, char *buffer, int buffer_size);
Writes the gradient as an SVG linearGradient element with the id "grad".
The angle becomes a gradientTransform rotation about the center. A radial gradient is also written as a linear one.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives the text, ended with a NUL byte. |
buffer_size |
The size of buffer; XTK_GRADIENT_CSS_MAX is always enough. |
xtk_gradient_to_glsl
void xtk_gradient_to_glsl(const XtkGradient *gradient, char *buffer, int buffer_size);
Writes the gradient as GLSL: arrays of colors and positions and a sampleGradient() function.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives the text, ended with a NUL byte. |
buffer_size |
The size of buffer; XTK_GRADIENT_CSS_MAX is always enough. |
xtk_gradient_to_hlsl
void xtk_gradient_to_hlsl(const XtkGradient *gradient, char *buffer, int buffer_size);
Writes the gradient as HLSL: arrays of colors and positions and a SampleGradient() function.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives the text, ended with a NUL byte. |
buffer_size |
The size of buffer; XTK_GRADIENT_CSS_MAX is always enough. |
xtk_gradient_to_unity_cs
void xtk_gradient_to_unity_cs(const XtkGradient *gradient, char *buffer, int buffer_size);
Writes C# code that builds a Unity Gradient with color and alpha keys.
Unity takes at most 8 keys: a gradient with more stops is written as 8 evenly spaced samples, with a comment that says so.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives the text, ended with a NUL byte. |
buffer_size |
The size of buffer; XTK_GRADIENT_CSS_MAX is always enough. |
xtk_gradient_to_godot_gd
void xtk_gradient_to_godot_gd(const XtkGradient *gradient, char *buffer, int buffer_size);
Writes GDScript code that builds a Godot 4 Gradient.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives the text, ended with a NUL byte. |
buffer_size |
The size of buffer; XTK_GRADIENT_CSS_MAX is always enough. |
xtk_gradient_to_json
void xtk_gradient_to_json(const XtkGradient *gradient, char *buffer, int buffer_size);
Writes the gradient as a JSON object with its type, angle and stops.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives the text, ended with a NUL byte. |
buffer_size |
The size of buffer; XTK_GRADIENT_CSS_MAX is always enough. |
xtk_gradient_to_javascript
void xtk_gradient_to_javascript(
const XtkGradient *gradient,
char *buffer,
int buffer_size);
Writes the gradient as a JavaScript const gradient = {...}; with its type, angle and stops.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives the text, ended with a NUL byte. |
buffer_size |
The size of buffer; XTK_GRADIENT_CSS_MAX is always enough. |
xtk_gradient_to_android_xml
void xtk_gradient_to_android_xml(
const XtkGradient *gradient,
char *buffer,
int buffer_size);
Writes the gradient as an Android drawable shape with a linear gradient.
Android takes only a start, center and end color, so these are the gradient's colors at 0, 0.5 and 1. The angle is rounded to a multiple of 45 degrees, as Android needs.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives the text, ended with a NUL byte. |
buffer_size |
The size of buffer; XTK_GRADIENT_CSS_MAX is always enough. |
xtk_gradient_to_ios_swift
void xtk_gradient_to_ios_swift(
const XtkGradient *gradient,
char *buffer,
int buffer_size);
Writes Swift code that sets up an iOS CAGradientLayer, with start and end points from the angle.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives the text, ended with a NUL byte. |
buffer_size |
The size of buffer; XTK_GRADIENT_CSS_MAX is always enough. |
xtk_gradient_to_gimp_ggr
void xtk_gradient_to_gimp_ggr(const XtkGradient *gradient, char *buffer, int buffer_size);
Writes the gradient as the text of a GIMP gradient (.ggr) file.
| Parameter | Description |
|---|---|
gradient |
The gradient. NULL writes nothing. |
buffer |
Receives the text, ended with a NUL byte. |
buffer_size |
The size of buffer; XTK_GRADIENT_CSS_MAX is always enough. |
xtk_gradient_interpolation_is_cylindrical
int xtk_gradient_interpolation_is_cylindrical(CSP_WheelModel interpolation);
Tells if blending in a space goes round a hue, so that hue_method matters.
| Parameter | Description |
|---|---|
interpolation |
The color space. |
Returns: 1 for OKLCh, LCh(ab), LCh(uv), JzCzhz, CAM16-UCS, HSL and HSV; 0 for every other space.
xtk_gradient_interpolation_label
const char *xtk_gradient_interpolation_label(CSP_WheelModel interpolation);
Gives a short name of a color space to show to the user.
For example "sRGB", "OKLCh" or "Display P3".
| Parameter | Description |
|---|---|
interpolation |
The color space. |
Returns: The name, a static string; "Unknown" for a value that is not a space.
xtk_gradient_interpolation_css_name
const char *xtk_gradient_interpolation_css_name(CSP_WheelModel interpolation);
Gives the name CSS uses for a color space in a gradient, such as "oklch".
| Parameter | Description |
|---|---|
interpolation |
The color space. |
Returns: The CSS name, a static string; NULL when CSS cannot blend in that space (RYB, HSV, LCh(uv), JzCzhz, CAM16-UCS, ACEScg).
xtk API
Widgets
- button
- checkbox
- radiobutton
- label
- entry
- textview
- edit_history
- spinbutton
- slider
- progressbar
- combobox
- scrollbar
- swatch
- panel
- statusbar
- tooltip
- toolbar
- icongrid
- iconview
- pathbar
- treeview
- datagrid
- graph
- scheme_wheel
- zoom
- tray
Containers and menus
Dialogs
Rich text
Drawing and images
Color
Configuration and themes
Input and the X server
Safety
Data formats