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
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.
Related
- mixToCss to emit a CSS expression
- parseColorMix to evaluate an existing CSS expression
- interpolate to mix across multiple stops
- OKLCH in JavaScript for a mixing walkthrough