Skip to content

unitToRgb

Converts a color whose channels are floats from 0 to 1 (as Figma, Sketch and Penpot store them) into an RgbColor, with channels from 0 to 255. Alpha is already 0–1 and is kept as it is. The channels are not rounded: rgbToCss and rgbToHex do it.

Available since v3.1.10

import { unitToRgb } from '@helpers4/color';
// or, from the all-in-one package (same code, one install):
import { unitToRgb } from 'helpers4/color';
unitToRgb(color: RgbColor): RgbColor
ParameterTypeDescription
colorRgbColorThe color, with `r`, `g`, `b` from `0` to `1`. `a` defaults to 1 (opaque)

RgbColor — The color with channels from 0 to 255

Figma, Sketch and Penpot store channels from 0 to 1.

rgbToHex(unitToRgb({ r: 1, g: 0.5, b: 0, a: 0.5 })) // => '#ff800080'

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