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
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.
Related
- scale for an array of evenly spaced colors
- mix to blend two colors
- OKLCH in JavaScript to choose a space for a color blend