Function Signature
js
rgbToHex(r: number, g: number, b: number): stringParameters
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
hexToRgbfor round-trip conversions.