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

xtk_zoom.h

A screen magnifier that picks the color of a pixel anywhere on the screen.

The zoom widget is a window that shows the screen around the pointer, magnified (20 times at first), with a crosshair and a small square that mark the pixel under the pointer, and a "#RRGGBB" readout of that pixel's color in a corner. It is the eyedropper of a color picker: the user starts a pick, moves over the screen, and clicks the pixel whose color they want. After the pick the window keeps showing the area around the picked pixel.

The idea of the magnifier comes from xzoom (Itai Nahshon) and that of clicking a pixel to read its color from grabc (Muhammad Muquit); no code of either is used. See CREDITS.md.

Making one. xtk_zoom_create() takes the parent window, the position and the size, and maps the window at once. It has its own defaults for every color and option; it does not read an XtkConfig. Free it with xtk_zoom_destroy().

A pick.

  1. xtk_zoom_begin_selection() (or Ctrl+Alt+Z, see below) starts a pick session. It grabs the pointer and the keyboard, so the program's other widgets get no pointer or key events until it ends. While xtk_zoom_is_active() is 1, give the zoom widget the pointer and key events first.
  2. Moving the pointer magnifies the area under it. The arrow keys move the pointer by one pixel. The wheel and the + (or =) and - keys change the magnification by 10, from 10 to 300.
  3. A left click, or Return or keypad Enter, picks the pixel under the pointer and ends the session. A right click or Escape ends it without a pick. Clicks and Enter inside an exclusion zone (xtk_zoom_add_exclusion_zone()) are ignored.
  4. After each event, the program checks xtk_zoom_color_picked() (a pick ended the session) and reads the color with xtk_zoom_get_last_pixel(), and checks xtk_zoom_was_cancelled() (the session ended without one).

Events. Pass every X event to xtk_zoom_handle_event(). It draws the window on Expose. A KeyPress with Ctrl+Alt+Z starts a pick session and fires the activation callback, whatever window the key event is for, so the shortcut works wherever the program passes key events to the widget. Outside a pick session the widget ignores the other pointer and key events (it returns 0 for them).

Config keys. The widget reads no config section itself. A program keeps these settings in its XtkConfig, where xtk_config_init_defaults() gives them the same defaults as the widget, and passes them on:

Example.

XtkZoom *zoom = xtk_zoom_create(display, window, 10, 10, XTK_ZOOM_WIDTH, XTK_ZOOM_HEIGHT);
xtk_zoom_begin_selection(zoom);

XEvent event;
while (xtk_zoom_is_active(zoom)) {
    XNextEvent(display, &event);
    xtk_zoom_handle_event(zoom, &event);
}
if (xtk_zoom_color_picked(zoom))
    printf("picked #%06lX\n", xtk_zoom_get_last_pixel(zoom) & 0xFFFFFF);
 * 

Limitations.

  • The picked color is an X pixel value, which is 0xRRGGBB only on a TrueColor display of depth 24 or 32; the widget does not work on other visuals.
  • A saved image is raw data in the byte order of the machine that saved it.
#include "xtk_zoom.h"

Types: XtkZoomHexPosition, XtkZoom, ZoomActivationCallback

Constants: XTK_ZOOM_WIDTH, XTK_ZOOM_HEIGHT, XTK_ZOOM_MAGNIFICATION, XTK_ZOOM_MAGNIFICATION_MIN, XTK_ZOOM_MAGNIFICATION_MAX, XTK_ZOOM_MAGNIFICATION_STEP

Functions: xtk_zoom_create, xtk_zoom_destroy, xtk_zoom_set_position, xtk_zoom_set_size, xtk_zoom_get_window, xtk_zoom_handle_event, xtk_zoom_begin_selection, xtk_zoom_cancel_selection, xtk_zoom_get_last_pixel, xtk_zoom_color_picked, xtk_zoom_was_cancelled, xtk_zoom_is_active, xtk_zoom_get_magnification, xtk_zoom_set_magnification, xtk_zoom_set_colors, xtk_zoom_set_visibility, xtk_zoom_save_image, xtk_zoom_load_image, xtk_zoom_clear_image, xtk_zoom_draw, xtk_zoom_set_activation_callback, xtk_zoom_add_exclusion_zone, xtk_zoom_clear_exclusion_zones, xtk_zoom_update_exclusion_zone, xtk_zoom_set_follow_cursor, xtk_zoom_set_hex_readout, xtk_zoom_set_hex_label_style, xtk_zoom_set_hex_border_style, xtk_zoom_set_hex_position, xtk_zoom_set_hex_value, xtk_zoom_regrab_input

Types

XtkZoomHexPosition

The corner of the zoom window the hex readout is drawn in.

Name Value Description
XTK_ZOOM_HEX_POSITION_BOTTOM_RIGHT 0 The bottom right corner (the default).
XTK_ZOOM_HEX_POSITION_BOTTOM_LEFT The bottom left corner.
XTK_ZOOM_HEX_POSITION_TOP_RIGHT The top right corner.
XTK_ZOOM_HEX_POSITION_TOP_LEFT The top left corner.

XtkZoom

typedef struct XtkZoom XtkZoom;

A zoom widget (opaque). Made by xtk_zoom_create().

ZoomActivationCallback

typedef void (*ZoomActivationCallback)(XtkZoom *zoom, void *user_data);

Called when Ctrl+Alt+Z starts a pick session.

The session has already begun when it is called; the program can update its own controls, such as a pick button.

Constants

XTK_ZOOM_WIDTH

#define XTK_ZOOM_WIDTH 300

A good default width for the zoom window, in pixels.

XTK_ZOOM_HEIGHT

#define XTK_ZOOM_HEIGHT 300

A good default height for the zoom window, in pixels.

XTK_ZOOM_MAGNIFICATION

#define XTK_ZOOM_MAGNIFICATION 20

The magnification a new zoom widget starts with.

XTK_ZOOM_MAGNIFICATION_MIN

#define XTK_ZOOM_MAGNIFICATION_MIN 10

The smallest magnification (each screen pixel drawn 10 times larger).

XTK_ZOOM_MAGNIFICATION_MAX

#define XTK_ZOOM_MAGNIFICATION_MAX 300

The largest magnification.

XTK_ZOOM_MAGNIFICATION_STEP

#define XTK_ZOOM_MAGNIFICATION_STEP 10

How much the wheel and the + and - keys change the magnification.

Functions

xtk_zoom_create

XtkZoom *xtk_zoom_create(
    Display *display,
    Window parent,
    int x,
    int y,
    int width,
    int height);

Creates a zoom widget and maps its window.

The window is black until the first pick session, or until an image is loaded.

Parameter Description
display The X display. The widget works on its default screen.
parent The parent window.
x The x position in the parent.
y The y position in the parent.
width The width in pixels; it must be at least 1. Odd sizes work; the crosshair is then drawn between two pixel columns.
height The height in pixels; it must be at least 1.

Returns: The new zoom widget, or NULL when display is NULL or memory runs out.

xtk_zoom_destroy

void xtk_zoom_destroy(XtkZoom *zoom);

Destroys a zoom widget and its window.

Parameter Description
zoom The zoom widget. NULL is allowed and does nothing.

xtk_zoom_set_position

void xtk_zoom_set_position(XtkZoom *zoom, int x, int y);

Moves the zoom window within its parent.

Parameter Description
zoom The zoom widget.
x The new x position.
y The new y position.

xtk_zoom_set_size

void xtk_zoom_set_size(XtkZoom *zoom, int width, int height);

Resizes the zoom window.

What it shows is kept: the same pixels around the same center, magnified again for the new size. After a pick, the widget keeps a wider area around the picked pixel than it shows (enough for a 1200-pixel view at the lowest magnification), so a bigger window still shows real pixels; changing the magnification uses it the same way.

Parameter Description
zoom The zoom widget.
width The new width in pixels (at least 1 is used).
height The new height in pixels (at least 1 is used).

xtk_zoom_get_window

Window xtk_zoom_get_window(XtkZoom *zoom);

Gives the zoom widget's window.

Parameter Description
zoom The zoom widget, or NULL.

Returns: The window, or None when zoom is NULL.

xtk_zoom_handle_event

int xtk_zoom_handle_event(XtkZoom *zoom, XEvent *event);

Handles one X event.

See the top of this page for what each event does.

Parameter Description
zoom The zoom widget.
event The event.

Returns: 1 if the widget used the event (an Expose of its window, Ctrl+Alt+Z, or a pointer or key event during a pick session), 0 if not.

xtk_zoom_begin_selection

void xtk_zoom_begin_selection(XtkZoom *zoom);

Starts a pick session.

The pointer turns into a cross, the window magnifies the area under the pointer at once, and the pointer and the keyboard are grabbed. If either grab fails (another program holds one), the session does not start; xtk_zoom_is_active() then stays 0.

Parameter Description
zoom The zoom widget.

xtk_zoom_cancel_selection

void xtk_zoom_cancel_selection(XtkZoom *zoom);

Ends a pick session without a pick.

The grabs are released, the window shows the last area once more with the after-pick overlays (see xtk_zoom_set_visibility()), and xtk_zoom_was_cancelled() becomes 1. The widget ends the session the same way on a right click or Escape; after a pick it ends it too, but xtk_zoom_was_cancelled() stays 0.

Parameter Description
zoom The zoom widget.

xtk_zoom_get_last_pixel

unsigned long xtk_zoom_get_last_pixel(XtkZoom *zoom);

Gives the picked color, and clears the "picked" flag.

After this call xtk_zoom_color_picked() returns 0 until the next pick.

Parameter Description
zoom The zoom widget, or NULL.

Returns: The X pixel value of the last picked pixel (0xRRGGBB on a 24-bit TrueColor display), or 0 when zoom is NULL or nothing has been picked.

xtk_zoom_color_picked

int xtk_zoom_color_picked(XtkZoom *zoom);

Tells whether a color was picked and not yet read.

Parameter Description
zoom The zoom widget, or NULL.

Returns: 1 if a pixel was picked since the last xtk_zoom_get_last_pixel(), 0 if not or zoom is NULL.

xtk_zoom_was_cancelled

int xtk_zoom_was_cancelled(XtkZoom *zoom);

Tells whether a pick session was cancelled, and clears the flag.

The flag is set when a session ends without a pick: by a right click, Escape, or xtk_zoom_cancel_selection(). A session that ends with a pick clears it (see xtk_zoom_color_picked()).

Parameter Description
zoom The zoom widget, or NULL.

Returns: 1 if a session was cancelled since the last call, 0 if not or zoom is NULL.

xtk_zoom_is_active

int xtk_zoom_is_active(const XtkZoom *zoom);

Tells whether a pick session is under way.

The pointer and the keyboard are grabbed for the whole session, so the program's other widgets should not take pointer or key events while it is

  1. A program whose widgets claim every motion or button event of their own windows gives the zoom widget those events first while this is 1, or the live view stops following the pointer.
Parameter Description
zoom The zoom widget, or NULL.

Returns: 1 while a session is under way, 0 otherwise.

xtk_zoom_get_magnification

int xtk_zoom_get_magnification(const XtkZoom *zoom);

Gives the magnification.

Parameter Description
zoom The zoom widget, or NULL.

Returns: How many times larger each screen pixel is drawn, from XTK_ZOOM_MAGNIFICATION_MIN to XTK_ZOOM_MAGNIFICATION_MAX; XTK_ZOOM_MAGNIFICATION when zoom is NULL.

xtk_zoom_set_magnification

void xtk_zoom_set_magnification(XtkZoom *zoom, int magnification);

Sets the magnification, and redraws.

The view keeps its center.

Parameter Description
zoom The zoom widget.
magnification How many times larger each screen pixel is drawn. It is clamped to XTK_ZOOM_MAGNIFICATION_MIN to XTK_ZOOM_MAGNIFICATION_MAX.

xtk_zoom_set_colors

void xtk_zoom_set_colors(
    XtkZoom *zoom,
    unsigned long crosshair_rgb,
    unsigned long square_rgb);

Sets the colors of the crosshair and the square, and redraws.

Parameter Description
zoom The zoom widget.
crosshair_rgb The crosshair color as 0xRRGGBB, not an X pixel value (default 0x00C000); higher bits are ignored.
square_rgb The color of the square around the center pixel as 0xRRGGBB (default 0xC00000).

xtk_zoom_set_visibility

void xtk_zoom_set_visibility(
    XtkZoom *zoom,
    int crosshair_show,
    int square_show,
    int crosshair_show_after_pick,
    int square_show_after_pick);

Sets when the crosshair and the square are shown, and redraws.

The square is drawn only at a magnification above 2.

Parameter Description
zoom The zoom widget.
crosshair_show 1 to show the crosshair during a pick session (default 1).
square_show 1 to show the square during a pick session (default 1).
crosshair_show_after_pick 1 to keep the crosshair after the session (default 0).
square_show_after_pick 1 to keep the square after the session (default 0).

xtk_zoom_save_image

int xtk_zoom_save_image(XtkZoom *zoom, const char *filepath);

Saves what the zoom widget shows to a file.

It saves the screen pixels behind the view (the wider area kept at the last pick, if there is one), not their magnified copy, so xtk_zoom_load_image() can show them again at any size and magnification.

Parameter Description
zoom The zoom widget.
filepath The file to write.

Returns: 0 on success, -1 when the file cannot be written or an argument is NULL.

xtk_zoom_load_image

int xtk_zoom_load_image(XtkZoom *zoom, const char *filepath);

Loads a file saved by xtk_zoom_save_image(), and shows it.

The saved pixels are shown at the widget's current size and magnification, centered as they were. A file in the older format (the magnified view itself) loads only into a window of the size it was saved at.

Parameter Description
zoom The zoom widget.
filepath The file to read.

Returns: 0 on success, -1 when the file cannot be read or is not a valid image, or an argument is NULL.

xtk_zoom_clear_image

void xtk_zoom_clear_image(XtkZoom *zoom);

Clears the view to black.

The kept pixels are dropped too, so a later resize also shows black.

Parameter Description
zoom The zoom widget.

xtk_zoom_draw

void xtk_zoom_draw(XtkZoom *zoom);

Draws the view now, as an Expose would.

It draws the current view again without taking new pixels from the screen. Use it when the window must be redrawn and no Expose will come, for example after an ancestor window was resized or reparented.

Parameter Description
zoom The zoom widget.

xtk_zoom_set_activation_callback

void xtk_zoom_set_activation_callback(
    XtkZoom *zoom,
    ZoomActivationCallback callback,
    void *user_data);

Sets the function called when Ctrl+Alt+Z starts a pick session.

Parameter Description
zoom The zoom widget.
callback The function, or NULL for none.
user_data Passed to the function.

xtk_zoom_add_exclusion_zone

int xtk_zoom_add_exclusion_zone(XtkZoom *zoom, int x, int y, int width, int height);

Adds an area of the screen where clicks and Enter do not pick.

Use it for the program's own controls, such as a toolbar, so that a click on them during a pick session does not pick their color. There can be at most 8 zones.

Parameter Description
zoom The zoom widget.
x The left edge of the zone, in root window coordinates.
y The top edge of the zone, in root window coordinates.
width The width of the zone in pixels.
height The height of the zone in pixels.

Returns: The index of the new zone (for xtk_zoom_update_exclusion_zone()), or -1 when there are already 8 zones or zoom is NULL.

xtk_zoom_clear_exclusion_zones

void xtk_zoom_clear_exclusion_zones(XtkZoom *zoom);

Removes all exclusion zones.

Parameter Description
zoom The zoom widget.

xtk_zoom_update_exclusion_zone

void xtk_zoom_update_exclusion_zone(
    XtkZoom *zoom,
    int index,
    int x,
    int y,
    int width,
    int height);

Moves or resizes an exclusion zone.

Parameter Description
zoom The zoom widget.
index The index xtk_zoom_add_exclusion_zone() returned. An index that is not a zone does nothing.
x The new left edge, in root window coordinates.
y The new top edge, in root window coordinates.
width The new width in pixels.
height The new height in pixels.

xtk_zoom_set_follow_cursor

void xtk_zoom_set_follow_cursor(XtkZoom *zoom, int enable);

Makes the zoom window follow the pointer during a pick session.

When it is on, the window moves with the pointer, 22 pixels below and to the right of it (or above and to the left near the edges of the screen), like a loupe. The window may be in any parent (the root, or a window of the program): it is moved in its parent's coordinates. Reparent the window before turning this on, as the parent is read then. It stays where it was when the session ends; turn it off then if the window should go back to its place.

Parameter Description
zoom The zoom widget.
enable 1 to follow the pointer, 0 not to (the default).

xtk_zoom_set_hex_readout

void xtk_zoom_set_hex_readout(XtkZoom *zoom, int enable);

Shows or hides the hex readout, and redraws.

The readout shows the color of the center pixel as "#RRGGBB" in the corner set by xtk_zoom_set_hex_position(). It is on by default.

Parameter Description
zoom The zoom widget.
enable 1 to show it, 0 to hide it.

xtk_zoom_set_hex_label_style

void xtk_zoom_set_hex_label_style(
    XtkZoom *zoom,
    const char *font_name,
    int font_size,
    unsigned int foreground_argb,
    unsigned int background_argb);

Sets the font and colors of the hex readout, and redraws.

Parameter Description
zoom The zoom widget.
font_name A fontconfig family name, such as "monospace", or NULL to keep the current font. If the font cannot be opened, the current one is kept.
font_size The font size in pixels; 0 or less keeps the current font.
foreground_argb The text color as 0xAARRGGBB (default 0xFFFFFFFF).
background_argb The color of the box behind the text as 0xAARRGGBB (default 0xCC101020); the alpha is dropped on a 24-bit display.

xtk_zoom_set_hex_border_style

void xtk_zoom_set_hex_border_style(
    XtkZoom *zoom,
    unsigned int border_argb,
    int border_width);

Sets the border of the hex readout's box, and redraws.

Parameter Description
zoom The zoom widget.
border_argb The border color as 0xAARRGGBB; the alpha is dropped on a 24-bit display.
border_width The border width in pixels; 0 or less draws no border (the default).

xtk_zoom_set_hex_position

void xtk_zoom_set_hex_position(XtkZoom *zoom, XtkZoomHexPosition position);

Sets the corner of the hex readout, and redraws.

The values have the same order as ZoomHexPosition in xtk_config.h (config.zoom_hex.position), so that value can be cast to this type.

Parameter Description
zoom The zoom widget.
position The corner.

xtk_zoom_set_hex_value

void xtk_zoom_set_hex_value(XtkZoom *zoom, unsigned long pixel);

Sets the color the hex readout shows, and redraws.

Use it at startup to show the last picked color before the widget has magnified anything; the next magnified view replaces it with the live center pixel.

Parameter Description
zoom The zoom widget.
pixel The color as 0xRRGGBB; higher bits are ignored.

xtk_zoom_regrab_input

void xtk_zoom_regrab_input(XtkZoom *zoom);

Grabs the pointer and the keyboard again during a pick session.

Call it after XReparentWindow() on the zoom window or an ancestor, which releases any grab. It does nothing outside a pick session.

Parameter Description
zoom The zoom widget.