EXEPERTAI LAB

Research alpha

Time Machine
EXEPERTAI LAB
GALLERY / COLLECTION
← Browse Astryx gallery
hooks·hook·@astryxdesign/core

useImageMode

Detects whether an image is predominantly dark or light by sampling pixels via OffscreenCanvas. Uses APCA perceptual lightness (sRGB linearization + power curve) for accurate detection, especially on saturated colors. Runs entirely off the paint path: no visible canvas, no layout thrash. Supports regional sampling for detecting luminance where text overlays will appear. Returns null while loading and falls back gracefully on CORS or network errors.

@astryxdesign/core/hooks

Signature

Call shape
useImageMode(src: string | null | undefined, options: UseImageModeOptions = {}): 'dark' | 'light' | null
Parameters
NameType and behavior
srcstring | null | undefined · required
Image source URL to analyze. When null/undefined, returns the fallback value.
optionsUseImageModeOptions · optional
Optional configuration for image analysis.
options.regionImageSampleRegion · optional
Region to sample within the image using normalized 0-1 coordinates ({ x, y, width, height }). Defaults to the full image.
options.thresholdnumber · optional · default 0.5
Luminance threshold for the dark/light split. Below = dark, above = light.
options.fallback'dark' | 'light' | null · optional · default null
Fallback value while loading or on error.
Returns
NameType and behavior
mode'dark' | 'light' | null
Detected luminance mode of the image. Returns null while loading or if src is null/undefined.

Usage

Detects whether an image is predominantly dark or light by sampling pixels via OffscreenCanvas. Uses APCA perceptual lightness (sRGB linearization + power curve) for accurate detection, especially on saturated colors. Runs entirely off the paint path: no visible canvas, no layout thrash. Supports regional sampling for detecting luminance where text overlays will appear. Returns null while loading and falls back gracefully on CORS or network errors.

  • Pair with MediaTheme to automatically adapt text color over dynamic background images.
  • Use the region option to sample only the area where text overlays will appear for more accurate results.
  • Use for images that change rapidly (e.g., video frames); each src change triggers a new fetch and analysis.