Colore JS

Search documentation

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

mix

mix calculates a color between two colors in JavaScript. It defaults to OKLab. Choose another space when the path between the colors matters to your design.

Example

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

colore('#0f766e').mix('#f59e0b', 0.5, 'oklch').css(); // '#759a41'
mix('#0f766e', '#f59e0b', 0.5, 'oklch'); // '#759a41'

Syntax

colore(color).mix(other: ColorInput | Color, ratio?: number, space?: MixColorSpace): Color
mix(color: ColorInput, other: ColorInput, ratio?: number, space?: MixColorSpace): string

Parameters

NameTypeDescription
colorColorInputAny supported CSS color, tagged color object, Color instance or packed RGB number.
otherColorInputThe second color to blend.
rationumberThe share of the second color from 0 to 1. Defaults to 0.5 for an even blend.
spaceMixColorSpaceDefaults to oklab. Also supports oklch, srgb, hsl, hwb, lab, lch, xyz and p3.

Returns

The method returns a new Color. The function returns a CSS string in hex, Display P3 or OKLCH. Alpha is interpolated linearly and hue follows the shortest path in spaces with a hue channel.

Throws

A ColorError if either color is invalid or if the ratio or color space is unsupported.

Choose a color space for the midpoint

Compare halfway blends of the same two colors. Select a space based on the midpoint you want, then keep it consistent across the palette.

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

mix('red', 'blue', 0.5, 'srgb'); // '#7f007f'
mix('red', 'blue', 0.5, 'oklab'); // '#8c53a2'
mix('red', 'blue', 0.5, 'oklch'); // 'oklch(0.53998 0.28545 326.64)'

Use mixToCss if the browser should do the mix. For a browser-authored expression with transparent operands, parseColorMix evaluates the CSS rules, including premultiplied alpha.