Colore JS

Search documentation

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

fromCssMix

Color.fromCssMix resolves a CSS color-mix() expression in JavaScript. Use parseColorMix from colore-js/fn for a tagged value you can pass to other functions.

Example

import { Color } from 'colore-js';
import { parseColorMix } from 'colore-js/fn';

const expression = 'color-mix(in srgb, red, blue)';
Color.fromCssMix(expression).hex(); // '#800080'
parseColorMix(expression).space; // 'srgb-linear'

Syntax

Color.fromCssMix(expression: string): Color
parseColorMix(expression: string): ColorValue

Parameters

NameTypeDescription
expressionstringA color-mix(in <space>, <color>, <color>) expression. Each operand can include a weight from 0% to 100%. Nested mixes are supported.

Returns

The static method returns a Color. The function returns a ColorValue with linear sRGB channels and alpha. The mix uses an explicit interpolation space from srgb, srgb-linear, lab, lch, oklab, oklch, hsl, hwb, xyz, xyz-d65 or display-p3.

Throws

A ColorError for an ordinary color, unsupported or malformed expression, or two weights of 0%. A var(--brand) operand needs stylesheet context to resolve.

Mix a color with transparency

A transparent operand affects alpha without tinting the visible color. In polar spaces, shorter hue is the default. You can choose longer hue, increasing hue or decreasing hue.

import { Color } from 'colore-js';

Color.fromCssMix('color-mix(in srgb, red 25%, transparent)').rgb(); // 'rgb(255 0 0 / 0.25)'
Color.fromCssMix('color-mix(in oklch, oklch(60% 0.2 none), oklch(80% 0.1 240))').oklch(); // 'oklch(0.70000 0.15000 240.00)'

An authored none channel can take its counterpart from the other operand before the mix. If explicit weights total less than 100%, that total multiplies the mixed alpha. The two-color syntax here requires an explicit interpolation space.

  • parse to handle mixes alongside ordinary colors
  • mixToCss to write a CSS expression
  • mix to blend two values directly