Skip to content

unpremultiply

Divides the color channels by the alpha channel: the inverse of premultiply. A fully transparent color has lost its channels, so it comes back as transparent black.

Available since v3.1.8

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

RgbColor — The straight (non-premultiplied) color

Divides the channels by the alpha, the inverse of premultiply.

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

A transparent color becomes transparent black

Section titled “A transparent color becomes transparent black”

The channels are lost at alpha 0, so there is nothing to recover.

unpremultiply({ r: 0, g: 0, b: 0, a: 0 })
// => { r: 0, g: 0, b: 0, a: 0 }

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