Skip to content

Mix

Interpolate between two colors.

mix(color1: string, color2: string, ratio?: number, formatOrOptions?: ColorType | MixOptions): string
ParameterDescriptionTypeDefault
color1First colorstring
color2Second colorstring
ratioInterpolation ratio (0 = color1, 1 = color2)number0.5
formatOutput formatColorType
hueHue interpolation modeHueMode’shorter’
spaceInterpolation color spaceColorModelKey’oklch’
import { mix } from 'colorizr';
mix('#ff0044', '#0066ff'); // '#bc34d7'
mix('#ff0044', '#0066ff', 0.2); // '#ed068a'
mix('#ff0044', '#0066ff', 0.8); // '#7351ff'

Playground

#bc34d7

The space option controls which color space is used for interpolation. OkLCH (default) produces perceptually uniform results:

mix('#ff0044', '#0066ff', 0.5, { space: 'oklch' }); // default
mix('#ff0044', '#0066ff', 0.5, { space: 'hsl' });
mix('#ff0044', '#0066ff', 0.5, { space: 'rgb' });
mix('#ff0044', '#0066ff', 0.5, { space: 'oklab' });

Playground

#bc34d7

The hue option controls how hue is interpolated in cylindrical color spaces (OkLCH, HSL):

mix('#ff0000', '#00ff00', 0.5, { hue: 'shorter' }); // default — shortest path
mix('#ff0000', '#00ff00', 0.5, { hue: 'longer' }); // goes the long way around
mix('#ff0000', '#00ff00', 0.5, { hue: 'increasing' }); // always increases hue
mix('#ff0000', '#00ff00', 0.5, { hue: 'decreasing' }); // always decreases hue

Playground

#f99500
interface MixOptions {
format?: ColorType;
space?: 'hsl' | 'oklab' | 'oklch' | 'rgb';
hue?: 'shorter' | 'longer' | 'increasing' | 'decreasing';
}