Skip to content

premultiply

Multiplies the color channels by the alpha channel (premultiplied alpha), the form compositing and GPU blending work in. The channels are returned as they are, not rounded, so that unpremultiply gives the color back.

Available since v3.1.8

import { premultiply } from '@helpers4/color';
// or, from the all-in-one package (same code, one install):
import { premultiply } from 'helpers4/color';
premultiply(color: RgbColor): RgbColor
ParameterTypeDescription
colorRgbColorA straight (non-premultiplied) color; a missing alpha is 1

RgbColor — The premultiplied color

The channels are scaled by the alpha, the form GPU blending and canvas pixels use.

premultiply({ r: 255, g: 100, b: 0, a: 0.5 })
// => { r: 127.5, g: 50, b: 0, a: 0.5 }

A missing alpha counts as 1.

premultiply({ r: 10, g: 20, b: 30 })
// => { r: 10, g: 20, b: 30, a: 1 }

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