Colore JS

Search documentation

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

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

NameTypeDescription
colorColorInputAny supported CSS color, tagged color object, Color instance or packed RGB number.
otherColorInputThe second color in the expression.
options.weightnumberShare of the second color from 0 to 1. Defaults to 0.5.
options.spaceCssColorMixSpaceInterpolation space, defaulting to oklab. See the supported spaces in parseColorMix.

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.