Skip to content

rgbToHex

Converts an RGB(A) color into a hex color string.

r/g/b are clamped to 0-255 and rounded to the nearest integer before formatting. The alpha channel is only appended (as #rrggbbaa) when it is below 1 — fully opaque colors format as the plain 6-digit #rrggbb.

Available since next

import { rgbToHex } from '@helpers4/color';
rgbToHex(color: RgbColor): string
ParameterTypeDescription
colorRgbColorThe color to convert. a defaults to 1 (opaque) when omitted.

string — A lowercase hex color string

Alpha defaults to 1 (opaque) and is omitted from the output.

rgbToHex({ r: 255, g: 0, b: 0 })
// => '#ff0000'

Include alpha when the color is translucent

Section titled “Include alpha when the color is translucent”

A non-opaque color formats as an 8-digit #rrggbbaa string.

rgbToHex({ r: 0, g: 255, b: 0, a: 0.5 })
// => '#00ff0080'

View source on GitHub