mixToCss
mixToCss generates a CSS color-mix() expression for two colors. The browser evaluates it in the interpolation space you choose.
Example
import { colore } from 'colore-js';
import { mixToCss } from 'colore-js/fn';
colore('red').mixToCss('blue', { space: 'srgb' }); // 'color-mix(in srgb, color(srgb 1 0 0), color(srgb 0 0 1))'
mixToCss('red', 'blue', { space: 'srgb' }); // 'color-mix(in srgb, color(srgb 1 0 0), color(srgb 0 0 1))'
Syntax
colore(color).mixToCss(other: ColorInput | Color, options?: MixToCssOptions): string
mixToCss(color: ColorInput, other: ColorInput, options?: MixToCssOptions): string
Parameters
Returns
A CSS expression with two serialized color operands. The operands keep more channel precision than ordinary CSS output. Equal weights omit the percentages.
Throws
A ColorError if a color is invalid, the options are invalid or contain an unknown key, or a weight or space is unsupported.
Write a muted color into a stylesheet
Mix an accent with white and give white a quarter of the weight. In the expression, the accent occupies the remaining 75%.
import { mixToCss } from 'colore-js/fn';
mixToCss('blue', 'white', { space: 'oklab', weight: 0.25 }); // 'color-mix(in oklab, color(srgb 0 0 1) 75%, color(srgb 1 1 1))'
Use mix when you want a concrete color in JavaScript. Use parseColorMix to evaluate a supported color-mix() string you already have.
Related
- mix for a computed JavaScript result
- parseColorMix to resolve an existing CSS expression
- OKLCH in JavaScript for choosing a mixing space