Colore JS

Search documentation

Find a function or jump to a section.
v2.0.1

interpolate

interpolate samples a color at a position between two or more stops. Pass endpoints for a two-color blend, or add intermediate stops for a gradient that follows a specific path.

Example

import { Color } from 'colore-js';
import { interpolate } from 'colore-js/fn';

const stops = ['#ef4444', '#f59e0b', '#10b981'];

Color.interpolate(stops, 0.5, 'oklch').css(); // '#f59e0b'
interpolate(stops, 0.25);                     // '#f47635'

Syntax

Color.interpolate(colors: (ColorInput | Color)[], t: number, space?: InterpolationColorSpace): Color
interpolate(colors: ColorInput[], t: number, space?: InterpolationColorSpace): string

Parameters

NameTypeDescription
colorsColorInput[]Ordered list of at least two stops. The class method also accepts Color objects.
tnumberPosition from 0 (first stop) to 1 (last stop). Stops divide the range evenly.
spaceInterpolationColorSpaceDefaults to 'oklab'. Also accepts 'oklch', 'srgb', 'p3', 'hsl', 'lab' and 'lch'.

Returns

A Color from the class method or a CSS string from the function. The function uses Display P3 or OKLCH when an output falls outside sRGB.

Throws

A ColorError for invalid colors, fewer than two stops, an unsupported color space, or a position outside 0–1.

Sample a wide-gamut gradient

import { interpolate } from 'colore-js/fn';

interpolate(['color(display-p3 1 0 0)', 'color(display-p3 0 1 0)'], 0.5);
// 'color(display-p3 0.78602 0.67702 0.01518)'

Each segment follows the chosen interpolation space. The function picks a CSS serialization that can hold the resulting color.