# Convert RGB color to hex

Pack three 0–255 channels into a #rrggbb string.

Canonical: https://1loc.dev/misc/convert-rgb-color-to-hex/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function rgbToHex({ red, green, blue }) { return `#${((1 << 24) + (red << 16) + (green << 8) + blue).toString(16).slice(1)}`; }
```

## Input

```json
{
  "red": 28,
  "green": 97,
  "blue": 65
}
```

## Expected output

```json
"#1c6141"
```

## Edge case

Input:
```json
{
  "red": 0,
  "green": 0,
  "blue": 0
}
```
Output:
```json
"#000000"
```

## Limits

- The 1 << 24 term keeps leading zeros. Channels must be integers from 0 to 255.

The lab validates types and size before calling this function. Add equivalent validation at your own trust boundary.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/convert-rgb-color-to-hex.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Takes named channels and validates 0–255 integers. Original MDX retained as inert source evidence.
