QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

RGB to Hex

Verified

Converts RGB(A) channels to a hex color string, clamping out-of-range values.

  • color
  • conversion

Install

npx shadcn@latest add https://quicksnip.dev/r/rgb-to-hex.json

Code

/** Converts RGB(A) channels to a lowercase hex string. Channels are clamped to 0–255, alpha to 0–1. */
export function rgbToHex(r: number, g: number, b: number, a?: number): string {
  const channel = (n: number) =>
    Math.round(Math.min(255, Math.max(0, n)))
      .toString(16)
      .padStart(2, "0");
  const alpha = a === undefined ? "" : channel(Math.min(1, Math.max(0, a)) * 255);
  return `#${channel(r)}${channel(g)}${channel(b)}${alpha}`;
}

Usage

rgbToHex(0, 181, 184);      // "#00b5b8"
rgbToHex(255, 0, 0, 0.5);   // "#ff000080"