RGB → Hex
Paste an RGB value, get hex back. rgb(37, 99, 235) →
#2563eb. The full converter at the bottom also gives
you HSL and OKLCH.
The conversion in one line
rgb(R, G, B) → "#" + hex(R) + hex(G) + hex(B)
each component padded to 2 hex digits, lowercase
Where hex(n) is n.toString(16).padStart(2, "0")
in JavaScript, or format(n, "02x") in Python.
Common conversions
| RGB | Hex | Name |
|---|---|---|
rgb(0, 0, 0) | #000000 | Black |
rgb(255, 255, 255) | #ffffff | White |
rgb(255, 0, 0) | #ff0000 | Red |
rgb(34, 197, 94) | #22c55e | Tailwind green-500 |
rgb(168, 85, 247) | #a855f7 | Tailwind purple-500 |
rgb(244, 114, 182) | #f472b6 | Tailwind pink-400 |
rgb(251, 191, 36) | #fbbf24 | Tailwind amber-400 |
rgb(75, 85, 99) | #4b5563 | Tailwind gray-600 |
In code
// JavaScript
const hex = (r, g, b) =>
"#" + [r, g, b].map(n => n.toString(16).padStart(2, "0")).join("");
hex(37, 99, 235); // "#2563eb"
// Python
def rgb_to_hex(r, g, b):
return "#{:02x}{:02x}{:02x}".format(r, g, b)
// Bash
printf "#%02x%02x%02x\n" 37 99 235
// Go
import "fmt"
fmt.Sprintf("#%02x%02x%02x", r, g, b)
// CSS (Color Module 4 supports rgb() with alpha and modern syntax)
.brand { color: rgb(37 99 235); } // space-separated
.brand { color: rgb(37 99 235 / 0.5); } // alpha Edge cases
- Out-of-range values. CSS clamps RGB to 0–255 silently.
rgb(300, 0, 0)renders as#ff0000. The converter does the same clamping. - Negative values. Same — clamped to 0.
- Float values. CSS Color Module 4 allows decimals.
rgb(37.5, 99, 235)rounds to the nearest integer. - Percentages.
rgb(15%, 39%, 92%)=rgb(38, 99, 235)≈#2663eb. Note: percentages introduce rounding; absolute integers are more precise.
Try the related tools
- Hex → RGB — the reverse direction
- Full color converter — HSL and OKLCH too
- WCAG contrast — check accessibility
FAQ
What input forms are accepted?
The RGB field accepts rgb(r, g, b), r, g, b, r g b, and percentage forms like rgb(50%, 25%, 75%). The output hex uses 6 lowercase digits.
Can I convert RGBA?
The alpha channel doesn't fit in 6-digit hex. CSS Color Module 4 added 8-digit hex (#RRGGBBAA) for alpha — paste an RGBA value and the converter ignores the alpha; for the 8-digit form you can append aa manually (alpha as 2 hex digits).
What if my values are floats (0.0–1.0)?
The converter expects 0–255. If you have 0.0–1.0 floats, multiply by 255 first: 0.5 → 127 (or 128 depending on rounding). Image-processing libraries often use floats; CSS uses integers.