Skip to content

rgbToUnit

Converts an RgbColor (channels from 0 to 255) into a color whose channels are floats from 0 to 1, the way Figma, Sketch and Penpot store them. Alpha is kept as it is.

Available since v3.1.10

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

RgbColor — The color with r, g, b from 0 to 1

Channels from 0 to 1, alpha unchanged.

rgbToUnit({ r: 255, g: 51, b: 0 }) // => { r: 1, g: 0.2, b: 0, 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