Skip to content

mixColors

Mixes two colors in sRGB, as CSS color-mix(in srgb, color P%, base) does: percent% of color and the rest of base. Alpha is interpolated in premultiplied space (CSS Color 4), so a translucent color weighs less in the result. Opaque colors give a plain weighted average of the channels.

Available since v3.1.10

import { mixColors } from '@helpers4/color';
// or, from the all-in-one package (same code, one install):
import { mixColors } from 'helpers4/color';
mixColors(color: RgbColor, base: RgbColor, percent: number): RgbColor
ParameterTypeDescription
colorRgbColorThe color to mix in
baseRgbColorThe color it is mixed into
percentnumberShare of `color`, from 0 (all `base`) to 100 (all `color`); clamped. Defaults to 50.

RgbColor — The straight result: channels rounded to integers, alpha to 3 decimals

The same as color-mix(in srgb, red 25%, blue), usable where there is no CSS engine.

mixColors({ r: 255, g: 0, b: 0 }, { r: 0, g: 0, b: 255 }, 25)
// => { r: 64, g: 0, b: 191, a: 1 }

Alpha is interpolated in premultiplied space, as CSS Color 4 specifies.

mixColors({ r: 255, g: 0, b: 0, a: 0.5 }, { r: 0, g: 0, b: 255 })
// => { r: 85, g: 0, b: 170, a: 0.75 }

An RGB(A) color with channels expressed as plain numbers, convenient for further conversion or manipulation.

interface RgbColor {
  a?: number;
  b: number;
  g: number;
  r: number;
}

View source on GitHub