Skip to content
colors

rgbToHex

The `rgbToHex` function converts RGB component values into a lowercase HEX color string. Each channel is rounded, clamped to the 0-255 range, and converted into a two-digit hexadecimal value.

1 min read126 words

Function Signature

js

rgbToHex(r: number, g: number, b: number): string

Parameters

  • r (number): Red channel value. Values are rounded and clamped to 0-255.
  • g (number): Green channel value. Values are rounded and clamped to 0-255.
  • b (number): Blue channel value. Values are rounded and clamped to 0-255.

Return Value

A lowercase HEX color string in the format #rrggbb.

js

rgbToHex(255, 102, 0);
// "#ff6600"

rgbToHex(-1, 170.4, 300);
// "#00aaff"

Examples

js

console.log(rgbToHex(255, 255, 255));
// "#ffffff"

console.log(rgbToHex(0, 0, 0));
// "#000000"

console.log(rgbToHex(34, 139, 34));
// "#228b22"

Error Handling

The function does not throw for out-of-range channel values. Numeric inputs are rounded and clamped before conversion.

Usage Notes

  • Always returns lowercase HEX values.
  • Does not output shorthand HEX; it always returns the full 6-character format.
  • Commonly paired with hexToRgb for round-trip conversions.

Previous

hexToRgb

Next

hexAToRgba


Herramientas destacadas

BoardlyLinksyChromoHub de Código

ReadyTools

CarrerasContactoHerramientas
Precios7 días gratis
GuíasDocsBlogUpdatesLaraVault

Seleccionar idioma

Establecer tema

© 2026 ReadyTools. Todos los derechos reservados.